单链表的反转
将学生成绩程序中的学生成绩结点按照座号反向输出
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 }
单链表的连接
将学生成绩程序中的学生成绩与新的学生成绩表连接起来
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 }
时间: 2024-12-19 15:11:06