主题:顺序表存储疑问,帮忙看看!应该不难,但重要!
/* lined list saving structure */
#include<stdio.h>
#define MAXSIZE 100
typedef int Element;
typedef struct
{
Element *data;
int length; /* the length of the list */
int content; /* the content of the list */
}linelist;
/* init the list */
void initlist(linelist *L)
{
L->data=(Element *)malloc(sizeof(Element)*MAXSIZE); /* content of the list */
L->length=0; /* the numbers of element */
L->content=MAXSIZE; /* the max length of the list */
}
/* insert a element into list */
int insert(linelist *L,int i,Element e)
{
int j;
if(i<1||i>L->content+1) return 0; /* insert wrong */
if(L->length==L->content) /* have no enough space */
{
L->data=(Element *)realloc(L->data,(L->content+1)*sizeof(Element));
L->content++;
}
for(j=L->length;j>=i;j--)
L->data[j]=L->data[j-1]; /* move rear */
L->data[i-1]=e;
L->length++;
return 1;
}
/* delete the element in which position */
int delposition(linelist *L,int i,Element *e)
{
int j;
if(i<1||i>L->length) return 0;
*e=L->data[i-1]; /* store the data which is deleted */
for(j=i;j<L->length;j++)
L->data[j-1]=L->data[j];
L->length--;
return 1;
}
/* delete the element from the list */
void delelem(linelist *L,Element *e,Element *delem)
{
int i,j;
for(i=0;i<L->length;i++)
{
if(*delem==L->data[i]) /* search element */
{
e[i]=L->data[i]; /* store the element */
for(j=i;j<=L->length;j++)
L->data[i]=L->data[i+1];
L->length--;
}
}
}
int search(linelist L,Element e)
{
int i;
for(i=0;i<L.length;i++)
if(e==L.data[i]) return i+1;
return 0; /* not exist the element */
}
void putlist(linelist L)
{
int i;
for(i=0;i<L.length;i++)
printf("%3d",L.data[i]);
printf("\n");
}
main()
{
linelist S;
int w,p,i=0;
Element x;
Element e[100]; /* 存放删除的数据 */
Element f;
initlist(&S);
printf("Please input datas,exit for input -1:");
scanf("%d",&x);
while(x!=-1)
{
S.data[i]=x;
S.length++;
++i;
scanf("%d",&x);
}
printf("----------------------------------\n");
printf("1----Insert element!\n");
printf("2----Delete element from list!\n");
printf("3----Delete element in a position!\n");
printf("4----Search element!\n");
printf("5----Print the list!\n");
printf("0----Exit!\n");
printf("----------------------------------\n");
gt1: printf("Please choose 0--5:");
scanf("%d",&w);
switch(w)
{
case 1:
{
printf("please input a data(int) to insert:");
scanf("%d",&x);
printf("please input the position you want to insert:");
scanf("%d",&p);
insert(&S,p,x);
goto gt1;
}
case 2:
{
printf("please input the data you want to delete:");
scanf("%d",&x);
delelem(&S,e,&x);
goto gt1;
}
case 3:
{
printf("please input the position you want to delete:");
scanf("%d",&p);
delposition(&S,p,&f);
goto gt1;
}
case 4:
{
printf("please input the element value you want to search:");
scanf("%d",&x);
search(S,x);
goto gt1;
}
case 5:putlist(S);goto gt1;
case 0:break;
default :printf("Error...please input again!\n");goto gt1;
}
system("pause");
}
终于独立完成了一个线性表但简单存储,功能部完全,只是为了自己练习一下!但是发现一些疑问。
首先在定义结构但时候 Element *data;书上为什么都是定义但*data,而不是data,好多书上都是的,我不大明白用处是什么,帮忙分析一下,谢谢!
其次:帮忙解决一下goto语句问题,我不想使用GOTO语句,但是想实现我现在程序运行大功能,只有自己输入0的时候才退出这个功能。
#include<stdio.h>
#define MAXSIZE 100
typedef int Element;
typedef struct
{
Element *data;
int length; /* the length of the list */
int content; /* the content of the list */
}linelist;
/* init the list */
void initlist(linelist *L)
{
L->data=(Element *)malloc(sizeof(Element)*MAXSIZE); /* content of the list */
L->length=0; /* the numbers of element */
L->content=MAXSIZE; /* the max length of the list */
}
/* insert a element into list */
int insert(linelist *L,int i,Element e)
{
int j;
if(i<1||i>L->content+1) return 0; /* insert wrong */
if(L->length==L->content) /* have no enough space */
{
L->data=(Element *)realloc(L->data,(L->content+1)*sizeof(Element));
L->content++;
}
for(j=L->length;j>=i;j--)
L->data[j]=L->data[j-1]; /* move rear */
L->data[i-1]=e;
L->length++;
return 1;
}
/* delete the element in which position */
int delposition(linelist *L,int i,Element *e)
{
int j;
if(i<1||i>L->length) return 0;
*e=L->data[i-1]; /* store the data which is deleted */
for(j=i;j<L->length;j++)
L->data[j-1]=L->data[j];
L->length--;
return 1;
}
/* delete the element from the list */
void delelem(linelist *L,Element *e,Element *delem)
{
int i,j;
for(i=0;i<L->length;i++)
{
if(*delem==L->data[i]) /* search element */
{
e[i]=L->data[i]; /* store the element */
for(j=i;j<=L->length;j++)
L->data[i]=L->data[i+1];
L->length--;
}
}
}
int search(linelist L,Element e)
{
int i;
for(i=0;i<L.length;i++)
if(e==L.data[i]) return i+1;
return 0; /* not exist the element */
}
void putlist(linelist L)
{
int i;
for(i=0;i<L.length;i++)
printf("%3d",L.data[i]);
printf("\n");
}
main()
{
linelist S;
int w,p,i=0;
Element x;
Element e[100]; /* 存放删除的数据 */
Element f;
initlist(&S);
printf("Please input datas,exit for input -1:");
scanf("%d",&x);
while(x!=-1)
{
S.data[i]=x;
S.length++;
++i;
scanf("%d",&x);
}
printf("----------------------------------\n");
printf("1----Insert element!\n");
printf("2----Delete element from list!\n");
printf("3----Delete element in a position!\n");
printf("4----Search element!\n");
printf("5----Print the list!\n");
printf("0----Exit!\n");
printf("----------------------------------\n");
gt1: printf("Please choose 0--5:");
scanf("%d",&w);
switch(w)
{
case 1:
{
printf("please input a data(int) to insert:");
scanf("%d",&x);
printf("please input the position you want to insert:");
scanf("%d",&p);
insert(&S,p,x);
goto gt1;
}
case 2:
{
printf("please input the data you want to delete:");
scanf("%d",&x);
delelem(&S,e,&x);
goto gt1;
}
case 3:
{
printf("please input the position you want to delete:");
scanf("%d",&p);
delposition(&S,p,&f);
goto gt1;
}
case 4:
{
printf("please input the element value you want to search:");
scanf("%d",&x);
search(S,x);
goto gt1;
}
case 5:putlist(S);goto gt1;
case 0:break;
default :printf("Error...please input again!\n");goto gt1;
}
system("pause");
}
终于独立完成了一个线性表但简单存储,功能部完全,只是为了自己练习一下!但是发现一些疑问。
首先在定义结构但时候 Element *data;书上为什么都是定义但*data,而不是data,好多书上都是的,我不大明白用处是什么,帮忙分析一下,谢谢!
其次:帮忙解决一下goto语句问题,我不想使用GOTO语句,但是想实现我现在程序运行大功能,只有自己输入0的时候才退出这个功能。