- 论坛徽章:
- 0
|
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
void initlist(LinkList L);
int Inselem(LinkList L,int i,int x);
void displist(LinkList L);
int Getlen(LinkList L);
main()
{
LinkList List=NULL;
int ii,xx,error,counter;
initlist(List);
printf("Please enter the linklist size:");
scanf("%d",&List->data);
printf("\n");
for(counter=1;counter<=List->data;counter++){
printf("Please enter data into linklist:");
scanf("%d",&xx);
Inselem(List,counter,xx);
}
printf("\n\nThe link list:\n");
displist(List);
printf("\n");
printf("\nNow ,please insert element's location and value:");
scanf("%d%d",&ii,&xx);
printf("\n");
error=Inselem(List,ii,xx);
printf("\n The new link list:\n");
if(error==1)
printf("Location error!\n");
else
displist(List);
return 0;
}
void initlist(LinkList L)
{ L=malloc(sizeof(LNode));
L->next=NULL;
}
int Inselem(LinkList L,int i,int x)
{
LNode *p,*q,*s;
int pos=1;
p=L;
if(i<1 || i>Getlen(L)+1)
return 1;
s=malloc(sizeof(LNode));
s->data=x;
while(pos<=i)
{ q=p;p=p->next;
pos++;
}
s->next=q->next;
q->next=s;
}
void displist(LinkList L)
{ LNode *p;
p=L->next;
while(p!=NULL)
{ printf("%4d",p->data);
p=p->next;
}
printf("\n");
}
int Getlen(LinkList L){
return L->data;
} |
|