单链线性表-C++

单链线性表-C++

  1 #include <iostream>
  2 using namespace std;
  3 
  4 typedef int ElemType;
  5 
  6 class LinkList {
  7         int length;
  8         struct Node{
  9                 ElemType data;
 10                 Node* next;
 11         };
 12         Node* StaPtr;
 13         public :
 14                 LinkList(int length = 0, Node* p = NULL): length(length), StaPtr(p){}
 15                 bool ListEmpty(){
 16                         if (StaPtr)
 17                                 return false;
 18                         else
 19                                 return true;
 20                 }
 21                 void ListFree(){
 22                         while (StaPtr)
 23                         {
 24                                 Node* ptr;
 25                                 ptr = StaPtr;
 26                                 StaPtr = StaPtr->next;
 27                                 delete(ptr);
 28                         }
 29                 }
 30                 Node* LocateElem(const ElemType& e)//定位元素e,如果存在就返回该结点地址,否则返回NULL;
 31                 {
 32                         if (StaPtr == NULL)
 33                                 cerr << "Wrong, the list is NULL!!!" << endl;
 34                         while (StaPtr)
 35                         {
 36                                 if (StaPtr->data = e)
 37                                         return StaPtr;
 38                                 else
 39                                         StaPtr = StaPtr->next;
 40                         }
 41                         return NULL;
 42                 }
 43                 bool ListInsert(int i, const ElemType& e)//在第i处插入元素e,插入成功则返回true, 否则返回false;
 44                 {
 45                         //不用length
 46                         Node* ptr = new Node;
 47                         ptr->data = e;
 48                         //cout << "----e----" << endl;
 49 
 50                         //定位指针
 51                         Node* temp = StaPtr;
 52                         //判断是否为空指针;
 53                         if (!temp)
 54                         {
 55                                 StaPtr = ptr;
 56                                 ptr->next = NULL;
 57                                 return true;
 58                         }
 59                         int j = 1;
 60                         //判断是否从头部开始插入
 61                         if (i==1)
 62                         {
 63                                 StaPtr = ptr;
 64                                 ptr->next = temp;
 65                                 length++;
 66                                 return true;
 67                         }
 68                         else
 69                         {
 70                                 while (temp)
 71                                 {
 72                                         if ( j != i - 1 )
 73                                         {
 74                                                 temp = temp->next;
 75                                                 j++;
 76                                         }
 77                                         else
 78                                         {
 79                                                 Node* p = temp->next;
 80                                                 temp->next = ptr;
 81                                                 ptr->next = p;
 82                                                 length++;
 83                                                 return true;
 84                                         }
 85                                 }
 86                                 return false;
 87                         }
 88                 }
 89                 bool ListDelete(int i, ElemType& e)//在第i处删除元素e,删除成功则返回true, 否则返回false;
 90                 {
 91                         //不用length
 92 
 93                         //定位指针
 94                         Node* temp = StaPtr;
 95                         if (!temp)
 96                         {
 97                                 return false;
 98                         }
 99                         int j = 1;
100                         //判断是否是删除头部结点
101                         if (i==1)
102                         {
103                                 Node* p;
104                                 p = temp ;
105                                 StaPtr = p->next;
106                                 e = p->data;
107                                 delete(p);
108                                 length--;
109                                 return true;
110                         }
111                         else
112                         {
113                                 while (temp)
114                                 {
115                                         if ( j != i - 1 )
116                                         {
117                                                 temp = temp->next;
118                                                 j++;
119                                         }
120                                         else
121                                         {
122                                                 Node* p = temp->next;
123                                                 temp->next = p->next;
124                                                 e = p->data;
125                                                 delete(p);
126                                                 length--;
127                                                 return true;
128                                         }
129                                 }
130                                 return false;
131                         }
132                         delete temp;
133                 }
134 
135                 void ListLength(int& len)
136                 {
137                         Node *temp = StaPtr;//统计指针
138                         while (temp)
139                         {
140                                 ++len;
141                                 temp = temp->next;
142                         }
143                         delete temp;
144                 }
145                 void show()
146                 {
147                         Node *temp = StaPtr;//打印指针
148                         while (temp)
149                         {
150                                 cout << "---" ;
151                                 cout << temp->data << "  " ;
152                                 temp = temp->next;
153                         }
154                         delete temp;
155                 }
156 
157 
158 };
159 
160 
161 
162 
163 
164 int main()
165 {
166 
167         LinkList L;
168 
169         cout << boolalpha << L.ListEmpty() << endl;
170         cout << "----" << endl;
171 
172         L.ListInsert(1, 3);
173         L.show();
174         cout << endl;
175 
176         cout << "----" << endl;
177         L.ListInsert(1, 5);
178         L.show();
179         cout << endl;
180 
181         cout << "----" << endl;
182         L.ListInsert(2, 1);
183         L.show();
184         cout << endl;
185 
186         cout << "----" << endl;
187         ElemType e;
188         L.ListDelete(1, e);
189         cout << e << endl;
190         L.show();
191 
192 
193 
194 
195         return 0;
196 }
197 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值