单链表
#include<stdlib.h>
#include<iostream>
using namespace std;
typedef struct LNode{
int data;
struct LNode* next;
}LNode,*LinkList;
const int MAXN = 12;
int A[MAXN];
int N = 12;
void declare(int);
LinkList List_HeadInsert(LinkList& L) {
for (int i = 0; i < N; i++) {
LNode* p = (LNode*)malloc(sizeof(LNode));
p->data = A[i];
p->next = L->next;
L->next = p;
}
return L;
}
LinkList List_TailInsert(LinkList& L) {
LNode* q = L;
for (int i = 0; i < N; i++) {
LNode* p = (LNode*)malloc(sizeof(LNode));
p->data = A[i];
p->next = NULL;
q->next = p;
q = p;
}
return L;
}
LNode* GetElem(LinkList L, int i) {
if (i < 0) return NULL;
if (i == 0) return L;
int j = 1;
LNode* p = L->next;
while (p != NULL && j < i) {
p = p->next;
j++;
}
if (j != i) return NULL;
return p;
}
LNode* LocateElem(LinkList L, int e) {
LNode* p = L->next;
while (p != NULL && p->data != e) p = p->next;
return p;
}
void Insert(LinkList& L,int i,int e) {
LNode* q = GetElem(L, i - 1);
LNode* p = (LNode*)malloc(sizeof(LNode));
p->data = e;
p->next = q->next;
q->next = p;
}
void Delete(LinkList& L, int i) {
LNode* q = GetElem(L, i-1);
LNode* p = q->next;
q->next = p->next;
free(p);
}
int Length(LinkList L) {
int len = 0;
LNode* p = L->next;
while (p) {
p = p->next;
len++;
}
return len;
}
void Print(LinkList L) {
LNode* p = L->next;
while (p != NULL) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int main() {
for (int i = 0; i < N; i++) {
A[i] = i * 100;
}
LNode* head = new LNode;
head->next = NULL;
head = List_TailInsert(head);
Print(head);
Insert(head, 11, 199);
Print(head);
cout << Length(head) << endl;
Delete(head, 11);
Print(head);
cout << Length(head) << endl;
return 0;
}
void declare(int N) {
cout << "插入链表的数组数据为:" << endl;
for (int i = 0; i < N; i++) {
A[i] = rand();
cout << A[i];
if (i != N - 1) cout << " ";
}
cout << endl;
}
双链表
#include<stdlib.h>
#include<iostream>
using namespace std;
typedef struct DNode{
int data;
struct DNode* next;
struct DNode* prior;
}DNode,*DLinkList;
DNode* GetElem(DLinkList L, int i) {
if (i < 0) return NULL;
if (i == 0) return L;
int j = 1;
DNode* p = L->next;
while (p != NULL and j < i) {
p = p->next;
j++;
}
return p;
}
void Insert(DLinkList& L, int i, int e) {
DNode* q = GetElem(L, i - 1);
DNode* p = (DNode*)malloc(sizeof(DNode));
p->next = q->next;
p->prior = q;
p->data = e;
q->next->prior = p;
q->next = p;
}
void Delete(DLinkList& L, int i) {
DNode* q = GetElem(L, i - 1);
DNode* p = q->next;
if (p->next == NULL) {
q->next = NULL;
free(p);
return;
}
q->next = p->next;
p->next->prior = q;
free(p);
}
int N = 12;
int A[12];
DLinkList List_TailInsert(DLinkList& L) {
DNode* q = L;
for (int i = 0; i < N; i++) {
DNode* p = (DNode*)malloc(sizeof(DNode));
p->data = A[i];
p->next = NULL;
p->prior = q;
q->next = p;
q = p;
}
return L;
}
void Print(DLinkList L) {
DNode* p = L->next;
while (p != NULL) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int main() {
for (int i = 0; i < N; i++) {
A[i] = i * 100;
}
DNode* head = new DNode;
head=List_TailInsert(head);
Print(head);
Insert(head, 10, 777);
Print(head);
Delete(head, 10);
Print(head);
return 0;
}