[C++]链表

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define error -1
#define OK 1
using namespace std;
typedef int DataType;
class LinkList {
	public:
		LinkList() { //创建链表,有首元结点,链表为空的条件为pHead->next==NULL
			pHead = new ListLinkNode;
			pHead->data = -1;
			pHead->next = NULL;
			len = 0;
		}
		~LinkList() { //摧毁链表
			while (pHead->next != NULL) {
				pTemp = pHead->next;
				delete pHead;
				pHead = pTemp;
			}
			delete pHead;
			pHead = NULL;
			len = 0;
		}
		int create(int n) { //输入n个数据
			if (n <= 0) return error;
			len = n;
			pTemp = pHead; //temp指针开始指向首元结点
			while (n--) { //插入n个数据,循环n次
				pNew = new ListLinkNode; //new指针指向新结点
				pNew->next = NULL; //新结点next指针默认为NULL
				cin >> pNew->data; //输入新结点的数据
				pTemp->next = pNew; //temp指向的结点next指针指向新结点,实现插入数据
				pTemp = pNew; //temp指针指向新结点从而继续插入
			}
			pTemp = pNew = NULL;
			return OK;
		}
		DataType operator [] (int pos) { //像数组一样访问某个数据,数据位置从0开始
			if (pos >= len) return error; //pos大于等于len即可,并不用判断链表指针状态
			DataType Target;
			pTemp = pHead->next; //Temp指向首元结点后的第一个结点
			Target = pTemp->data; //返回值
			while (pos--) {
				pTemp = pTemp->next;
				Target = pTemp->data;
			}
			pTemp = NULL;
			return Target;
		}
		int display() { //遍历输出
			if (pHead->next == NULL) return error;
			pTemp = pHead->next;
			while (pTemp != NULL) {
				cout << pTemp->data << "->";
				pTemp = pTemp->next;
			}
			cout << "NULL" << endl;
			pTemp = NULL;
			return OK;
		}
		void clear() { //清空链表
			while (pHead->next != NULL) {
				pTemp = pHead->next;
				delete pHead;
				pHead = pTemp;
			}
			pHead->next = NULL;
			len = 0;
		}
		int insert(int pos, const DataType x) { //在某个位置的前面插入数据,若该位置没有数据,则插在该位置上面
			pTemp = pHead;
			while (pTemp->next != NULL && pos != 0) { pTemp = pTemp->next; pos--; } //找到结点位置或者超出范围时退出
			if (pos != 0) { pTemp = NULL; return error; } //超出范围了返回error;如果位置[0,1]有数据但2没有数据时,输入2也可以添加元素,但是3就不行
			pNew = new ListLinkNode; //创建新结点
			pNew->data = x; //新结点数据域
			pNew->next = pTemp->next; //插入结点next指针指向原结点next指针所指向结点
			pTemp->next = pNew; //原结点next指针指向新结点
			pTemp = pNew = NULL;
			len++; //链表长度加一
			return OK;
		}
		int erase(int pos) { //删除数据(单个)
			if (pHead->next == NULL) return error; //链表没有数据返回error,和insert有区别
			pTemp = pHead;
			while (pTemp->next != NULL && pos != 0) { pTemp = pTemp->next; pos--; }
			if (pTemp->next == NULL || pos != 0) { pTemp = NULL; return error; } //所找位置超过了链表范围返回error
			pNew = pTemp->next->next;
			delete pTemp->next;
			pTemp->next = pNew;
			pTemp = pNew = NULL;
			len--;
			return OK;
		}
		int erase(int pos1, int pos2) { //删除数据(区间)
			if (pHead->next == NULL) return error;
			pTemp = pHead; //双指针处理
			len -= pos2 - pos1 + 1;
			while (pTemp->next != NULL && pos1 != 0) { pTemp = pTemp->next; pos1--; }
			if (pTemp->next == NULL || pos1 != 0) { pTemp = NULL; return error; }
			pNew = pHead->next;
			while (pNew->next != NULL && pos2 != 0) { pNew = pNew->next; pos2--; }
			if (pos2 != 0) { pNew = NULL; return error; }
			pTr1 = pTemp->next; //释放内存
			pTemp->next = pNew->next;
			while (pTr1 != pNew) {
				pTr2 = pTr1->next;
				delete pTr1;
				pTr1 = pTr2;
			}
			pTemp = pNew = pTr1 = pTr2 = NULL;
			return OK;
		}
		int size() { //返回链表长度,相当于外部访问len的数据
			return len;
		}
		bool empty() { //判断链表是否无数据
			return len == 0 ? 1 : 0;
		}
	private:
		struct ListLinkNode {
			DataType data;
			ListLinkNode* next;
		}*pHead = NULL, *pTemp = NULL, *pNew = NULL, *pTr1 = NULL, *pTr2 = NULL;
		int len;
};
int main(int argc, char** argv) {
	LinkList L;
	for (int i = 0; i < 5; i++)
		L.insert(i, i);
	L.display();
	for (int i = 0; i < 5; i++)
		cout << L[i] << ' ';
	cout << endl;
	L.erase(0, 1);
	for (int i = 0; i < 4; i++)
		cout << L[i] << ' ';
	cout << endl;
	L.clear();
	cout << L.display() << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值