数据结构与算法(C++版本)-链表

单链表的反转

将学生成绩程序中的学生成绩结点按照座号反向输出

 1 #include<iostream>
 2 #include<iomanip>
 3 #include<ctime>
 4 #include<cstdlib>
 5 using namespace std;
 6 class list
 7 {
 8 public:
 9     int num,score;
10     char name[10];
11     class list *next;
12 };
13 typedef class list node;
14 typedef node *link;
15 int main()
16 {
17     link ptr,last,before;
18     int i,j,findword = 0,data[12][2];
19     char namedata[12][10] = {{"Allen"},{"Mako"},{"Lean"},{"Melissa"},{"Angle"},{"Sabrina"},{"Joyce"},{"Jasica"},{"Hanson"},
20     {"Amy"},{"Bob"},{"Jack"}};
21     srand((unsigned)time(NULL));
22     for(i = 0;i < 12;i++)
23     {
24         data[i][0] = i + 1;
25         data[i][1] = rand()%50 + 51;
26     }
27     link head = new node;
28     if(!head)
29     {
30         cout << "Error!!内存地址申请失败!!" << endl;
31         exit(1);
32     }
33     head->num = data[0][0];
34     for(j = 0;j < 10; j++)
35         head->name[j] = namedata[0][j];
36     head->score = data[0][1];
37     head->next = NULL;
38     ptr = head;
39     for(i = 1;i < 12;i++)
40     {
41         link newnode = new node;
42         newnode->num = data[i][0];
43         for(j = 0;j < 10; j++)
44             newnode->name[j] = namedata[i][j];
45         newnode->score = data[i][1];
46         newnode->next = NULL;
47         ptr->next = newnode;
48         ptr = ptr->next;
49     }
50     ptr = head;
51     i = 0;
52     cout << "原始表资数" << endl;
53     while(ptr != NULL)
54     {
55         cout << "[" << setw(2) << ptr->num << setw(8)
56             << ptr->name << setw(3) << ptr->score << "] -> ";
57         i++;
58         if(i >= 3)
59         {
60             cout << endl;
61             i = 0;
62         }
63         ptr = ptr->next;
64     }
65     ptr = head;
66     before = NULL;
67     cout << "\n反转后表数据" << endl;
68     while(ptr != NULL)
69     {
70         last = before;
71         before = ptr;
72         ptr = ptr->next;
73         before->next = last;
74     }
75     ptr = before;
76     while(ptr != NULL)
77     {
78         cout << "[" << setw(2) << ptr->num << setw(8)
79             << ptr->name << setw(3) << ptr->score << "] -> ";
80         i++;
81         if(i > 3)
82         {
83             cout << endl;
84             i = 0;
85         }
86         ptr = ptr->next;
87     }
88     system("pause");
89 }
View Code

单链表的连接

将学生成绩程序中的学生成绩与新的学生成绩表连接起来

  1 #include<iostream>
  2 #include<iomanip>
  3 #include<ctime>
  4 #include<cstdlib>
  5 using namespace std;
  6 class list
  7 {
  8 public:
  9     int num,score;
 10     char name[10];
 11     class list *next;
 12 };
 13 typedef class list node;
 14 typedef node *link;
 15 link concatlist(link,link);
 16 int main()
 17 {
 18     link head,ptr,newnode,last,before;
 19     link head1,head2;
 20     int i,j,findword = 0,data[12][2];
 21     char namedata1[12][10] = {{"Allen"},{"Scott"},{"Marry"},{"Jon"},{"Mark"},{"Ricky"},{"Lisa"},{"Jasica"},{"Hanson"},
 22     {"Amy"},{"Bob"},{"Jack"}};
 23     char namedata2[12][10] = {{"May"},{"John"},{"Michael"},{"Andy"},{"Tom"},{"Jane"},{"Yoko"},{"Axel"},{"Alex"},
 24     {"Judy"},{"Kelly"},{"Lucy"}};
 25     srand((unsigned)time(NULL));
 26     for(i = 0;i < 12;i++)
 27     {
 28         data[i][0] = i + 1;
 29         data[i][1] = rand()%50 + 51;
 30     }
 31     head1 = new node;
 32     if(!head1)
 33     {
 34         cout << "Error!!内存地址申请失败!!" << endl;
 35         exit(1);
 36     }
 37     head1->num = data[0][0];
 38     for(j = 0;j < 10; j++)
 39         head1->name[j] = namedata1[0][j];
 40     head1->score = data[0][1];
 41     head1->next = NULL;
 42     ptr = head1;
 43     for(i = 1;i < 12;i++)
 44     {
 45         newnode = new node;
 46         newnode->num = data[i][0];
 47         for(j = 0;j < 10; j++)
 48             newnode->name[j] = namedata1[i][j];
 49         newnode->score = data[i][1];
 50         newnode->next = NULL;
 51         ptr->next = newnode;
 52         ptr = ptr->next;
 53     }
 54     srand((unsigned)time(NULL));
 55     for(i = 0;i < 12;i++)
 56     {
 57         data[i][0] = i + 13;
 58         data[i][1] = rand()%40 + 41;
 59     }
 60     head2 = new node;
 61     if(!head2)
 62     {
 63         cout << "Error!!内存地址申请失败!!" << endl;
 64         exit(1);
 65     }
 66     head2->num = data[0][0];
 67     for(j = 0;j < 10; j++)
 68         head2->name[j] = namedata2[0][j];
 69     head2->score = data[0][1];
 70     head2->next = NULL;
 71     ptr = head2;
 72     for(i = 1;i < 12;i++)
 73     {
 74         newnode = new node;
 75         newnode->num = data[i][0];
 76         for(j = 0;j < 10; j++)
 77             newnode->name[j] = namedata2[i][j];
 78         newnode->score = data[i][1];
 79         newnode->next = NULL;
 80         ptr->next = newnode;
 81         ptr = ptr->next;
 82     }
 83     i = 0;
 84     ptr = concatlist(head1,head2);
 85     cout << "两个链表相连的结果" << endl;
 86     while(ptr != NULL)
 87     {
 88         cout << "[" << ptr->num << " " << ptr->name << " " << ptr->score << " ]\t-> ";
 89         i++;
 90         if(i >= 3)
 91         {
 92             cout << endl;
 93             i = 0;
 94         }
 95         ptr = ptr->next;
 96     }
 97     delete newnode;
 98     delete head2;
 99     system("pause");
100     return 0;
101 }
102 link concatlist(link ptr1,link ptr2)
103 {
104     link ptr;
105     ptr = ptr1;
106     while(ptr->next != NULL)
107         ptr = ptr->next;
108     ptr->next = ptr2;
109     return ptr1;
110 }
View Code

 

转载于:https://www.cnblogs.com/Lited/p/4327675.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值