数据结构的一个程序有有关问题,求指点
数据结构的一个程序有问题,求指点
这学期在学数据结构
于是乎有个实验,用c语言实现线性表的顺式和链式结构
虽然照着例子编了一个顺式的但完全不能用啊
快没时间了还得做链式的
所以求大神指点一下
以下是代码
这学期在学数据结构
于是乎有个实验,用c语言实现线性表的顺式和链式结构
虽然照着例子编了一个顺式的但完全不能用啊
快没时间了还得做链式的
所以求大神指点一下
以下是代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
typedef int elemtype;
typedef struct
{
char number[10];
char name[20];
int age;
}DATA;
typedef struct
{
elemtype *elem;
DATA ListData[MAXLEN+1];
int ListLength;
}sqlist;
void InitList_sq(sqlist *L)
{
L->elem=(elemtype *)malloc(MAXLEN*sizeof(elemtype));
L->ListLength=0;
if(!L->elem)
exit(0);
printf("\t\t------------建立成功-----------\n");
getch();
return;
}
int ListLength_sq(sqlist *L)
{
return (L->ListLength);
}
int ListEmpty(sqlist *L)
{
if(L->ListLength==0)
return ;
exit(0);
}
void ListAdd_sq(sqlist *L,DATA data)
{
printf("请输入添加的节点(学号,姓名,年龄):");
scanf("%s,%s,%d",&data.number,&data.name,&data.age);
L->ListData[L->ListLength++]=data;
getch();
return;
}
void ListDelete_sq(sqlist *L,int n)
{
int i;
if(n < 0 || n > L->ListLength)
{
printf("删除位置非法,不能删除元素");
getch();
return;
}
for(i=n;i<L->ListLength;i++)
{
L->ListData[i]=L->ListData[i+1];
}
L->ListLength--;
getch();
return;
}
DATA *ListFindByNum(sqlist *L,int m)
{
if(m<0||m>L->ListLength)
{
printf("节点序号错误,不能返回元素");
getch();
return;
}
return &(L->ListData[m]);
}
int ListFindByNumber(sqlist *L,char *key)
{
int i;
if(i<0||i>L->ListLength)
{
printf("无对应元素,无法返回");
getch();
return;
}
for(i=0;i<L->ListLength;i++)
{
if(L->ListData[i].number==key)
return i;
}
}
void ListAll(sqlist *L)
{
int i;
if(i<0||i>L->ListLength)
{
printf("没有元素,无法输出");
getch();
return;
}
for(i=0;i<L->ListLength;i++)
{
printf("(%s %s %d)\n",L->ListData[i].number,L->ListData[i].name,L->ListData[i].age);
}
getch();
return;
}
void main()
{
char a;
int i;
int n;
int m;
sqlist L;
DATA data;
DATA *pdata;
char key[10];
InitList_sq(&L);
ListEmpty(&L);
do
{
system("cls");
printf("\t\t============ 请按提示输入指令 ==============\n");
printf("\t\t------------1向线性表插入元素---------------\n");
printf("\t\t------------2删除线性表元素-----------------\n");
printf("\t\t------------3显示所有元素-------------------\n");
printf("\t\t------------4按学号查找元素-----------------\n");
printf("\t\t------------5按关键字查找元素---------------\n");
printf("\t\t------------0退出系统-----------------------\n");
a=getchar();
switch(a)
{
case'1':ListAdd_sq(&L,data);
break;
case'2':
{
printf("请输入要删除的元素");
scanf("%d",&n);
ListDelete_sq(&L,n);
}
break;
case'3':ListAll(&L);
break;
case'4':
{
printf("\n要取出节点的序号:");
scanf("%d",&m);
pdata=ListFindByNum(&L,m);
if(pdata)
{