日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

2022年1月17日

發(fā)布時(shí)間:2024/1/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2022年1月17日 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

總結(jié)了一個(gè)關(guān)于鏈表的合并的模板,可以進(jìn)行排序合并,主要思路是head1和head2兩個(gè)鏈表從第一位置開始依次比較,最后存儲(chǔ)到head3中;

?

構(gòu)建一個(gè)雙向鏈表并進(jìn)行刪除和插入操作,按要求輸出。

輸入格式

輸入:

第一行輸入元素個(gè)數(shù)M

第二行輸入M個(gè)元素

第三行輸入刪除位置,位置為0時(shí)不刪除

第四行輸入插入位置和插入元素

第五行輸入輸出時(shí)的起始位置

輸出格式

按要求的起始位置輸出鏈表

樣例輸入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é)點(diǎn)的線性鏈表list,鏈表中結(jié)點(diǎn)構(gòu)造為(data、link),其中data為數(shù)據(jù)域,link為指針域。請(qǐng)寫一算法,將該鏈表按結(jié)點(diǎn)數(shù)據(jù)域的值的大小從小到大重新鏈接。要求鏈接過程中不得使用除該鏈表以外的任何鏈結(jié)點(diǎn)空間。

輸入格式

自定義鏈表節(jié)點(diǎn)數(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é)點(diǎn)的和不帶頭結(jié)點(diǎn)的,用malloc(sizeof(struct node));可以定義鏈表結(jié)點(diǎn)的寬度及鏈表的大小

總結(jié)

以上是生活随笔為你收集整理的2022年1月17日的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。