数据结构习题代码

设计一个带头结点的单链表中删除一个最大值结点的算法。
 1 //设计一个带头结点的单链表中删除一个最大值结点的算法。
 2 #include <iostream>
 3 #include <bits/stdc++.h>
 4 #define test cout<<"****"<<endl;
 5 using namespace std;
 6 typedef struct Node{
 7   int val;
 8   struct Node *next;
 9 }Node;
10 int Max=0;
11 Node *head=(Node*)malloc(sizeof(Node));
12 
13 void getlist(int n){
14     Node *p=(Node*)malloc(sizeof(Node));
15     p=head;
16   for(int i=0;i<n;i++){
17       Node *q=(Node*)malloc(sizeof(Node));
18       cin>>q->val;
19       p->next=q;
20       q->next=NULL;
21       p=q;
22   }
23 }
24 
25 void Delete(Node *head){
26     struct Node *p,*max;
27     int tmp;
28     p=head;
29     tmp=p->val;
30     max=head;
31     while(p->next!=NULL){
32         p=p->next;
33         if(p->val>tmp){
34             max=p;
35             tmp=p->val;
36         }
37     }
38     if(max->next!=NULL){
39         tmp=max->next->val;
40         max->val=tmp;
41         max->next=max->next->next;
42     }else{
43         Node *k=(Node*)malloc(sizeof(Node));
44         k=head;
45         while(k->next!=NULL){
46             if(k->next==max){
47                 break;
48             }
49             k=k->next;
50         }
51         k->next=NULL;
52     }
53 }
54 void output(){
55     struct Node *p;
56     p=head;
57     while(p->next!=NULL){
58         p=p->next;
59         cout<<p->val<<" ";
60     }
61     cout<<endl;
62 }
63 int main(){
64     int n;
65     cout<<"输入链表长度:";
66     cin>>n;
67     getlist(n);
68     Delete(head);
69     output();
70     return 0;
71 }
View Code

