线性表---链式存储(单链表)

本代码是通过建立List类来完成对成员变量和成员函数的定义的,成员变量是要定义结点结构体的。

注意:此实验中要多次使用while语句,一定要仔细检查(就因为FindList函数值的while不小心写成了if语句,从下午四点到现在一直耗在这儿了,血的教训啊)

  1 //链式存储---单链表
  2 #include <iostream>
  3 using namespace std;
  4 //定义线性表类
  5 class List {
  6 private:
  7     //定义单链表存储结构
  8     typedef struct LNode {
  9         int data;
 10         struct LNode *next;
 11     };
 12     struct LNode *head;
 13 public:
 14     //构造函数(初始化)
 15     List() {
 16         head = NULL;
 17     }
 18     //析构函数
 19     ~List() {
 20         LNode *p;
 21         if (head == NULL) return;
 22         p = head;
 23         LNode *q;
 24         while (p != NULL) {
 25             q = p->next;
 26             delete p;
 27             p = q;
 28         }
 29     }
 30     //插入
 31     void insertList(int a,int k);
 32     //删除
 33     void deleteList(int k);
 34     //查找    
 35     //按数字查找
 36     LNode *findList(int a) {
 37         LNode *p;
 38         p = head;
 39         while (p != NULL&&p->data!=a) {
 40             p = p->next;
 41         }
 42         if (p->data == a) return p;
 43         else return NULL;
 44     }
 45     //按序号查找
 46     LNode *FindList(int k) {
 47         //判断序号是否符合条件
 48         LNode *q;
 49         if (k<1 || k>getLength()) {
 50             cout << "查找元素位置不符合条件,查找失败" << endl;
 51             return NULL;
 52         }
 53         int i = 1;
 54         q = head;
 55         while (q != NULL && i < k) {
 56             q = q->next;
 57             i++;
 58         }
 59         if (i == k) return q;
 60         else {
 61             cout << "查找失败" << endl;
 62             return NULL;
 63         }
 64     }
 65     //求线性表长度
 66     int getLength();
 67     //判断表是否为空
 68     bool isEmpty();
 69     //清空线性表
 70     void clearList();
 71 };
 72 void List::insertList(int a,int k) {
 73     //插入位置是否符合条件
 74     int n = getLength();
 75     if (k<1 || k>(n+1)) {
 76         cout << "所输入的插入位置不满足条件,无法进行插入操作" << endl;
 77         return;
 78     }
 79     LNode *s, *p;
 80     s = (LNode*)malloc(sizeof(LNode));
 81     if (k == 1) {
 82         s->data = a;
 83         s->next = head;
 84         head = s;
 85         return;
 86     }
 87     else if (k <= n) {
 88         k = k - 1;
 89         p = FindList(k);
 90         s->data = a;
 91         s->next = p->next;
 92         p->next = s;
 93         return;
 94     }
 95     else{
 96         p = FindList(k-1);
 97         if (p == NULL) {
 98             cout << "插入失败" << endl;
 99             return;
100         }
101         s->data = a;
102         s->next = NULL;
103         p->next = s;
104         return;
105     }
106 }
107 void List::deleteList(int k) {
108     //判断k是否符合条件
109     if (k<1 || k>getLength()) {
110         cout << "所要删除结点位置不符合条件" << endl;
111         return;
112     }
113     LNode *p;
114     if (k == 1) {
115         p = head;
116         head = head->next;
117         free(p);
118         return;
119     }
120     LNode *s;
121     p = FindList(k - 1);
122     s = p->next;
123     p->next = s->next;
124     free(s);
125     return;
126 }
127 int List::getLength() {
128     LNode *p;
129     int i = 0;
130     p = head;
131     while (p != NULL) {
132         p = p->next;
133         i++;
134     }
135     return i;
136 }
137 bool List::isEmpty() {
138     if (getLength() == 0) return true;
139     else
140         return false;
141 }
142 void List::clearList() {
143     //注意要释放每一个节点
144     LNode *p;
145     while (head != NULL) {
146         p = head;
147         head = head->next;
148         free(p);
149     }
150 }
151 
152 int main() {
153     List listA, listB;
154     int i;
155     cout << "开始进行插入操作" << endl;
156     for (i = 1; i <= 10; i++) {
157         listA.insertList(2 * i, i);
158     }
159     cout << "链表A插入操作结束" << endl;
160     for (i = 1; i <= 10; i++) {
161         listB.insertList(2 * i + 1, i);
162     }
163     cout << "链表B插入操作结束" << endl;
164     //依次输出两个线性表的元素
165     cout << "线性表A的元素如下" << endl;
166     for (i = 1; i <=listA.getLength(); i++) cout << listA.FindList(i)->data << "     ";
167     cout << endl;
168     cout << "线性表B的元素如下" << endl;
169     for (i = 1; i <= listB.getLength(); i++) cout << listB.FindList(i)->data << "      ";
170     cout << endl;
171     cout << "请输入要链表A删除的元素位置序号:";
172     int n;
173     cin >> n;
174     listA.deleteList(n);
175     cout << "删除节点后链表A内的元素依次为" << endl;
176     for (i = 1; i <= listA.getLength(); i++) cout << listA.FindList(i)->data << "      ";
177     cout << endl;
178     return 0;
179 }

 

猜你喜欢

转载自www.cnblogs.com/2019-12-10-18ykx/p/12960804.html