LinkList.h
#pragma
typedef int ElemType;
typedef struct _Node
{
ElemType data;
struct _Node* next;
}Node, * NodePtr;
void InitList(NodePtr head);
void DestroyList(NodePtr head, ElemType * data);
bool IsListEmpty(NodePtr head);
int ListLength(NodePtr head);
bool GetElem(NodePtr head, int pos,ElemType* data);
int LocateElem(NodePtr head, ElemType data);
Node* GetPosPrior(NodePtr head, int pos);
Node* GetPosNext(NodePtr head, int pos);
void Show(NodePtr head);
bool ListInsert(NodePtr head, int pos, ElemType data);
void ListInsertHead(NodePtr head, ElemType data);
void ListInsertTail(NodePtr head, ElemType data);
bool ListDeletePos(NodePtr head, int pos, ElemType* data);
void ListDeleteHead(NodePtr head, ElemType* data);
void ListDeleteTail(NodePtr head, ElemType* data);
void ListDeleteData(NodePtr head, ElemType data);
LinkLlist.cpp
#include "LinkList.h"
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <assert.h>
#define ERROR_JUDGEMENT assert(NULL != head);\
if (NULL == head)\
{\
printf("%s %d error\n", __FUNCTION__, __LINE__);\
exit(-1);\
}
void InitList(NodePtr head)
{
ERROR_JUDGEMENT
head->next = NULL;
head->data = 0;
}
void DestroyList(NodePtr head, ElemType* data)
{
ERROR_JUDGEMENT
while (head->next)
ListDeleteHead(head,data);
}
bool IsListEmpty(NodePtr head)
{
ERROR_JUDGEMENT
return head->next == NULL ? true : false;
}
int ListLength(NodePtr head)
{
ERROR_JUDGEMENT
return head->data;
}
bool GetElem(NodePtr head, int pos,ElemType *data)
{
ERROR_JUDGEMENT
Node* tmp = head->next;
int j = 1;
while (tmp && j < pos)
{
tmp = tmp->next;
j++;
}
if (!tmp || j > pos)
return false;
else
{
*data = tmp->data;
return true;
}
}
int LocateElem(NodePtr head, ElemType data)
{
ERROR_JUDGEMENT
Node* tmp = head -> next;
unsigned count = 0;
while (tmp)
{
count++;
if (data == tmp->data)
return count;
else
{
tmp = tmp->next;
}
}
return 0;
}
Node* GetPosPrior(NodePtr head, int pos)
{
ERROR_JUDGEMENT
if (pos<1 || pos>head->data)
{
printf("pos error");
exit(0);
}
Node* tmp =head;
while (tmp && pos > 1)
{
tmp = tmp->next;
pos--;
}
return tmp;
}
Node* GetPosNext(NodePtr head, int pos)
{
ERROR_JUDGEMENT
if (pos<1 || pos>head->data)
{
printf("pos error");
exit(0);
}
Node* tmp = head;
while (tmp && pos > 0)
{
tmp = tmp->next;
pos--;
}
return tmp->next;
}
void Show(NodePtr head)
{
ERROR_JUDGEMENT
Node* tmp = head->next;
while (tmp)
{
printf("%d\t", tmp->data);
tmp = tmp->next;
}
printf("\n");
}
bool ListInsert(NodePtr head, int pos, ElemType data)
{
ERROR_JUDGEMENT
if (pos<1 || pos>head->data + 1)
{
printf("pos error");
exit(0);
}
NodePtr p = head;
int j = 0;
while (p && j < pos - 1)
{
p = p->next;
++j;
}
if (p && j > pos - 1)
{
return false;
}
NodePtr tmp = (NodePtr)malloc(sizeof(Node));
tmp->data = data;
tmp->next = p->next;
p->next = tmp;
head->data++;
return true;
}
void ListInsertHead(NodePtr head, ElemType data)
{
ERROR_JUDGEMENT
ListInsert(head, 1, data);
}
void ListInsertTail(NodePtr head, ElemType data)
{
ERROR_JUDGEMENT
ListInsert(head, head->data + 1, data);
}
bool ListDeletePos(NodePtr head, int pos,ElemType * data)
{
ERROR_JUDGEMENT
if (pos<1 || pos>head->data)
exit(0);
NodePtr p = head, q;
int j = 0;
while (p->next && j < pos - 1)
{
p = p->next;
++j;
}
if (!(p->next) || j > pos - 1)
return false;
q = p->next;
p->next = q->next;
*data = q->data;
free(q);
head->data--;
return true;
}
void ListDeleteHead(NodePtr head,ElemType *data)
{
ERROR_JUDGEMENT
ListDeletePos(head, 1, data);
}
void ListDeleteTail(NodePtr head, ElemType* data)
{
ERROR_JUDGEMENT
ListDeletePos(head, head->data,data);
}
void ListDeleteData(NodePtr head, ElemType data)
{
ERROR_JUDGEMENT
Node* tmp = head->next;
int count = 1;
while (tmp)
{
if (tmp->data == data)
{
Node* PosPrior = GetPosPrior(head, count);
Node* PosNode = PosPrior->next;
PosPrior->next = PosNode->next;
free(PosNode);
return;
}
else
{
tmp = tmp->next;
count++;
}
}
}
main.cpp
#include "LinkList.h"
#include <stdio.h>
int main()
{
Node Head;
ElemType data = 0;
int pos = 1;
InitList(&Head);
for (int i = 1; i <= 10; i++)
{
ListInsertHead(&Head, i * 10);
}
Show(&Head);
data = 999;
pos = 4;
printf("在單鏈表的第%d位置插入數據:%d\n",pos, data);
ListInsert(&Head, pos, data);
Show(&Head);
data = 666;
printf("在單鏈表的尾部插入數據%d:\n", data);
ListInsertTail(&Head,data);
Show(&Head);
data = 119;
printf("在單鏈表的頭部插入數據%d:\n", data);
ListInsertHead(&Head, data);
Show(&Head);
pos = 4;
printf("刪除單鏈表的%d位置元素:\n",pos);
ListDeletePos(&Head, pos, &data);
Show(&Head);
printf("刪除單鏈表頭部位置的元素:\n");
ListDeleteHead(&Head, &data);
Show(&Head);
printf("刪除單鏈表頭尾位置的元素:\n");
ListDeleteTail(&Head, &data);
Show(&Head);
printf("獲得單鏈表長度:\n");
printf("%d\n", ListLength(&Head));
pos = 3;
printf("%d\n", GetElem(&Head, pos, &data));
printf("獲得單鏈表的第%d個位置的數據元素爲%d\n",pos,data);
data = 70;
printf("數據元素%d所在的位置爲%d\n", data, LocateElem(&Head, data));
pos = 7;
printf("獲得第%d個數據元素的前驅結點數據:\n",pos);
Node* PosPrior = GetPosPrior(&Head, pos);
printf("pos %d prior node data : %d\n", pos, (*PosPrior).data);
pos = 7;
printf("獲得第%d個數據元素的後繼結點數據:\n", pos);
PosPrior = GetPosNext(&Head, pos);
printf("pos %d next node data : %d\n", pos, (*PosPrior).data);
if (IsListEmpty(&Head))
printf("單鏈表爲空\n");
else
printf("單鏈表不爲空\n");
printf("銷燬單鏈表\n");
DestroyList(&Head, &data);
if (!Head.next)
printf("單鏈表銷燬成功");
return 0;
}
測試運行結果