數(shù)據(jù)結(jié)構(gòu)鏈表的講解及代碼實現(xiàn)!3

#include <iostream>
//鏈表
using namespace std;
typedef struct elemType {
int elemNo;
string elemName;
struct elemType * next;
}elemType,*node;
typedef struct head {
int length;
node next;
}lkListHead;
void initList(lkListHead &head){
cout<<endl<<"初始成功!"<<endl;
head.next = NULL;
head.length = 0;
}
int isPok(lkListHead &head){
cout<<"請輸入要操作的位置:";
int p = 1;
cin>>p;
if(p>=1&&p<=head.length){
return p;
}
cout<<"輸入的位置不合法!";
return -1;
}
void cleanList(lkListHead &head){
while(head.next){
node p = head.next;
head.next = p->next;
if(p){
cout<<"刪除了結(jié)點: "<<p->elemNo<<endl;
delete p;
}
}
head.length = 0;
}
void allList(lkListHead &head) { //遍歷數(shù)據(jù)
node p = head.next;
cout<<endl<<"遍歷結(jié)果如下:"<<endl;
cout<<"elemNo"<<" "<<"elemName"<<endl;
while(p){
cout<<p->elemNo<<" "<<p->elemName<<endl;
p=p->next;
}
p = head.next;
cout<<"當前數(shù)據(jù)個數(shù)為:"<<head.length<<"個"<<endl;
cout<<"head->";
while(p){
cout<<p->elemNo<<"->";
p=p->next;
}
cout<<"^"<<endl;
}
void addList(lkListHead &head, node elem) {//增,頭插法
if(elem){
node p = NULL;
p = head.next;
head.next = elem;
elem->next = p;
head.length++;
head.next->elemNo=head.length;//動態(tài)改變序號
cout<<"添加成功!"<<endl;
}
}
node setAndGetEmem(int elemNo, string elemName) {
node t = new elemType;
if(t == NULL){
cout<<"內(nèi)存分配失??!"<<endl;
return t;
}
t->elemNo = elemNo;
t->elemName = elemName;
return t;
}
void delList(lkListHead &head) {//刪
int pos = isPok(head);
if(pos == - 1){
cout<<"刪除失?。?#34;;
return;
}
else {
if(pos == 1){
node t = head.next;
head.next=t->next;
cout<<"刪除了結(jié)點: "<<t->elemNo<<endl;
delete t;
head.length--;
}
else{
node p = head.next;
for(int i = 1; i<pos-1;i++)
p = p->next;
node t = p->next;
p->next=t->next;
if(t){
cout<<"刪除了結(jié)點: "<<t->elemNo<<endl;
delete t;
head.length--;
}
}
}
}
void changeList(lkListHead &head) {//改
int pos = isPok(head);
if(pos == -1){
cout<<"失??!";
return;
}
else{
node p = head.next;
for(int i = 1; i<pos;i++)
p = p->next;
cout<<"原編號為: "<<p->elemNo<<" 請輸入要修改的編號:"<<endl;
cin>>p->elemNo;
cout<<"原名稱為: "<<p->elemName<<" 請輸入要修改的名稱:"<<endl;
cin>>p->elemName;
cout<<"修改成功!"<<endl;
}
}
node getList(lkListHead &head) {//查
node t;
int pos = isPok(head);
if(pos == -1){
cout<<"失??!";
return t;
}
else{
node p = head.next;
for(int i = 1; i<pos;i++)
p = p->next;
cout<<"查詢成功查詢結(jié)果為:"<<endl;
cout<<"編號為: "<<p->elemNo<<endl;
cout<<"名稱為: "<<p->elemName<<endl;
return p;
}
}
void menu (){
cout<<endl<<"\n1.初始化&清除鏈表\n2.增加數(shù)據(jù)\n3.刪除數(shù)據(jù)\n4.修改數(shù)據(jù)\n5.查詢數(shù)據(jù)\n6.遍歷數(shù)據(jù)\n7.清除數(shù)據(jù)\n8.退出"<<endl;
cout<<"請輸入選項: "<<endl;
}
int main() {
lkListHead head; //聲明頭結(jié)點
while(1){
menu();
int choice = 1;
cin>>choice;
switch(choice){
case 1:initList(head);//初始化
break;
case 2:addList(head, setAndGetEmem(1, "test")); //增
break;
case 3:delList(head); //刪
break;
case 4:changeList(head); //改
break;
case 5:getList(head); //查
break;
case 6:allList(head); //遍歷數(shù)據(jù)
break;
case 7:cleanList(head); //清除數(shù)據(jù)
break;
case 8:cout<<"已退出?。?#34;;return 0;
default: cout<<"輸入有誤!"<<endl;
}
}
return 0;
}