首页 > 代码库 > 数据结构 链式表
数据结构 链式表
数据结构课作业之链式表。
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 6 struct Node 7 { 8 char name[30]; 9 char telp[30]; 10 Node * next; 11 }; 12 13 class Stulist 14 { 15 public : 16 Stulist (); 17 ~Stulist (); 18 19 void Creat (int n); //建立n个元素的链式表 20 21 void Insert (Node item, int pos);//按位置插入元素item 22 23 void Find (int pos) const; //按位置查找 24 25 void Delete (int pos); //按位置删除 26 27 void Print () const; //打印所有数据 28 29 void Clear (); //清空链式表 30 private : 31 Node * first; 32 }; 33 34 void Stulist :: Clear () 35 { 36 Node * Pre = first; 37 while (Pre) 38 { 39 memset (Pre->name, 0, sizeof Pre->name); 40 memset (Pre->telp, 0, sizeof Pre->telp); 41 Pre = Pre->next; 42 } 43 } 44 45 void Stulist :: Print () const 46 { 47 Node * Pre = first->next; 48 int cnt=1; 49 cout << "******************************\n"; 50 while (Pre) 51 { 52 cout <<" "<< cnt <<". "<< Pre->name << " " << Pre->telp << endl; 53 Pre = Pre->next; 54 cnt++; 55 } 56 cout << "******************************\n"; 57 } 58 59 void Stulist :: Delete (int pos) 60 { 61 Node * Pre = first; 62 int cnt=0; 63 cout << "请输入要删除的位置: "; cin >> pos; 64 while (Pre && cnt<pos-1) 65 { 66 Pre = Pre->next; 67 cnt++; 68 } 69 if (Pre==NULL) 70 { 71 cout << "试图删除非法位置!\n"; 72 return; 73 } 74 Pre->next = Pre->next->next; 75 cout << "以成功删除第" << pos << " 个数据.\n"; 76 } 77 78 void Stulist :: Find (int pos) const 79 { 80 Node * Pre = first; 81 int cnt=0; 82 cout << "请输入要查找的位置: "; cin >> pos; 83 while (Pre && cnt<pos) 84 { 85 Pre = Pre->next; 86 cnt++; 87 } 88 if (Pre==NULL) 89 { 90 cout << "你要查找的数据不存在,请核对后再查找!\n"; 91 return; 92 } 93 cout << "你要查找的信息详细如下:\n"; 94 cout << "************************\n"; 95 cout << " 姓名:"<< Pre->name <<endl; 96 cout << " 号码:"<< Pre->telp <<endl; 97 cout << "************************\n"; 98 } 99 100 void Stulist::Insert (Node item, int pos)101 {102 Node * Pre=first, * Cur;103 cout << "请输入要插入的位置: "; cin >> pos;104 int cnt=0;105 while (Pre && cnt < pos-1)106 {107 Pre = Pre->next;108 cnt++;109 }110 if (Pre==NULL)111 {112 cout << "试图插入非法位置!\n";113 return;114 }115 cout << "请出入要插入的姓名:"; cin >> item.name;116 cout << "请出入要插入的号码:"; cin >> item.telp;117 Cur = new Node;118 strcpy (Cur->name, item.name);119 strcpy (Cur->telp, item.telp);120 Cur->next = Pre->next;121 Pre->next = Cur;122 cout << "插入成功!\n";123 }124 125 Stulist::Stulist ()126 {127 first = new Node;128 first->next = NULL;129 }130 131 Stulist :: ~Stulist ()132 {133 Node * pre = first;134 while (first)135 {136 pre = first;137 first = pre->next;138 delete pre;139 }140 }141 void Stulist::Creat (int n)142 {143 Node *Pre, *Cur;144 first = new Node;145 Pre = first;146 cout << "请输入要建立表的大小: "; cin >> n;147 for (int i=0; i<n; i++)148 {149 Cur = new Node;150 cout << "请输入第 " << i+1 << " 个数据:"; cin >> Cur->name >> Cur->telp;151 Pre->next = Cur;152 Pre = Cur;153 }154 Pre->next = NULL;155 }156 157 void Meue ()158 {159 cout<<" \n ~~~~~~~~~~~~~~请选择您要执行的操作:~~~~~~~~~~~~~~\n"; 160 cout<<endl<<endl; 161 cout<<" `````````````````````````````````````````````\n"; 162 cout<<" ` 1、清空顺序表 `\n"; 163 cout<<" ` 2、创建顺序表 `\n"; 164 cout<<" ` 3、插入信息 `\n"; 165 cout<<" ` 4、删除信息 `\n"; 166 cout<<" ` 5、查找信息 `\n"; 167 cout<<" ` 6、显示当前信息 `\n"; 168 cout<<" ` 0、退出系统 `\n"; 169 cout<<" `````````````````````````````````````````````\n"; 170 cout<<"\n请从序号0--6中选择:"; 171 }172 173 void Operater (Stulist &Link)174 {175 Meue ();176 int op, pos, n;177 Node item;178 while (1)179 {180 while (cin >> op, (op<0||op>6)) cout << "操作不合法: \n";181 switch (op)182 {183 case 1:184 Link.Clear (); break;185 case 2:186 Link.Creat (n); break;187 case 3:188 Link.Insert (item, pos); break;189 case 4:190 Link.Delete (pos); break;191 case 5:192 Link.Find (pos); break;193 case 6:194 Link.Print (); break;195 default:break;196 }197 }198 cout << "~~~~~~~~~欢迎下次使用~~~~~~~~~\n";199 }200 201 202 //*************** 主程序 **************203 int main ()204 {205 Stulist Link;206 207 Operater (Link);208 209 return 0;210 }
数据结构 链式表
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。