Code
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
struct sList
{
int List[MAXSIZE];
int size;
};
void setNULL(struct sList *p)
{
p->size=0;
}
int Length(struct sList *p)
{
return(p->size);
}
void InitData(struct sList *p)
{
int i=0,x;
printf("please input InitData:\n");
scanf("%d",&x);
while(x!=0)
{ p->List[i]=x;
i++;
scanf("%d",&x);
}
p->size=i+1;
}
void Insert(struct sList *p,int x,int i)
{
int j;
if(i<1||i>p->size+1)
printf("error!\n");
else
{
p->size++;
for(j=p->size-1;j>=i;j--)
p->List[j]=p->List[j-1];
p->List[j]=x;
}
}
void display(struct sList *p)
{
int j;
if (p->size==0)
printf("List is empty!\n");
else
{ printf("List:");
if(p->size==1)
printf("%5d",p->List[p->size-1]);
else
{for(j=0;j<p->size-1;j++)
printf("%5d",p->List[j]);
}
printf("\n");
}
}
void delete(struct sList *p,int i)
{
int j;
if(i<1||i>p->size)
printf("error!\n");
else
{for(j=i-1;j<p->size-1;j++)
p->List[j]=p->List[j+1];
p->size--;
}
}
int Find(struct sList *p,int i)
{
if(i<1||i>p->size)
printf("error");
else
printf("%d",p->List[i-1]);
}
main()
{
struct sList *q;
int e,pos,delData;
setNULL(q);
printf("%d\n",Length(q));
InitData(q);
display(q);
printf("please input what you want to delete:\n");
scanf("%d",&delData);
delete(q,delData);
display(q);
printf("please input what you want to insert:\n");
scanf("%d",&e);
scanf("%d",&pos);
Insert(q,e,pos);
display(q);
printf("please input what you want to find position:\n");
scanf("%d",&pos);
Find(q,pos);
}
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
struct sList
{
int List[MAXSIZE];
int size;
};
void setNULL(struct sList *p)
{
p->size=0;
}
int Length(struct sList *p)
{
return(p->size);
}
void InitData(struct sList *p)
{
int i=0,x;
printf("please input InitData:\n");
scanf("%d",&x);
while(x!=0)
{ p->List[i]=x;
i++;
scanf("%d",&x);
}
p->size=i+1;
}
void Insert(struct sList *p,int x,int i)
{
int j;
if(i<1||i>p->size+1)
printf("error!\n");
else
{
p->size++;
for(j=p->size-1;j>=i;j--)
p->List[j]=p->List[j-1];
p->List[j]=x;
}
}
void display(struct sList *p)
{
int j;
if (p->size==0)
printf("List is empty!\n");
else
{ printf("List:");
if(p->size==1)
printf("%5d",p->List[p->size-1]);
else
{for(j=0;j<p->size-1;j++)
printf("%5d",p->List[j]);
}
printf("\n");
}
}
void delete(struct sList *p,int i)
{
int j;
if(i<1||i>p->size)
printf("error!\n");
else
{for(j=i-1;j<p->size-1;j++)
p->List[j]=p->List[j+1];
p->size--;
}
}
int Find(struct sList *p,int i)
{
if(i<1||i>p->size)
printf("error");
else
printf("%d",p->List[i-1]);
}
main()
{
struct sList *q;
int e,pos,delData;
setNULL(q);
printf("%d\n",Length(q));
InitData(q);
display(q);
printf("please input what you want to delete:\n");
scanf("%d",&delData);
delete(q,delData);
display(q);
printf("please input what you want to insert:\n");
scanf("%d",&e);
scanf("%d",&pos);
Insert(q,e,pos);
display(q);
printf("please input what you want to find position:\n");
scanf("%d",&pos);
Find(q,pos);
}