数据结构单链表应用

#include <stdio.h>
#include <stdlib.h>
#include <string.h>




#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2


typedef int Status;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct {
char sno[4];
char sname[10];
int sage;
char sdept[3];
}StuType;
typedef StuType ElemType;
typedef struct  LNode 
{
ElemType      data;
struct LNode   *next;
} LNode, *List;






Status InitList(List &L){  
   L = (List)malloc(sizeof(LNode));
   if (!L) exit(OVERFLOW);
   L->next = NULL;
   return OK;
}


Status ListInsert(List &L, int i, ElemType e) {
    LNode *p = L;    int j = 0;
    while (p && j < i-1) 
         { p = p->next;  ++j; }
    if (!p || j > i-1)
        return ERROR;    
    LNode *s = (List) malloc ( sizeof (LNode)); 
    s->data = e; 
    s->next = p->next;  p->next = s;
    return OK;
}




Status ListDelete(List &L, int i, ElemType &e){ 
    LNode *p = L;    int j = 0;
    while (p->next && j < i-1) 
        {  p = p->next;   ++j; } 
    if  (!(p->next) || j > i-1) 
        return ERROR;
    LNode *q = p->next;   p->next = q->next; 
    e = q->data;   free(q); 
    return OK;
}


int LocateElem(List L, ElemType e, Status (*compare)(ElemType, ElemType)){
  LNode *p=L->next;
  int i=1;
  while (p && !(*compare)(p->data, e)) {
    p=p->next;
++i;
  }
  if (p) 
    return i;
  else
    return 0;
}


Status PutElem( List&L, int i,ElemType &e )
{
  ElemType tmp;
      LNode *p = L->next;   int j = 1;
  while (p && j<i)  { p = p->next;  ++j;  } 
  if ( !p || j>i )
    return ERROR;
      tmp=p->data;
  p->data =e;
      e=tmp;
      return OK;
}


Status GetElem(List L, int i, ElemType &e){ 
  LNode *p;
  p = L->next;   int j = 1
  while (p && j<i)  { p = p->next;  ++j;  }
  if ( !p || j>i )
return ERROR;
  e = p->data;               
  return OK;
}


bool ListEmpty(List L )
{
if (!L->next) 
return true;
else
return false;
}


Status ListTraverse(List L, Status (* visit)(ElemType)){
    LNode *p;
    p = L->next;
    while (p) {
        if (!visit(p->data)) return ERROR;   
        p=p->next; 
    }
    return OK;
}
int ListLength(List L )
{
LNode *p=L;
int i=0;
while (p->next)
{
p=p->next;
++i;
}
return i;
}




Status equal (ElemType a,ElemType b){
int i;
for(i=0;(a.sno[i]!='\0')&&(b.sno[i]!='\0');i++){
if(a.sno[i]!=b.sno[i]){
return ERROR;
}
}
return OK;
}


void visit(ElemType e){
printf("%s\t%s\t%d\t%s\n",e.sno,e.sname,e.sage,e.sdept);
}




void main()
{
List L;
ElemType stu,stu_tmp;
int func,pos;


if (!InitList(L)) 
{
printf("overflow!\n");
exit(-1);
}
for (int j=0;;j++)
{
printf("Please select the function:\n0--Exit\n1--insert student\n2--delete student\n3--update student name\n4--search student with sno\n5--show all student\n6--count the student\n Please input:");
scanf("%d",&func);
switch (func)
{
case 0: 
exit(1);
break;
case 1: //插入学生信息
printf("sno: ");
scanf("%s", &stu.sno);
printf("sname: ");
scanf("%s", &stu.sname);
printf("sage: ");
scanf("%d", &stu.sage);
printf("sdept: ");
scanf("%s", &stu.sdept);
ListInsert(L,ListLength(L)+1,stu);
break;
case 2://删除指定学号的学生信息
printf("input the sno:");
scanf("%s",&stu.sno);
pos=LocateElem(L,stu,equal);
if (pos>0)
ListDelete(L,pos,stu);
else
printf("The student is not exist!\n");
break;
case 3://更新指定学号的学生姓名
printf("input the sno of the student to be updated:");
scanf("%s",&stu.sno);
pos=LocateElem(L,stu,equal);
if (pos>0)
{
printf("input the new name:");
scanf("%s",&stu.sname);
GetElem(L,pos,stu_tmp);
stu.sage=stu_tmp.sage;
strcpy(stu.sdept,stu_tmp.sdept);
PutElem(L,pos,stu);
}
else
printf("The student is not exist!\n");
break;
case 4://查找指定学号的学生信息
printf("input the sno:");
scanf("%s",&stu.sno);
pos=LocateElem(L,stu,equal);
if (pos>0)
{
GetElem(L,pos,stu_tmp);
printf("%s\t%s\t%d\t%s\n",stu_tmp.sno,stu_tmp.sname,stu_tmp.sage,stu_tmp.sdept);
}
else
printf("The student is not exist!\n");
break;
case 5: //输出所有学生信息
ListTraverse(L,visit);
break;
case 6: //输出学生数量
printf("The student count is %d\n",ListLength(L));
break;
}
}
}

猜你喜欢

转载自blog.csdn.net/wangjianxin97/article/details/78494039