仅供参考,正确性有待检验(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;
}