2022年1月17日
總結(jié)了一個關(guān)于鏈表的合并的模板,可以進行排序合并,主要思路是head1和head2兩個鏈表從第一位置開始依次比較,最后存儲到head3中;
?
構(gòu)建一個雙向鏈表并進行刪除和插入操作,按要求輸出。
輸入格式
輸入:
第一行輸入元素個數(shù)M
第二行輸入M個元素
第三行輸入刪除位置,位置為0時不刪除
第四行輸入插入位置和插入元素
第五行輸入輸出時的起始位置
輸出格式
按要求的起始位置輸出鏈表
樣例輸入content_copy
8
1 2 3 4 5 6 7 8
6
6 6
5
樣例輸出content_copy
5 6 7 8 1 2 3 4
#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
? ? int data;
? ? node *next;
? ? node *front;
} node;
node *creat(int num)
{
? ? node* head=(struct node*)malloc(sizeof(struct node));
? ? node* p1;
? ? node* p2=head;
? ? int data;
? ? for(int i=0; i<num; i++)
? ? {
? ? ? ? scanf("%d",&data);
? ? ? ? p1=(struct node*)malloc(sizeof(struct node));
?? ??? ?p1->data = data;
?? ??? ?p1->front = p2;
?? ??? ?p2->next = p1;
?? ??? ?p2 = p1;
? ? }
? ? p2->next = NULL;
? ? return head->next;
}
node *del(node *head,int n){
?? ?if(n==0){
?? ??? ?return head;
?? ?}
?? ?node *p;
?? ?p = head;
?? ?for(int i=1;i<n;i++){
?? ??? ?p = p->next;
?? ?}
?? ?p->front->next = p->next;
?? ?if(p->next){
?? ??? ?p->next->front = p->front;
?? ?}
? ? return head;
}
node *insert(node *head,int n,int m){
?? ?node *p,*q;
?? ?p = head;
?? ?for(int i=1;i<n;i++){
?? ??? ?p = p->next;
?? ?}
?? ?q = (struct node*)malloc(sizeof(struct node));
?? ?q->data = m;
?? ?q->front = p->front;
?? ?q->next = p;
?? ?p->front->next = q;
?? ?p->front = q;
? ? return head;
}
int main()
{
? ? int n;
? ? scanf("%d",&n);
? ? node *head,*p;
? ? head=creat(n);
? ? int num1,num2,element,num3;
? ? scanf("%d",&num1);
? ? scanf("%d %d",&num2,&element);
? ? scanf("%d",&num3);
? ??
? ? head = del(head,num1);
?? ?head = insert(head,num2,element);
?? ?
?? ?node *head_pre = head;
?? ?for(int i=1;i<num3;i++){
?? ??? ?head_pre = head_pre->next;
?? ?}
?? ?
?? ?while(head_pre){
?? ??? ?printf("%d ",head_pre->data);
?? ??? ?head_pre = head_pre->next;
?? ?}
?? ?for(int i=0;i<num3;i++){
?? ??? ?printf("%d ",head->data);
?? ??? ?head = head->next;
?? ?}
?? ?
?? ?return 0;
}
(線性表)已知不帶頭結(jié)點的線性鏈表list,鏈表中結(jié)點構(gòu)造為(data、link),其中data為數(shù)據(jù)域,link為指針域。請寫一算法,將該鏈表按結(jié)點數(shù)據(jù)域的值的大小從小到大重新鏈接。要求鏈接過程中不得使用除該鏈表以外的任何鏈結(jié)點空間。
輸入格式
自定義鏈表節(jié)點數(shù)
m=5
3 1 5 4 6
輸出格式
1 3 4 5 6
樣例輸入content_copy
8
10 1 5 14 32 55 67 6
樣例輸出content_copy
1 5 6 10 14 32 55 67
#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
? ? int data;
? ? node *next;
} node;
node *creat(int num)
{
? ? node* head=NULL;
? ? node* p1=head;
? ? node* p2=NULL;
? ? int data;
? ? for(int i=0; i<num; i++)
? ? {
? ? ? ? scanf("%d",&data);
? ? ? ? if(head==NULL)
? ? ? ? {
? ? ? ? ? ? head=(struct node*)malloc(sizeof(struct node));
? ? ? ? ? ? p1=head;
? ? ? ? ? ? head->data=data;
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? p2=(struct node*)malloc(sizeof(struct node));
? ? ? ? ? ? p1->next=p2;
? ? ? ? ? ? p2->data=data;
? ? ? ? ? ? p1=p2;
? ? ? ? ? ? p1->next=NULL;
? ? ? ? }
? ? }
? ? return head;
}
node *sort(node *head){
?? ?node *p,*q;
?? ?int t;
?? ?for(p=head;p!=NULL;p=p->next){
?? ??? ?for(q=p->next;q!=NULL;q=q->next){
?? ??? ??? ?if(p->data > q->data){
?? ??? ??? ??? ?t = p->data;
?? ??? ??? ??? ?p->data = q->data;
?? ??? ??? ??? ?q->data = t;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?return head;
}
int main()
{
? ? int n;
? ? scanf("%d",&n);
? ? node *head;
? ? head = creat(n);
? ? head = sort(head);
? ? while(head){
? ? ?? ?printf("%d ",head->data);
? ? ?? ?head = head->next;
?? ?}
?? ?return 0;
}
鏈表分為帶頭結(jié)點的和不帶頭結(jié)點的,用malloc(sizeof(struct node));可以定義鏈表結(jié)點的寬度及鏈表的大小
總結(jié)
以上是生活随笔為你收集整理的2022年1月17日的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 了解 Windows Azure 存储计
- 下一篇: String,StringBuilder