线性表链式存储操作实现
删除操作和查找操作不起作用,求指教。附代码:
#include<stdio.h>
#include<malloc.h>
#include"conio.h"
#include"string.h"
#define ERROR 0
#define OK 1
#define EQUAL 1
#define OVERFLOW -1
#define LIST_INIT_SIZE 200
#define LISTINCREMENT 10
struct STU{
char name[20];
char number[10];
char sex[5];
char kind[10];
char address[10];
int age;
}stu[100];
typedef struct STU ElemType;
struct LIST
{
ElemType *elem;
int length;
int listsize;
};
typedef struct LIST List;
struct LNODE
{
ElemType data;
struct LNODE *next;;
};
typedef struct LNODE LNode;
typedef struct LNODE *LinkList;
int init(LinkList *L)
{
*L=(LNode *)malloc(sizeof(LNode));
if(!L) exit(ERROR);
(*L)->next=NULL;
return OK;
}
int ListLength(LinkList L)
{
int j=0;
while (L->next)
{
L=L->next;
j++;
}
return j;
}
int GetElem(LinkList L,int i,struct STU *e)
{
LinkList p; int j;
p=L->next;j=1;
while(p&&j<i){
p=p->next;++j;
}
if(!p||j>1) return ERROR;
*e=p->data;
return OK;
}
int ListDelete(LinkList L,int i, struct STU e)
{
LinkList p,q;
int j;
p=L;j=0;
while(p->next&&j<i-1)
{
p=p->next;j++;
}
if((!p->next)||j>i-1);
return ERROR;
q=p->next;
p->next=q->next;
e=q->data;
free(q);
return OK;
}
int EqualList(ElemType *e1,ElemType *e2)
{
if (strcmp(e1->name,e2->name)==0)
return 1;
else
return 0;
}
int Less_EqualList(ElemType *e1,ElemType *e2)
{
if (strcmp(e1->name,e2->name)<=0)
return 1;
else
return 0;
}
int LocateElem(LinkList La,ElemType e,int type)
{
LinkList p;
p=La;
switch (type)
{
case EQUAL:
while(p->next)
{
p=p->next;
if(EqualList(&p->data,&e))
return 1;
}
return 0;
break;
default:
break;
}
return 0;
}
int printlist(LinkList L)
{
LinkList p;
p=L;
printf("name number sex kind address age\n");
while(p->next)
{
p=p->next;
printf("%-10s %s \t%-10s\t%-10s%-10s %d\n", p->data.name, p->data.number,
p->data.sex, p->data.kind, p->data.address, p->data.age);
}
printf("\n");
}
int ListInsert(LinkList L,int i,ElemType e)
{
LinkList p,s;
int j;
p=L;j=0;
while(p&&j<i-1)
{
p=p->next;
++j;
}
if(!p||j>i-1) return ERROR;
s=(LinkList)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
return OK;
}
int main()
{
struct STU e;
LinkList La,Lb,Lc;
void clrscr();
printf("\n\n-------------------List Demo is running...----------------\n\n");
printf("First is InsertList function.\n");
init(&La);
strcpy(e.name,"Elbert");
strcpy(e.number,"1211");
strcpy(e.sex,"boy");
strcpy(e.kind,"art");
strcpy(e.address,"tianjin");
e.age=18;
ListInsert(La,1,e);
strcpy(e.name,"Joe");
strcpy(e.number,"1212");
strcpy(e.sex,"girl");
strcpy(e.kind,"sport");
strcpy(e.address,"beijing");
e.age=17;
ListInsert(La,2,e);
printlist(La);
getch();
strcpy(e.name,"Branson");
strcpy(e.number,"1213");
strcpy(e.sex,"boy");
strcpy(e.kind,"art");
strcpy(e.address,"shanghai");
e.age=18;
ListInsert(La,2,e);
printlist(La);
getch();
printf("First is DeletetList function.\n");
ListDelete(La,2,e);
printlist(La);
getch();
printf("First is GetList function.\n");
GetElem(La,1,e);
printlist(La);
getch();
return 0;
}