数据结构 链表and顺序表
链表
7-3 单链表的创建,遍历与销毁
从键盘输入任意多个正整数,输入以-1结束。逆序输出这些整数(不包括-1)。
提示:
1、逆序创建单链表。结点数据域是整型数。每输入一个整数,向链表中插入一个结点。当输入-1时结束链表的创建。
2、遍历链表,输出结点数据域的值。
3、遍历完成后,要求销毁该链表。
输入格式:
任意多的正整数,输入序列以-1结束。
输出格式:
逆序输出这些整数(不包括-1)。
输入样例:
在这里给出一组输入。例如:
3 8 2 9 7 4 -1
输出样例:
在这里给出相应的输出。例如:
4 7 9 2 8 3
#include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; }*list; list creat()//逆序建立链表 { list head,p; int n; head=(list)malloc(sizeof(struct node)); head->next=NULL; while(scanf("%d",&n)&&n!=-1) { p=(list)malloc(sizeof(struct node)); p->data=n; p->next=head->next;//新节点指向原本的最后一个节点 head->next=p;//head指向新节点(新的最后一个节点) } return head; } void print(list head) { list p; p=head->next; while(p!=NULL) { printf("%d ",p->data); p=p->next; } } int main() { list head; head=creat(); print(head); free(head); return 0; }
7-4 排序和逆置单链表
7-4 程序设计综合实践 1.3
1.3 在第1题( 编写程序,建立2个带头结点单链表,输入若干整数将正整数插入第1个单链表,将负整数插入第2个单链表,插入前和插入后单链表保持递增或相等次序,显示2个单链表,最后销毁。程序不可存在内存泄漏。)建立2个单链表基础上,设计和实现就地逆置单链表函数,即利用原单链表结点建立元素次序相反的单链表。编写程序,建立2个单链表,就地逆置这2个单链表,显示逆置前后的各单链表。注意不可存在内存泄漏。
输入格式:
若干整数。
输出格式:
每个单链表输出占一行,元素间用分隔符分隔;两个初始单链表和两个就地逆置后单链表,4个单链表,共4行。
输入样例:
100 2 3 -2 -8 -6 -9 -10 50 2 -1
输出样例:
2->2->3->50->100
-10->-9->-8->-6->-2->-1
100->50->3->2->2
-1->-2->-6->-8->-9->-10
#include<stdio.h> #include<stdlib.h> /* 思路: (1)输入一排数据 (2)申请两个链表,将正数和负数分别放入在两个链表中 (3)将两条链表从低到高排序 (4)输出两条链表 (5)将两条链表逆序然受分别输出 */ struct Node { int data; struct Node* next; }; //创建头节点 struct Node* Create_head() { struct Node* head = (struct Node*)malloc(sizeof(struct Node)); if (head != NULL) { head->next = NULL; } return head; } //创建新节点 struct Node* Create_node(int data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); if (new_node != NULL) { new_node->data = data; new_node->next = NULL; } return new_node; } //尾插法 struct Node* Insert_node(struct Node* head, struct Node* new_node) { struct Node* temp; temp = head; while (temp->next !=NULL) { temp = temp->next; } temp->next = new_node; } //打印链表 void Print_node(struct Node* head) { struct Node* temp = head; if(temp==NULL){//判断链表是否为空 free(temp); } if(temp!=NULL&&temp->next!=NULL){ temp=temp->next; printf("%d", temp->data); } while (temp->next != NULL) { temp = temp->next; printf("->%d", temp->data); } printf("\n"); } //排序 void Sort_list(struct Node* head) { //对链表中的元素进行顺序排序 struct Node* posFrontNode = head; struct Node* posNode; int t; for (posFrontNode = posFrontNode->next; posFrontNode != NULL; posFrontNode = posFrontNode->next) { for (posNode = posFrontNode->next; posNode != NULL; posNode = posNode->next) { if (posFrontNode->data > posNode->data) { t = posFrontNode->data; posFrontNode->data = posNode->data; posNode->data = t; } } } } //反转链表 struct Node* reverseList(struct Node* headNode) { struct Node* posFrontNode; //将第一个节点赋给posNode struct Node* posNode = headNode->next; //将传进来的链表变成空链表 headNode->next = NULL; //此时的posNode代表传进来的链表(且从第一个链表开始) while (posNode != NULL) { posFrontNode = posNode; posNode = posNode->next;//此时的posNode代表posFrontNode后面的一个节点 posFrontNode->next = headNode->next; headNode->next = posFrontNode; } return headNode; } //主函数 int main() { int n; struct Node* list_zheng = Create_head(); struct Node* list_fu = Create_head(); while (scanf("%d",&n)!=EOF) { struct Node* m = Create_node(n); if (n >= 0) { Insert_node(list_zheng, m); } else { Insert_node(list_fu, m); } } Sort_list(list_zheng); Sort_list(list_fu); Print_node(list_zheng); Print_node(list_fu); reverseList(list_zheng); reverseList(list_fu); Print_node(list_zheng); Print_node(list_fu); return 0; }
7-1 两个有序链表序列的合并
输入格式:
输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。
输出格式:
在一行中输出合并后新的非降序链表,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL
输入样例:
1 3 5 -1
2 4 6 8 10 -1
输出样例:
1 2 3 4 5 6 8 10
#include<bits/stdc++.h> using namespace std; struct node { int data; node *next; }; node* creat(node *head) { node *q,*p; q=head; int t; while(cin>>t&&t!=-1) { p = new node; p->next=NULL; p->data=t; q->next = p; q=q->next; } return head; } node* merge_list(node *head1,node *head2) { node *p1=head1->next; node *p2=head2->next; node *head=new node; head->next=NULL; node *q=head; while(p1!=NULL&&p2!=NULL) { if(p1->data<=p2->data) { q->next=p1; q=q->next; // q->next=NULL; p1=p1->next; } else { q->next=p2; q=q->next; // q->next=NULL; p2=p2->next; } } if(p2!=NULL) { q->next=p2; } else if(p1!=NULL) { q->next=p1; } else { q->next=NULL; } return head; } int main() { node *head1,*head2,*head,*p; head1=new node; head1->next=NULL; head1=creat(head1); head2=new node; head2->next=NULL; creat(head2); head=merge_list(head1,head2); p=head->next; if(p==NULL) cout<<"NULL"<<endl; else { while(p!=NULL) { if(p->next==NULL) printf("%d\n",p->data); else printf("%d ",p->data); p=p->next; } } }
7-7 约瑟夫环
作者 李廷元
单位 中国民用航空飞行学院
N个人围成一圈顺序编号,从1号开始按1、2、3......顺序报数,报p者退出圈外,其余的人再从1、2、3开始报数,报p的人再退出圈外,以此类推。
请按退出顺序输出每个退出人的原序号。
输入格式:
输入只有一行,包括一个整数N(1<=N<=3000)及一个整数p(1<=p<=5000)。
输出格式:
按退出顺序输出每个退出人的原序号,数据间以一个空格分隔,但行尾无空格。
输入样例:
在这里给出一组输入。例如:
7 3
输出样例:
3 6 2 7 5 1 4
#include<bits/stdc++.h> using namespace std; struct node{ int data; node *next; }; int main() { int n,de_num; cin>>n>>de_num; node *head,*p,*q,*ttt; head = new node; head->next= NULL; q=head; for(int i=1;i<=n;i++){ p=new node; p->next=NULL; p->data=i; q->next=p; q=q->next; } q->next=head->next; p=head; q=head->next; while(n--){ int t=de_num; while(1){ if(t==1){ cout<<q->data<<" "; ttt=q; q=q->next; p->next=q; free(ttt); break; } else{ p=q; q=q->next; t--; } } } return 0; }
顺序表
7-2 顺序表的建立及遍历
读入n值及n个整数,建立顺序表并遍历输出。
输入格式:
读入n及n个整数
输出格式:
输出n个整数,以空格分隔(最后一个数的后面没有空格)。
输入样例:
在这里给出一组输入。例如:
4
-3 10 20 78
输出样例:
在这里给出相应的输出。例如:
-3 10 20 78
#include <bits/stdc++.h> using namespace std; #define ll long long struct seqlist { ll* data; ll length; ll last; }; seqlist creat(int n) { seqlist a; a.data=new ll[n+1]; a.last=-1; a.length=n; for(ll i=0;i<a.length;i++) { cin>>a.data[i]; } return a; } void output(seqlist a) { for(ll i=0;i<a.length;i++) { if(i==a.length-1) cout<<a.data[a.length-1]<<endl; else cout<<a.data[i]<<" "; } } int main() { ll n; cin>>n; seqlist a=creat(n); output(a); return 0; }
7-3 顺序表(删除)
已知一组数据,采用顺序存储结构存储,其中所有的元素为整数。设计一个算法,删除元素值在[x,y]之间的所有元素
输入格式:
输入包含三行数据,第一行是表中元素个数,第二行是顺序表的各个元素,第三行是区间x和y。
输出格式:
删除元素值在[x,y]之间的所有元素后,输出新的顺序表。(最后无空格)
输入样例:
在这里给出一组输入。例如:
10
55 11 9 15 67 12 18 33 6 22
10 20
输出样例:
在这里给出相应的输出。例如:
55 9 67 33 6 22
#include <bits/stdc++.h> using namespace std; #define ll long long struct seqlist { ll* data; ll length; ll last; }; seqlist creat(int n) { seqlist a; a.data=new ll[n+1]; a.last=-1; a.length=n; for(ll i=0;i<a.length;i++) { cin>>a.data[i]; } return a; } void output(seqlist a) { for(ll i=0;i<a.length;i++) { if(i==a.length-1) cout<<a.data[a.length-1]; else cout<<a.data[i]<<" "; } } void de(seqlist &a,int key) { for(int i=key;i<a.length-1;i++) { a.data[i]=a.data[i+1]; } } void deleted(seqlist &a,int mi,int ma) { int i=0; while(i<a.length) { if(a.data[i]<=ma&&a.data[i]>=mi) { de(a,i); a.length--; } if(a.data[i]<mi||a.data[i]>ma) { i++; } } } int main() { ll n; cin>>n; seqlist a=creat(n); int be,en; cin>>be>>en; deleted(a,be,en); output(a); return 0; }
本文作者:kingwzun
本文链接:https://www.cnblogs.com/kingwz/p/18547484
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2022-11-15 六、总线系统
2021-11-15 7-1 sdut-List--C~K的班级(I) (10 分)