1.带头节点的单链表
list头文件
#pragma once
typedef struct Node
{
int data;
struct Node* next;
}Node, * List;
void InitList(List plist);
bool Insert_head(List plist, int val);
bool Insert_tail(List plist, int val);
bool Insert(List plist, int pos, int val);
bool IsEmpty(List plist);
int GetLength(List plist);
Node* Search(List plist, int key);
bool DelPos(List plist, int pos);
bool DelVal(List plist, int val);
Node* GetPrio(List plist, int key);
Node* GetNext(List plist, int key);
void Show(List plist);
void Clear(List plist);
void Destroy(List plist);
list.cpp文件
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "list.h"
void InitList(List plist)
{
assert(plist != NULL);
if (plist == NULL)
return;
plist->next = NULL;
}
bool Insert_head(List plist, int val)
{
Node* p = (Node*)malloc(sizeof(Node));
assert(p != NULL);
p->data = val;
p->next = plist->next;
plist->next = p;
return true;
}
bool Insert_tail(List plist, int val)
{
assert(plist != NULL);
if (plist == NULL)
return false;
Node* p = (Node*)malloc(sizeof(Node));
p->data = val;
Node* q;
for (q = plist; q->next != NULL; q = q->next)
{
;
}
p->next = q->next;
q->next = p;
return true;
}
bool Insert(List plist, int pos, int val)
{
if (pos<0 || pos>GetLength(plist))
{
return false;
}
Node* p = (Node*)malloc(sizeof(Node));
p->data = val;
Node* q;
int i;
for (q = plist, i = 0; i < pos; i++, q = q->next);
p->next = q->next;
q->next = p;
return true;
}
bool IsEmpty(List plist)
{
return plist->next == NULL;
}
int GetLength(List plist)
{
int count = 0;
for (Node* p = plist->next; p != NULL; p = p->next)
{
count++;
}
return count;
}
Node* Search(List plist, int key)
{
for (Node* p = plist->next; p != NULL; p = p->next)
{
if (p->data == key)
return p;
}
return NULL;
}
bool DelPos(List plist, int pos)
{
if (pos < 0 || pos >= GetLength(plist))
{
return false;
}
Node* p;
int i;
for (p = plist, i = 0; i < pos; i++, p = p->next);
Node* q = p->next;
p->next = q->next;
free(q);
return true;
}
bool DelVal(List plist, int val)
{
Node* p = GetPrio(plist, val);
if (p == NULL)
return false;
Node* q = p->next;
p->next = q->next;
free(q);
return true;
}
Node* GetPrio(List plist, int key)
{
for (Node* p = plist; p->next != NULL; p = p->next)
{
if (p->next->data == key)
return p;
}
return NULL;
}
Node* GetNext(List plist, int key)
{
Node* p = Search(plist, key);
if (p == NULL)
return NULL;
return p->next;
}
void Show(List plist)
{
for (Node* p = plist->next; p != NULL; p = p->next)
{
printf("%d ", p->data);
}
printf("\n");
}
void Clear(List plist)
{
Destroy(plist);
}
void Destroy(List plist)
{
Node* p;
while (plist->next != NULL)
{
p = plist->next;
plist->next = p->next;
free(p);
}
}
void Destroy1(List plist)
{
if (plist == NULL || plist->next == NULL)
return;
Node* p = plist->next;
Node* q;
plist->next = NULL;
while (p != NULL)
{
q = p->next;
free(p);
p = q;
}
}
2.带头结点的双向链表
dlist头文件
#pragma once
typedef struct DNode
{
int data;
struct DNode* next;
struct DNode* prio;
}DNode, * DList;
void InitList(DList plist);
bool Insert_head(DList plist, int val);
bool Insert_tail(DList plist, int val);
bool Insert(DList plist, int pos, int val);
bool IsEmpty(DList plist);
int GetLength(DList plist);
DNode* Search(DList plist, int key);
bool DelPos(DList plist, int pos);
bool DelVal(DList plist, int val);
DNode* GetPrio(DList plist, int key);
DNode* GetNext(DList plist, int key);
void Show(DList plist);
void Clear(DList plist);
void Destroy(DList plist);
dlist.cpp文件
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "dlist.h"
void InitList(DList plist)
{
assert(plist != NULL);
if (plist == NULL)
return;
plist->next = NULL;
plist->prio = NULL;
}
static DNode* BuyNode(int val)
{
DNode* p = (DNode*)malloc(sizeof(DNode));
p->data = val;
return p;
}
bool Insert_head(DList plist, int val)
{
DNode* p = (DNode*)malloc(sizeof(DNode));
p->data = val;
p->next = plist->next;
plist->next = p;
p->prio = plist;
if (p->next != NULL)
{
p->next->prio = p;
}
return true;
}
bool Insert_tail(DList plist, int val)
{
DNode* p = (DNode*)malloc(sizeof(DNode));
p->data = val;
DNode* q = plist;
for(;q->next != NULL;q = q->next)
{
;
}
p->next = q->next;
q->next = p;
p->prio = q;
return true;
}
bool Insert(DList plist, int pos, int val)
{
if (pos < 0 || pos > GetLength(plist))
return false;
DNode* q = plist;
for (int i = 0; i < pos; i++)
{
q = q->next;
}
DNode* p = (DNode*)malloc(sizeof(DNode));
p->data = val;
p->next = q->next;
q->next = p;
p->prio = q;
if (p->next != NULL)
{
p->next->prio = p;
}
return true;
}
bool IsEmpty(DList plist)
{
return plist->next == NULL;
}
int GetLength(DList plist)
{
int count = 0;
for (DNode* p = plist->next; p != NULL; p = p->next)
{
count++;
}
return count;
}
DNode* Search(DList plist, int key)
{
for (DNode* p = plist->next; p != NULL; p = p->next)
{
if (p->data == key)
return p;
}
return NULL;
}
bool DelPos(DList plist, int pos)
{
if (pos < 0 || pos > GetLength(plist))
return false;
DNode* q = plist->next;
for (int i = 0; i < pos; i++)
{
q = q->next;
}
q->prio->next = q->next;
if (q->next != NULL)
{
q->next->prio = q->prio;
}
free(q);
return true;
}
bool DelVal(DList plist, int val)
{
DNode* p = Search(plist, val);
if (p == NULL)
return false;
p->prio->next = p->next;
if (p->next != NULL)
{
p->next->prio = p->prio;
}
free(p);
return true;
}
DNode* GetPrio(DList plist, int key)
{
DNode* p = Search(plist, key);
return p == NULL ? NULL : p->prio;
}
DNode* GetNext(DList plist, int key)
{
DNode* p = Search(plist, key);
return p == NULL ? NULL : p->next;
}
void Show(DList plist)
{
for (DNode* p = plist->next; p != NULL; p = p->next)
{
printf("%d ", p->data);
}
printf("\n");
}
void Clear(DList plist)
{
Destroy(plist);
}
void Destroy(DList plist)
{
while (plist->next != NULL)
{
DelPos(plist, 0);
}
}
3.带头结点的循环链表
clist头文件
#pragma once
typedef struct CNode
{
int data;
struct CNode* next;
}CNode, * CList;
void InitList(CList plist);
bool Insert_head(CList plist, int val);
bool Insert_tail(CList plist, int val);
bool Insert(CList plist, int pos, int val);
bool IsEmpty(CList plist);
int GetLength(CList plist);
CNode* Search(CList plist, int key);
bool DelPos(CList plist, int pos);
bool DelVal(CList plist, int val);
CNode* GetPrio(CList plist, int key);
CNode* GetNext(CList plist, int key);
void Show(CList plist);
void Clear(CList plist);
void Destroy(CList plist);
clist.cpp文件
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "clist.h"
void InitList(CList plist)
{
assert(plist != NULL);
if (plist == NULL)
return;
plist->next = plist;
}
bool Insert_head(CList plist, int val)
{
CNode* p = (CNode*)malloc(sizeof(CNode));
assert(p != NULL);
if (p == NULL)
return false;
p->data = val;
p->next = plist->next;
plist->next = p;
return true;
}
bool Insert_tail(CList plist, int val)
{
CNode* p = (CNode*)malloc(sizeof(CNode));
assert(p != NULL);
if (p == NULL)
return false;
p->data = val;
CNode* q;
for (q = plist; q->next != plist; q = q->next);
p->next = q->next;
q->next = p;
return true;
}
bool Insert(CList plist, int pos, int val)
{
if (pos < 0 || pos > GetLength(plist))
return false;
CNode* q = plist;
for (int i = 0; i < pos; i++)
{
q = q->next;
}
CNode* p = (CNode *)malloc(sizeof(CNode));
p->data = val;
p->next = q->next;
q->next = p;
return true;
}
bool IsEmpty(CList plist)
{
return plist->next == plist;
}
int GetLength(CList plist)
{
int count = 0;
for (CNode* p = plist->next; p != plist; p = p->next)
{
count++;
}
return count;
}
CNode* Search(CList plist, int key)
{
assert(plist != NULL);
for (CNode* q = plist->next; q->next != plist;q = q->next)
{
if (q->data == key)
return q;
}
return NULL;
}
bool DelPos(CList plist, int pos)
{
if (pos < 0 || pos > GetLength(plist))
return false;
CNode* q = plist;
for (int i = 0; i < pos - 1; i++)
{
q = q->next;
}
CNode* p = q->next;
q->next = p->next;
free(p);
return true;
}
bool DelVal(CList plist, int val)
{
CNode* p = GetPrio(plist, val);
if (p == NULL)
return false;
CNode* q = p->next;
p->next = q->next;
free(q);
return true;
}
CNode* GetPrio(CList plist, int key)
{
assert(plist != NULL);
for (CNode* q = plist->next; q->next != plist; q = q->next)
{
if (q->next->data == key)
return q;
}
return NULL;
}
CNode* GetNext(CList plist, int key)
{
assert(plist != NULL);
for (CNode* q = plist->next; q->next != plist; q = q->next)
{
if (q->data == key)
return q->next;
}
return NULL;
}
void Show(CList plist)
{
for (CNode* p = plist->next; p != plist; p = p->next)
{
printf("%d ", p->data);
}
printf("\n");
}
void Clear(CList plist)
{
Destroy(plist);
}
void Destroy(CList plist)
{
CNode* p;
while (plist->next != plist)
{
p = plist->next;
plist->next = p->next;
free(p);
}
}