标签:
单链表的建立
1 #include<iostream> 2 using namespace std; 3 class list 4 { 5 public: 6 int num,score; 7 char name[10]; 8 class list *next; 9 }; 10 typedef class list node; 11 typedef node *link; 12 int main() 13 { 14 link newnode,ptr,delptr; //声明三个表结构指针 15 cout << "请输入5个学生数据: " << endl; 16 delptr = new node; //delptr暂当表头 17 if(!delptr) 18 { 19 cout << "Error!内存地址申请失败!" << endl; 20 exit(1); 21 } 22 cout << "请输入座位号: "; 23 cin >> delptr->num; 24 cout << "请输入姓名: "; 25 cin >> delptr->name; 26 cout << "请输入成绩: "; 27 cin >> delptr->score; 28 ptr = delptr; 29 for(int i = 1;i < 5;i++) 30 { 31 newnode = new node; //建立新结点 32 if(!newnode) 33 { 34 cout << "Error!内存地址申请失败!" << endl; 35 exit(1); 36 } 37 cout << "请输入座位号: "; 38 cin >> newnode->num; 39 cout << "请输入姓名: "; 40 cin >> newnode->name; 41 cout << "请输入成绩: "; 42 cin >> newnode->score; 43 newnode->next = NULL; 44 ptr->next = newnode; //把新结点加在表后面 45 ptr = ptr->next; //让ptr保持在表的最后面 46 } 47 cout << "\n 学生成绩" << endl; 48 cout << "座号\t姓名\t成绩\n=====================" << endl; 49 ptr = delptr; //让ptr回到表头 50 while(ptr!=NULL) 51 { 52 cout << ptr->num << "\t" << ptr->name << "\t" << ptr->score << endl; 53 delptr = ptr; 54 ptr = ptr->next; //ptr依序往后走访 55 delete delptr; //释放内存空间 56 } 57 system("pause"); 58 }
单链表的结点删除
删除单链表的第一个结点
top = head;
head = head->next;
free(top);
删除单链表的中间结点
Y = ptr->next;
ptr->next = Y->next;
free(Y);
删除单链表的最后一个结点
Ptr->next = tail;
Ptr->next = NULL;
free(tail);
现在建立一组学生成绩的链表程序,包含座号、姓名与成绩三种数据。只要输入欲删除的成绩就可以查找链表,并删除该学生的结点。当结束时输入-1,就会列出链表中剩余的所有学生数据。用C++实现!
#include<iostream> #include<iomanip> #include<ctime> #include<cstdlib> using namespace std; class list { public: int num,score; char name[10]; class list *next; }; list del_ptr(list *head,list *ptr); int main() { list *ptr; //声明三个表结构指针 int findword = 0,find,data[12][2]; char namedata[12][10] = {{"Allen"},{"Moko"},{"Lean"},{"Melissa"},{"Angel"},{"Sabrina"},{"Joyce"},{"Jasica"},{"Hanson"}, {"Amy"},{"Bob"},{"Jack"}}; srand((unsigned)time(NULL)); cout << "座号 成绩 座号 成绩 座号 成绩 座号 成绩" << endl; cout << "====================================================" << endl; for(int i = 0;i < 12;i++) { data[i][0] = i+1; data[i][1] = rand()%50+51; } for(int i = 0;i < 3;i++) { for(int j = 0;j < 4;j++) cout << "[" << data[j*3+i][0] << "] [" << data[j*3+i][1] << "] "; cout << endl; } list *head = new list; if(!head) { cout << "Error!内存地址申请失败!" << endl; exit(1); } head->num = data[0][0]; for(int j=0;j < 10;j++) head->name[j] = namedata[0][j]; head->score = data[0][1]; head->next = NULL; ptr = head; for(int i=1;i < 12;i++) { list *newnode = new list; newnode->num = data[i][0]; for(int j = 0;j<10;j++) newnode->name[j] = namedata[i][j]; newnode->score = data[i][1]; newnode->next = NULL; ptr->next = newnode; ptr = ptr->next; } while(1) { cout << "请输入要删除的成绩,结束输入-1: "; cin >> findword; if(findword == -1) break; else { ptr = head; find = 0; while(ptr!= NULL) { if(ptr->score == findword) { *ptr = del_ptr(head,ptr); find++; } ptr = ptr->next; } if(find == 0) cout << "######没有找到######" << endl; } } ptr = head; cout << "\n\t座号\t姓名\t成绩" << endl; cout << "\t================================" << endl; while(ptr != NULL) { cout << "\t[" << ptr->num << "]\t[" << setw(10) << ptr->name << "]\t[" << ptr->score << "]" << endl; ptr = ptr->next; } system("pause"); } list del_ptr(list *head,list *ptr) { list *top; top = head; if(ptr == head) { head = head->next; cout << "已删除第" << ptr->num << "号学生!!姓名" << ptr->name << endl; } else { while(top->next != ptr) top = top->next; if(ptr->next ==NULL) { top->next = NULL; cout << "已删除第" << ptr->num << "号学生!!姓名" << ptr->name << endl; } else { top->next = ptr->next; cout << "已删除第" << ptr->num << "号学生!!姓名" << ptr->name << endl; } } delete []ptr; return *head; }
单链表的结点插入
在前面的学生成绩链表中插入一个学生成绩数据
#include<iostream> #include<iomanip> #include<ctime> #include<cstdlib> using namespace std; class list { public: int num,score; char name[10]; class list *next; }; typedef class list node; typedef node *link; link findnode(link head,int num) { link ptr; ptr = head; while(ptr!=NULL) { if(ptr->num == num) return ptr; ptr = ptr->next; } return ptr; } link insertnode(link head,link ptr,int num,int score,char name[10]) { link InsertNode; InsertNode = new node; if(!InsertNode) return NULL; InsertNode->num = num; InsertNode->score = score; strcpy(InsertNode->name,name); InsertNode->next = NULL; if(ptr == NULL) { InsertNode->next = head; return InsertNode; } else { if(ptr->next == NULL) { ptr->next = InsertNode; } else { InsertNode->next = ptr->next; ptr->next = InsertNode; } } return head; } int main() { link head,ptr,newnode; int new_num,new_score; char new_name[10]; int i,j,position = 0,find,data[12][2]; char namedata[12][10] = {{"Allen"},{"Moko"},{"Lean"},{"Melissa"},{"Angel"},{"Sabrina"},{"Joyce"},{"Jasica"},{"Hanson"}, {"Amy"},{"Bob"},{"Jack"}}; srand((unsigned)time(NULL)); cout << "座号 成绩 座号 成绩 座号 成绩 座号 成绩" << endl; cout << "====================================================" << endl; for(int i = 0;i < 12;i++) { data[i][0] = i+1; data[i][1] = rand()%50+51; } for(int i = 0;i < 3;i++) { for(int j = 0;j < 4;j++) cout << "[" << data[j*3+i][0] << "] [" << data[j*3+i][1] << "] "; cout << endl; } head = new node; if(!head) { cout << "Error!内存地址申请失败!" << endl; exit(1); } head->num = data[0][0]; for(int j=0;j < 10;j++) head->name[j] = namedata[0][j]; head->score = data[0][1]; head->next = NULL; ptr = head; for(int i=1;i < 12;i++) { list *newnode = new list; newnode->num = data[i][0]; for(int j = 0;j<10;j++) newnode->name[j] = namedata[i][j]; newnode->score = data[i][1]; newnode->next = NULL; ptr->next = newnode; ptr = ptr->next; } while(1) { cout << "请输入要插入其后的学生编号,结束输入-1"; cin >> position; if(position == -1) break; else { ptr = findnode(head,position); cout << "请输入新插入的学生编号"; cin >> new_num; cout << "请输入新插入的学生成绩"; cin >> new_score; cout << "请输入新插入的学生姓名"; cin >> new_name; head = insertnode(head,ptr,new_num,new_score,new_name); } } ptr = head; cout << "\n\t座号\t姓名\t成绩" << endl; cout << "\t================================" << endl; while(ptr != NULL) { cout << "\t[" << ptr->num << "]\t[" << ptr->name << "]" << setw(6) << "\t[" << ptr->score << "]" << endl; ptr = ptr->next; } delete head; system("pause"); return 0; }
标签:
原文地址:http://www.cnblogs.com/Lited/p/4324468.html