知识框架
单链表
#include <iostream>
using namespace std;
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkedList;
bool InitList2(LinkedList &L){
L = NULL;
return true;
}
bool Empty2(LinkedList L){
return L==NULL;
}
bool InitList(LinkedList &L){
L = (LNode *) malloc(sizeof (LNode));
if (L==NULL){
return false;
}
L->next = NULL;
return true;
}
bool Empty(LinkedList L){
return L->next == NULL;
}
bool InsertNextNode(LNode *p,int e){
if (p == NULL){
return false;
}
LNode *s = (LNode *) malloc(sizeof (LNode));
if (s == NULL){
return false;
}
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
bool InsertPriorNode(LNode *p,int e){
if (p == NULL){
return false;
}
LNode *s = (LNode *) malloc(sizeof (LNode));
if (s == NULL){
return false;
}
s->next = p->next;
p->next = s;
s->data = p->data;
p->data = e;
return true;
}
bool ListInsert(LinkedList &L,int i,int e){
if (i < 1){
return false;
}
LNode *p = L;
int j = 0;
while (p != NULL && j < i-1){
p = p->next;
j++;
}
return InsertNextNode(p,e);
}
bool ListInsert2(LinkedList &L,int i,int e){
if ( i < 1){
return false;
}
if (i == 1){
LNode *s = (LNode *) malloc(sizeof (LNode));
s->data = e;
s->next = L;
L = s;
return true;
}
LNode *p = L;
int j = 1;
while (p != NULL && j < i-1){
p = p->next;
j++;
}
return InsertNextNode(p,e);
}
bool ListDelete(LinkedList &L,int i,int &e){
if (i < 1){
return false;
}
LNode *p = L;
int j = 0;
while (p != NULL && j < i-1){
p = p->next;
j++;
}
if (p == NULL){
return false;
}
if (p->next == NULL){
return false;
}
LNode *q = p->next;
e = q->data;
p->next = q->next;
free(q);
return true;
}
bool ListDelete2(LinkedList &L,int i,int &e){
if (i < 1){
return false;
}
if (i == 1){
LNode *p = L;
L = p->next;
free(p);
}
LNode *p = L;
int j = 1;
while (p != NULL && j < i-1){
p = p->next;
j++;
}
if (p == NULL){
return false;
}
if (p->next == NULL){
return false;
}
LNode *q = p->next;
e = q->data;
p->next = q->next;
free(q);
}
bool DeleteNode(LNode *p){
if (p == NULL){
return false;
}
LNode *q = p->next;
p->data = q->data;
p->next = q->next;
free(q);
}
LNode * GetElem(LinkedList L,int i){
if (i < 1){
return NULL;
}
LNode *p = L;
int j = 0;
while (p != NULL && j < i){
p = p->next;
j++;
}
return p;
}
LNode * LocateElem(LinkedList L,int e){
LNode *p = L->next;
while (p != NULL && p->data != e){
p = p->next;
}
return p;
}
int Length(LinkedList L){
LNode *p = L;
int j = 0;
while (p->next != NULL){
p = p->next;
j++;
}
return j;
}
LinkedList ListTailInsert(LinkedList &L){
int x;
L = (LNode *) malloc(sizeof (LNode));
LNode *r = L;
cin>>x;
LNode *s;
while (x != 9999){
s = (LNode *) malloc(sizeof (LNode));
s->data = x;
r->next = s;
r = s;
cin>>x;
}
r->next = NULL;
return L;
}
LinkedList ListHeadInsert(LinkedList &L){
int x;
L = (LNode *) malloc(sizeof (LNode));
LNode *p = L;
p->next = NULL;
LNode *s;
cin>>x;
while (x != 9999){
s = (LNode *) malloc(sizeof (LNode));
s->data = x;
s->next = p->next;
p->next = s;
cin>>x;
}
return L;
}
LinkedList Reverse(LinkedList &L){
LNode *p = L;
LNode *first = L->next;
LNode *q = L->next->next;
LNode *s;
while (q != NULL){
s = q;
q = q->next;
s->next = p->next;
p->next = s;
}
first->next = NULL;
return L;
}
void ListPrint(LinkedList L){
LNode *p = L->next;
while (p != NULL){
cout << p->data<<" ";
p = p->next;
}
cout<<endl;
}
int main() {
LinkedList L;
ListHeadInsert(L);
ListPrint(L);
Reverse(L);
ListPrint(L);
int length = Length(L);
cout<<"Length: "<<length<<endl;
ListInsert(L,2,10);
ListPrint(L);
int e = 0;
ListDelete(L,3,e);
ListPrint(L);
return 0;
}
双链表
#include <iostream>
using namespace std;
typedef struct DNode{
int data;
struct DNode *prior,*next;
}DNode,*DLinkList;
bool InitDoubleList(DLinkList &L){
L = (DNode *) malloc(sizeof (DNode));
if (L == NULL){
return false;
}
L->prior = NULL;
L->next = NULL;
return true;
}
bool IsEmpty(DLinkList L){
return L->next == NULL;
}
bool InsertNextDNode(DNode *p,DNode *s){
if (p == NULL || s == NULL){
return false;
}
s->next = p->next;
if (p->next != NULL){
p->next->prior = s;
}
s->prior = p;
p->next = s;
return true;
}
bool DeleteNextDNode(DNode *p){
if (p == NULL){
return false;
}
DNode *q = p->next;
if (q == NULL){
return false;
}
p->next = q->next;
if (q->next != NULL){
q->next->prior = p;
}
free(q);
return true;
}
void DestroyList(DLinkList &L){
if (L == NULL){
return;
}
while (L->next != NULL){
DeleteNextDNode(L);
}
free(L);
L = NULL;
}
void TraverseBack(DNode *p){
while (p != NULL){
cout << p->data <<" ";
p = p->next;
}
}
void TraversePrior(DNode *p){
while (p->prior != NULL){
cout << p->data <<" ";
p = p->prior;
}
}
int main() {
DLinkList L;
InitDoubleList(L);
DNode *s;
for (int i = 1; i <= 5; ++i) {
s = (DNode *) malloc(sizeof (DNode));
s->data = i;
InsertNextDNode(L,s);
}
TraverseBack(L->next);
DestroyList(L);
return 0;
}
循环链表
#include <iostream>
using namespace std;
typedef struct LNode {
int data;
struct LNode *next;
}LNode, *LinkList;
bool InitList(LinkList &L){
L = (LNode *) malloc(sizeof (LNode));
if (L == NULL){
return false;
}
L->next = L;
return true;
}
bool IsEmpty(LinkList L){
return L->next == L;
}
bool IsTail(LinkList L,LNode *p){
return p->next == L;
}
typedef struct DNOde{
int data;
struct DNOde *prior,*next;
}DNOde,*LinkedList;
bool InitDLinkList(LinkedList &L){
L = (DNOde *) malloc(sizeof (DNOde));
if (L == NULL){
return false;
}
L->next = L;
L->prior = L;
return true;
}
bool Empty(LinkedList L){
return L->next == L;
}
bool Tail(LinkedList L,DNOde *p){
return p->next == L;
}
int main() {
return 0;
}
静态链表
#include <iostream>
using namespace std;
#define MaxSize 10
typedef struct {
int data;
int next;
}SLinkList[MaxSize];
int main() {
return 0;
}