附上全部代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<stdio.h>
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode* next;
}LNode,LinkList;
LinkList CreatList1(LNode& L) {
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
LNode* s;
int x;
scanf("%d", &x);
while (x != 9999) {
s = (LinkList)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
scanf("%d", &x);
}
return L;
}
LinkList CreatList2(LinkList& L) {
int x;
L = (LNode*)malloc(sizeof(LNode));
LNode* s, * r = L;
scanf("%d", &x);
while (x != 9999) {
s = (LinkList)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf("%d", &x);
}
r->next = NULL;
return L;
}
void ListPrint(LinkList& L) {
L = L->next;
while (L != NULL) {
printf("%3d", L->data);
/if (L->next != NULL)
printf(" ");/
L = L->next;
}
printf("\n");
}
LNode GetElem(LinkList L, int i) {
int j = 1;
LNode p = L->next;
if (0 == i) {
return L;
}
if (i < 1) {
return NULL;
}
while (p&&j<i){
p = p->next;
j++; }
return p;
}
LNode* LoacteElem(LNode* L, ElemType e) {//按值查找
LNode* p = L->next;
while (p && p->data != e) {
p = p->next;
}
return p;
}
bool ListInster(LNode* L, int i, ElemType e) {
LNode* p = GetElem(L, i-1);
if (NULL == p) {
return false;
}
LNode* q = (LNode*)malloc(sizeof(LNode));
q->next = p->next;
q->data = e;
p->next = q;
}
int main() {
LNode* L;
LinkList search;
CreatList2(L);
ListPrint(L);
search = GetElem(L, 2);//按序列查找
if (search != NULL) {
printf("按值查找成功");
printf("%d\n", search->data);
}
/CreatList2(L);
ListPrint(L);/
//printf("%d", search->data);
return 0;}