73.链表的基本操作

  1 #include <iostream>
  2 #include <cstring>
  3 #include <string>
  4 using namespace std;
  5 int n;
  6 struct LinkList
  7 {
  8     int value;//数据域
  9     struct LinkList *next;//指针域
 10 }*head;
 11 
 12 void init()
 13 {
 14     cout<<"                           线性链表"<<endl;
 15     cout<<endl;
 16     cout<<"                      (1)设置线性链表中的元素"<<endl;
 17     cout<<endl;
 18     cout<<"                      (2)查找线性链表中的元素"<<endl;
 19     cout<<endl;
 20     cout<<"                      (3)删除线性链表中的元素"<<endl;
 21     cout<<endl;
 22     cout<<"                      (4)统计线性链表中元素的总数"<<endl;
 23 }
 24 
 25 void Add()
 26 {
 27     cout<<endl;
 28     int addNum,addvalue;//元素的个数及数值
 29     cout<<"                      请输入将要设置链表元素的个数:"<<endl;
 30     cout<<endl;
 31     cout<<"                      ";
 32     cin>>addNum;
 33     cout<<endl;
 34     cout<<"                      请依次输入将要设置的元素的值:"<<endl;
 35     cout<<endl;
 36     head=new LinkList;
 37     head->next=NULL;
 38     struct LinkList *q,*p;
 39     p=head;
 40     cout<<"                      ";
 41     while(addNum--){
 42         q=new LinkList;
 43         q->next=NULL;
 44         cin>>addvalue;
 45         p->value=addvalue;
 46         p->next=q;
 47         p=q;
 48     }
 49     cout<<endl;
 50     cout<<"                      输入完毕 !!!"<<endl;
 51     cout<<endl;
 52 }
 53 
 54 void Delete()
 55 {
 56     cout<<endl;
 57     struct LinkList *k,*v;
 58     k=head;
 59     cout<<"                      请输入要删除的元素"<<endl;
 60     cout<<endl;
 61     int deletevalue;
 62     cout<<"                      ";
 63     cin>>deletevalue;
 64     cout<<endl;
 65     while(k->next){
 66         if(k->next->value==deletevalue){
 67             v=k->next;
 68             k->next=v->next;
 69             delete v;
 70         };
 71         k=k->next;
 72     }
 73     cout<<endl;
 74     cout<<"                      删除完毕 !!!"<<endl;
 75     cout<<endl;
 76 }
 77 
 78 void Find()
 79 {
 80     cout<<endl;
 81     struct LinkList *k;
 82     k=head;
 83     cout<<"                      请输入要查找的元素:"<<endl;
 84     cout<<endl;
 85     int Findvalue;
 86     cout<<"                      ";
 87     cin>>Findvalue;
 88     cout<<endl;
 89     int flag=0;
 90     int i=1;
 91     while(k->next!=NULL){
 92         if(k->value==Findvalue){
 93             flag=1;
 94             break;
 95         }
 96         k=k->next;
 97         i++;
 98     }
 99     if(flag){
100         cout<<"                      要查找的元素找到了!!!它在第"<<i<<"个位置"<<endl;
101     }else{
102         cout<<"                      所查找的值不存在!!!"<<endl;
103     }
104     cout<<endl;
105 
106 }
107 
108 void Count()
109 {
110     cout<<endl;
111     struct LinkList *k;
112     k=head;
113     int sum=0;
114     while(k->next!=NULL){
115         sum++;
116         k=k->next;
117     }
118     cout<<"                      线性链表元素的总数为:"<<sum<<""<<endl;
119     cout<<endl;
120 }
121 
122 void Put()
123 {
124     struct LinkList *k;
125     k=head;
126     cout<<"线性链表中元素依次为:"<<endl;
127     cout<<endl;
128     cout<<"                      ";
129     while(k->next!=NULL){
130         cout<<k->value<<" ";
131         k=k->next;
132     }
133     cout<<endl;
134     cout<<endl;
135 }
136 
137 int main()
138 {
139     init();
140     while(1){
141         cout<<endl;
142         cout<<"                      请按下功能键 ..."<<endl;
143         cout<<endl;
144         cout<<"                      ";
145         cin>>n;
146         if(n==1) Add();
147         if(n==2) Find();
148         if(n==3) {
149             cout<<endl;
150             cout<<"                      删除操作前";
151             Put();
152             Delete();
153             cout<<"                      删除操作后";
154             Put();
155         }
156         if(n==4) Count();
157         if(n<1||n>4){
158             cout<<endl;
159             cout<<"                      功能键不存在请重新输入!!!"<<endl;
160             cout<<endl;
161         }
162         cout<<"                      -----------------------"<<endl;
163         cout<<endl;
164         init();
165     }
166     return 0;
167 }
posted @ 2018-01-23 17:13  wydxry  阅读(366)  评论(0编辑  收藏  举报
Live2D