参考王道考研视频
#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct LNode {
int data;
struct LNode* next;
}LNode, *LinkList;
LinkList List_HeadInsert(LinkList &L) {
LNode* s;
int x;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
scanf_s("%d", &x);
while (x != -1) {
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
scanf_s("%d", &x);
}
return L;
}
LinkList List_TailInsert(LinkList &L) {
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode *s, *r = L;
scanf_s("%d", &x);
while (x != -1) {
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf_s("%d", &x);
}
r->next = NULL;
return L;
}
LNode* GetElem(LinkList L, int i) {
if (i < 0) return NULL;
LNode* p = L;
int j = 0;
while (p != NULL && j < i) {
p = p->next;
j++;
}
return p;
}
LNode* LocateElem(LinkList L, int e) {
LNode* p = L->next;
while (p->data != e && p != NULL) p = p->next;
return p;
}
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->data = p->data;
s->next = p->next;
p->next = s;
p->data = e;
return true;
}
bool DeleteNode(LNode* p) {
if (p == NULL) return false;
LNode* q = p->next;
p->data = q->data;
p->next = q->next;
free(q);
return true;
}
bool DeleteNextNode(LNode* p, int &e) {
if (p == NULL || p->next == NULL) return false;
LNode* q = p->next;
e = q->data;
p->next = q->next;
free(q);
return true;
}
bool ListInsert(LinkList &L, int i, int e) {
LNode* p = GetElem(L, i);
return InsertNextNode(p, e);
}
int ListDelete(LinkList &L, int i, int &e) {
LNode* p = GetElem(L, i);
return DeleteNextNode(p, e);
}
void DestoryList(LinkList &L) {
LNode *p = L;
while (p != NULL){
LNode* r = p;
p = p->next;
free(r);
}
}
int main() {
return 0;
}
|