单链表

  1 #include <iostream>                  //引入输入输出流
  2 using namespace std;
  3 
  4 struct Node
  5 {
  6   int data;               //数据域
  7   Node *next;       //指针域
  8 };
  9 
 10 class LinkList
 11 {
 12 public:
 13   LinkList();                      //无参构造函数,建立只有头结点的空链表
 14   LinkList(int a[], int n);       //有参构造函数,建立有n个元素的单链表
 15   ~LinkList();                     //析构函数
 16   int Length();                     //求单链表的长度
 17   int Get(int i);               //按位查找。查找第i个结点的元素值
 18   int Locate(int x);            //按值查找。查找值为x的元素序号
 19   void Insert(int i, int x);       //插入操作,第i个位置插入值为x的结点
 20   int Delete(int i);            //删除操作,删除第i个结点
 21   void PrintList();                  //遍历操作,按序号依次输出各元素
 22 private:
 23   Node *first;           //单链表的头指针
 24 };
 25 
 26 LinkList::LinkList()
 27 {
 28   first = new Node;              //生成头结点
 29   first->next = NULL;                      //头结点的指针域置空
 30 }
 31 
 32 LinkList :: ~LinkList()
 33 {
 34   Node *q = NULL;
 35   while (first != NULL)        //释放单链表的每一个结点的存储空间
 36   {
 37     q = first;                 //暂存被释放结点
 38     first = first->next;         // first指向被释放结点的下一个结点
 39     delete q;
 40   }
 41 }
 42 
 43 void LinkList::PrintList()
 44 {
 45   Node *p = first->next;                //工作指针p初始化
 46   while (p != NULL)
 47   {
 48     cout << p->data << "\t";
 49     p = p->next;                 //工作指针p后移,注意不能写作p++
 50   }
 51 }
 52 
 53 int LinkList::Length()
 54 {
 55   Node *p = first->next;   //工作指针p初始化为开始接点
 56   int count = 0;                    //累加器count初始化
 57   while (p != NULL)
 58   {
 59     p = p->next;
 60     count++;
 61   }
 62   return count;              //注意count的初始化和返回值之间的关系
 63 }
 64 
 65 int LinkList::Get(int i)
 66 {
 67   Node *p = first->next;    //工作指针p初始化
 68   int count = 1;                     //累加器count初始化
 69   while (p != NULL && count < i)
 70   {
 71     p = p->next;                   //工作指针p后移
 72     count++;
 73   }
 74   if (p == NULL) cout<< "位置异常";
 75   else return p->data;
 76 }
 77 
 78 int LinkList::Locate(int x)
 79 {
 80   Node *p = first->next;   //工作指针p初始化
 81   int count = 1;                     //累加器count初始化
 82   while (p != NULL)
 83   {
 84     if (p->data == x) return count;     //查找成功,结束函数并返回序号
 85     p = p->next;
 86     count++;
 87   }
 88   return 0;                        //退出循环表明查找失败
 89 }
 90 
 91 void LinkList::Insert(int i, int x)
 92 {
 93   Node *p = first, *s = NULL;        //工作指针p初始化
 94   int count = 0;
 95   while (p != NULL && count < i - 1)            //查找第i – 1个结点
 96   {
 97     p = p->next;                              //工作指针p后移
 98     count++;
 99   }
100   if (p == NULL) cout<<"位置异常";        //没有找到第i – 1个结点
101   else {
102     s = new Node; s->data = x;      //申请结点s,数据域为x
103     s->next = p->next; p->next = s;     //将结点s插入到结点p之后
104   }
105 }
106 
107 // 头插法构造 
108 //LinkList:: LinkList(int a[ ], int n)
109 //{
110 //  first = new Node; first->next = NULL;     //初始化一个空链表
111 //  for (int i = 0; i < n; i++)
112 //  { 
113 //    Node *s;
114 //    s = new Node; s->data = a[i];    
115 //     s->next = first->next; first->next = s;    //将结点s插入到头结点之后
116 //  }
117 //}
118 
119 LinkList::LinkList(int a[], int n)
120 {
121   first = new Node;                    //生成头结点
122   Node  *r = first, *s = NULL;           //尾指针初始化
123   for (int i = 0; i < n; i++)
124   {
125     s = new Node; s->data = a[i];
126     r->next = s; r = r->next; //r=s;                //将结点s插入到终端结点之后
127   }
128   r->next = NULL;        //单链表建立完毕,将终端结点的指针域置空
129 }
130 int LinkList::Delete(int i)
131 {
132   int x;
133   Node *p = first, *q = NULL;        //工作指针p指向头结点
134   int count = 0;
135   while (p != NULL && count < i - 1)           //查找第i-1个结点
136   {
137     p = p->next;
138     count++;
139   }
140   if (p == NULL || p->next == NULL)  //结点p不存在或p的后继结点不存在
141     cout<< "位置异常";
142   else {
143     q = p->next; x = q->data;         //暂存被删结点
144     p->next = q->next;              //摘链
145     delete q;
146     return x;
147   }
148 }
149 
150 int main()
151 {
152   int r[5] = { 2, 5, 7, 1, 9 }, i, x,n,m;
153   LinkList L(r, 5);
154   cout << "当前线性表的数据为:"<<endl;
155   L.PrintList();                        
156   cout << endl;
157   cout << "当前单链表的长度为:" << endl;
158   cout<<L.Length() << endl;      
159   cout << "请输入要查找元素的位置:" << endl;
160   cin >> i;
161   x = L.Get(i);
162   cout << "" << i << "位置的元素为" << x<<endl;
163   cout << "请输入在第几个位置插入几:" << endl;
164   cin >> i >> x;
165     L.Insert(i, x);                         
166     cout << "执行插入操作后数据为:"<<endl;
167     L.PrintList();                        
168     cout << endl;
169   cout << "请输入查找的元素值:"<<endl;
170   cin >> x;
171   i = L.Locate(x);
172   if (1 <= i) 
173       cout << "元素" << x << "的元素位置为:" << i << endl;
174   else 
175       cout << "单链表中没有元素" << x << endl;
176   
177     cout << "请输入要删除第几个元素:"<<endl;
178     cin >> i;
179     x = L.Delete(i);                       //删除第i个元素
180     cout << "删除的元素值是" << x << "执行删除操作后数据为:"<<endl;
181     L.PrintList();                        //输出删除后链表
182   return 0;
183 }

 

posted @ 2020-11-21 00:31  丁帅帅dss  阅读(116)  评论(0)    收藏  举报