设计一个算法,从顺序表hq中删除a到b的元素,并保证剩余元素相对次序不变。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <cstring>
 5 #define size 100
 6 #define test   cout<<"****"<<endl;
 7 typedef struct{
 8   int date[size];
 9   int length;
10 }List;
11 bool k[100];
12 void init(List *l,int n){
13   printf("输入顺序表的元素:");
14   for(int i=0;i<n;i++){
15     scanf("%d",&l->date[i]);
16   }
17   l->length=n;
18 }
19 void delet(List *l){
20   int a,b;
21   printf("输入要删除的区间范围:");
22   scanf("%d%d",&a,&b);
23   int j=0;
24   for(int i=0;i<l->length;i++){
25       if(!(l->date[i]>=a&&l->date[i]<=b))
26         l->date[j++]=l->date[i];
27   }
28   l->length=j;
29 }
30 void output(List *l){
31   printf("输出去掉a到b的值的链表:");
32   for(int i=0;i<l->length;i++){
33     printf("%d ",l->date[i]);
34   }
35   printf("
");
36 }
37 int main(){
38   int m;
39   List l;
40   printf("输入线性表的长度:");
41   scanf("%d",&m);
42   // test;
43   init(&l,m);
44   memset(k,0,sizeof(k));
45   delet(&l);
46   output(&l);
47   return 0;
48 }
View Code

设计一个算法从顺序表L中删除所有值为x的元素

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <cstring>
 5 #define size 100
 6 #define test   cout<<"****"<<endl;
 7 typedef struct{
 8     int date[size];
 9     int length;
10 }List;
11 
12 using namespace std;
13 
14 void init(List *l,int n){
15     printf("输入顺序表的元素:");
16     for(int i=0;i<n;i++){
17       scanf("%d",&l->date[i]);
18     }
19     l->length=n;
20 }
21 
22 void Delete(List *L){
23     int x;
24     printf("输入所要删除的X:");
25     scanf("%d",&x);
26     int j=0;
27     for(int i=0;i<L->length;i++){
28         int y=L->date[i];
29         if(y!=x){
30             L->date[j++]=L->date[i];
31         }
32     }
33     L->length=j;
34 }
35 
36 void output(List *l){
37     printf("输出去掉x的值的链表:");
38     for(int i=0;i<l->length;i++){
39         printf("%d ",l->date[i]);
40     }
41     printf("
");
42 }
43 int main(){
44     int n;
45     printf("输入线性表的长度:");
46     scanf("%d",&n);
47     List l;
48     init(&l,n);
49     Delete(&l);
50     output(&l);
51     return 0;
52 }
View Code

设有一个线性链表,其头指针为head,试编写一个算法计算数据域为x的结点个数。

 1 #include <iostream>
 2 #include <bits/stdc++.h>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 #include <cstring>
 6 typedef struct LinkList{
 7   int val;
 8   struct LinkList *next;
 9 }LinkList;
10 using namespace std;
11 
12 LinkList *head=(LinkList*)malloc(sizeof(LinkList));
13 
14 void getlist(int n){
15     LinkList *p=(LinkList*)malloc(sizeof(LinkList));
16     p=head;
17   for(int i=0;i<n;i++){
18       LinkList *q=(LinkList*)malloc(sizeof(LinkList));
19       cin>>q->val;
20       p->next=q;
21       q->next=NULL;
22       p=q;
23   }
24 }
25 
26 void count(LinkList *L) {
27   int sum=0;
28   int x;
29   printf("请输x的值:");
30   scanf("%d",&x);
31   LinkList *p;
32   if(!L)
33     return ;
34     p=L->next;
35   while(p) {
36     if(p->val==x) {
37       sum++;
38     }
39     p=p->next;
40   }
41   printf("链表中X域的元素的个数为:%d
",sum);;
42 }
43 
44 void output(){
45   LinkList *p=(LinkList*)malloc(sizeof(LinkList));
46   p=head;
47   while(p->next!=NULL){
48       p=p->next;
49       cout<<p->val<<" ";
50   }
51   cout<<endl;
52 }
53 int main(){
54     int n;
55     cout<<"输入链表长度:";
56     cin>>n;
57     getlist(n);
58     count(head);
59     output();
60     return 0;
61 }
View Code

用顺序表表示集合,实现集合的求差集运算

 1 #include <iostream>
 2 #include <bits/stdc++.h>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #define size 100
 7 using namespace std;
 8 
 9 typedef struct{
10   int date[size];
11   int length;
12 }List;
13 bool k[100];
14 
15 void init(List *l,int n){
16   for(int i=0;i<n;i++){
17     scanf("%d",&l->date[i]);
18   }
19   l->length=n;
20 }
21 
22 void output(List *A,int n,List *B,int m){
23   printf("输出在顺序A中而不在B中的值:");
24   for(int i=0;i<m;i++){
25     int x=B->date[i];
26     k[x]=1;
27   }
28   for(int i=0;i<n;i++){
29     int x=A->date[i];
30     if(!k[x])
31       printf("%d ",x);
32   }
33   printf("
");
34 }
35 int main(){
36     int n,m;
37     List A,B;
38     printf("输入A表的长度:");
39     scanf("%d",&n);
40     init(&A,n);
41     printf("输入B表的长度:");
42     scanf("%d",&m);
43     init(&B,m);
44     output(&A,n,&B,m);
45     return 0;
46 }
View Code

设计一个算法,利用栈的基本运算返回栈的栈底元素

 1 #include <iostream>
 2 #include <stack>
 3 using namespace std;
 4 stack<int> s;
 5 void init(int n){
 6     int x;
 7     while(n--){
 8         scanf("%d",&x);
 9         s.push(x);
10     }
11 }
12 void Return(){
13   int x;
14   printf("栈底元素为:");
15   while(!s.empty()){
16     x=s.top();
17     s.pop();
18   }
19   printf("%d
",x);
20 }
21 int main(){
22     int n;
23     printf("输入栈的大小:");
24     scanf("%d",&n);
25     init(n);
26     Return();
27     return 0;
28 }
View Code

在hq的链队中,设计一个算法求该链队中结点个数。

 1 #include <iostream>
 2 using namespace std;
 3 typedef struct Node{
 4     int date;
 5     struct Node *next;
 6 }Node;
 7 
 8 Node *head=(Node*)malloc(sizeof(Node));
 9 
10 
11 void getNode(int n){
12     Node *p=(Node*)malloc(sizeof(Node));
13     p=head;
14   for(int i=0;i<n;i++){
15       Node *q=(Node*)malloc(sizeof(Node));
16       cin>>q->date;
17       p->next=q;
18       q->next=NULL;
19       p=q;
20   }
21 }
22 
23 int Count(Node*head){
24     int count=0;
25     Node*top=head;
26     while(top->next!=NULL){
27         count++;
28         top=top->next;
29     }
30     return count;
31 }
32 
33 int main(){
34     int n;
35     printf("输入链队长度:");
36     scanf("%d",&n);
37     getNode(n);
38     int sum=Count(head);
39     printf("链队长为:%d
",sum);
40     return 0;
41 }
View Code

设计一个算法,从顺序表hq中删除重复的元素,并保证剩余元素相对次序不变

 1 #include <iostream>
 2 #define size 100
 3 
 4 using namespace std;
 5 typedef struct List{
 6     int date[size];
 7     int length;
 8 }List;
 9 
10 int k[100];
11 void init(List *l,int n){
12     int x;
13     for(int i=0;i<n;i++){
14         cin>>x;
15         l->date[i]=x;
16     }
17     l->length=n;
18 }
19 
20 void Delete(List *l){
21     int j=0;
22     for(int i=0;i<l->length;i++){
23         int x=l->date[i];
24         if(!k[x]){
25             l->date[j++]=x;
26             k[x]=1;
27         }
28     }
29     l->length=j;
30 }
31 
32 void output(List *l){
33     cout<<"输出去重后的顺序表:";
34     for(int i=0;i<l->length;i++){
35         cout<<l->date[i]<<" ";
36     }
37     cout<<endl;
38 }
39 
40 int main(){
41     int n;
42     cout<<"输入hq表的长度:";
43     cin>>n;
44     List hq;
45     init(&hq,n);
46     Delete(&hq);
47     output(&hq);
48     return 0;
49 }
View Code

设计一个算法,利用队列和栈的基本运算将指定队列中的内容进行逆转

 1 #include <iostream>
 2 #include <queue>
 3 #include <stack>
 4 
 5 using namespace std;
 6 queue<int> q;
 7 stack<int> s;
 8 void init(int n){
 9     cout<<"输入队列:";
10     while(n--){
11         int x;
12         cin>>x;
13         q.push(x);
14     }
15 }
16 
17 void change(){
18     while(!q.empty()){
19     int x=q.front();
20     s.push(x);
21     q.pop();
22   }
23   while(!s.empty()){
24     int x=s.top();
25     q.push(x);
26     s.pop();
27   }
28 }
29 
30 void output(){
31     cout<<"输出倒置后的队列:";
32     while(!q.empty()){
33         int x=q.front();
34         cout<<x<<" ";
35         q.pop();
36     }
37     cout<<endl;
38 }
39 int main(){
40     int n;
41     cout<<"输入队列有多长:";
42     cin>>n;
43     init(n);
44     change();
45     output();
46     return 0;
47 }
View Code
原文地址:https://www.cnblogs.com/zllwxm123/p/7979776.html