#include<stdio.h>
#include<stdlib.h>
#define MaxSize 100
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode * next;
}LNode ,*LinkeList ;
bool InitLIst(LinkeList& L) {
L = (LNode*)malloc(sizeof(LNode));
if (L == NULL) return false;
L->next = NULL;
return true;
}
bool HeadInsert(LinkeList& L) {
int x;
scanf("%d", &x);
while (x != 9999) {
LNode* s = (LNode*)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
scanf("%d", &x);
}
return true;
}
bool ListpreInsert(LNode * p,ElemType 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(LinkeList &L, int i, ElemType e) {
if (i < 1) return false;
LNode* P;
int j = 0;
P = L;
while (P != NULL && j < i - 1) {
P = P->next;
j++;
}
LNode* S = (LNode*)malloc(sizeof(LNode));
if (P == NULL) return false;
S->data = e;
S->next = P->next;
P->next = S;
return true;
}
bool ListDelet(LinkeList& L, int i, ElemType e) {
if (i < 0) return false;
LNode* p;
p = L;
int j = 0;
while (p!=NULL&&j<i-1){
p = p->next;
}
if (p == NULL) return false;
if (p->next == NULL) return false;
LNode* s = p->next;
e = s->data;
p->next = s->next;
free(s);
return true;
}
bool DeletNode(LNode* p) {
if (p == NULL) return false;
if (p->next == NULL) {
p = p->next;
return true;
}
p->next = p->next->next;
p->data = p->next->data;
free(p->next);
return true;
}
LNode* GetElem(LinkeList L, int i) {
LNode* p;
p = L;
int j = 0;
while (p != NULL && j < i ) {
p = p->next;
}
return p;
}
LNode* LocateElem(LinkeList L, ElemType e) {
LNode* p=L->next;
while (p->next!=NULL&&p->data!=e){
p = p->next;
}
return p;
}
bool ShowLNode(LinkeList L) {
if (L == NULL) return false;
LNode* p = L->next;
while (p!= NULL) {
printf("%d\t", p->data);
p = p->next;
}
return true;
}
int main() {
LNode* L;
InitLIst(L);
HeadInsert(L);
ShowLNode(L);
ListInsert(L, 2, 66);
ShowLNode(L);
return 1;
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-2301.html