ADT - 单链表(Linked List)

仅供参考,正确性有待检验(QAQ)
//ADT  Linked List
#include<iostream>

#define		TRUE				1
#define		FALSE				0	
#define		OK					1
#define		ERROR				0
#define		OVERFLOW			-1

using namespace std;

typedef int ElemType;
typedef	int Status;

typedef struct LNode {
	ElemType data;	//元素值
	struct LNode *next;//后继元素的地址
}LNode, *LinkList;

//创建单链表
Status ListCreater_L(LinkList &L) {
	LNode *curPtr;
	curPtr = (LNode *)malloc(sizeof(LNode));
	if (!curPtr)exit(OVERFLOW);
	L = curPtr;
	L->next = NULL;
}

//清空单链表
Status ListClear_L(LinkList &L) {
	L->next = NULL;
	return OK;
}

//销毁单链表
Status ListFree_L(LinkList &L) {
	free(L->next);
	L->next = NULL;
	return OK;
}

//单链表输入
Status ListInput_L(LinkList &L) {
	int len, val;
	LNode * reerPtr, *curPtr;
	reerPtr = L;
	cin >> len;
	while (len--) {
		cin >> val;
		curPtr = (LNode *)malloc(sizeof(LNode));
		if (!curPtr)exit(OVERFLOW);
		curPtr->data = val;
		curPtr->next = NULL;
		reerPtr->next = curPtr;
		reerPtr = reerPtr->next;
	}
	return OK;
}

//单链表遍历输出
void ListPrint_L(LinkList &L) {
	LNode *curPtr;
	curPtr = L->next;
	while (curPtr) {
		if (curPtr == L->next) {
			cout << curPtr->data;
		}
		else cout << ' ' << curPtr->data;
		curPtr = curPtr->next;
	}
	cout << endl;
}

//单链表插入元素
Status ListInsert_L(LinkList &L, int pos, ElemType e) {
	if (pos <= 0) return ERROR;
	LNode *curPtr, *reerPtr, *tmpPtr;
	curPtr = L->next;
	reerPtr = L;
	int cnt = 1;
	while (curPtr && cnt < pos) {
		curPtr = curPtr->next;
		reerPtr = reerPtr->next;
		cnt++;
	}
	if (cnt < pos) return ERROR;
	tmpPtr = (LNode *)malloc(sizeof(LNode));
	if (!tmpPtr)exit(OVERFLOW);
	tmpPtr->data = e;
	tmpPtr->next = curPtr;
	reerPtr->next = tmpPtr;
	return OK;
}

//单链表删除元素
Status ListDelete_L(LinkList &L, int pos, ElemType &e) {
	if (pos <= 0) return ERROR;
	LNode *curPtr, *reerPtr;
	curPtr = L->next;
	reerPtr = L;
	int cnt = 1;
	while (curPtr && cnt < pos) {
		curPtr = curPtr->next;
		reerPtr = reerPtr->next;
		cnt++;
	}
	if (cnt < pos || !curPtr) return ERROR;
	e = curPtr->data;
	reerPtr->next = curPtr->next;
	return OK;
}

//单链表改值
Status ListSetVal_L(LinkList &L, int pos, ElemType e) {
	if (pos <= 0) return ERROR;
	LNode *curPtr;
	curPtr = L->next;
	int cnt = 1;
	while (curPtr && cnt < pos) {
		curPtr = curPtr->next;
		cnt++;
	}
	if (cnt < pos || !curPtr)return ERROR;
	curPtr->data = e;
	return OK;
}

//单链表逆序
void ListReverse_L(LinkList &L) {
	LNode *curPtr, *nextPtr;
	curPtr = L->next;
	nextPtr = curPtr->next;
	while (nextPtr) {
		curPtr->next = nextPtr->next;
		nextPtr->next = L->next;
		L->next = nextPtr;
		nextPtr = curPtr->next;
		ListPrint_L(L);
	}
}


线性表元素排序 (<)
//void ListSort_Sq(SqList &L) {
//	int len = L.listLength;
//	int j;
//	for (int i = 2; i <= len; i++) {
//		ElemType t = *(L.elem + i - 1);
//		for (j = i - 1; j >= 1 && *(L.elem + j - 1) > t; j--) {
//			*(L.elem + j) = *(L.elem + j - 1);
//		}
//		*(L.elem + j) = t;
//	}
//}

//单链表锁定元素位置
int ListLocate_L(LinkList &L, ElemType e) {
	LNode *curPtr;
	curPtr = L->next;
	int cur = 1;
	while(curPtr){
		if (curPtr->data == e)
			return cur;
		cur++;
		curPtr = curPtr->next;
	}
	return 0;
}

//判断表空
Status IsEmpty_L(LinkList &L) {
	if (!L->next)
		return TRUE;
	return FALSE;
}

//单链表查找元素个数
int ListCount_L(LinkList &L, ElemType e) {
	int cnt = 0;
	LNode *curPtr;
	curPtr = L->next;
	while(curPtr){
		if (curPtr->data == e)
			cnt++;
		curPtr = curPtr->next;
	}
	return cnt;
}

//返回表长
int ListLength_L(LinkList &L) {
	int len = 0;
	LNode *curPtr = L->next;
	while (curPtr) {
		len++;
		curPtr = curPtr->next;
	}
	return len;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chook_lxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值