线性表链式存储结构代码记录
#include<stdio.h>
#include<stdlib.h>
typedef int Elemtype;
typedef struct
{
Elemtype data;
struct Node *next;
}Node;
typedef Node *Linklist;
Elemtype GetLinklist(Linklist L,int i)
{
Linklist p;
int n,j=1;
p=L->next;
while(p&&j<i)
{
p=p->next;
j++;
}
if(!p) return -1;
return p->data;
}
int Nodeinsert(Linklist L,Linklist n)
{
n->next=L->next;
L->next=n;
return 1;
}
int Datainsert(Linklist L,Elemtype add)
{
Linklist n;
n=(Linklist)malloc(sizeof(Node));
n->next=NULL;
n->data=add;
Nodeinsert (L,n);
return 1;
}
int NodeDelete(Linklist L,int position)
{
int n;
Linklist a,b;
a=L;
n=1;
while(a->next&&n<position)
{
n++;
a=a->next;
}
if(!(a->next)||n>position) return 0;
b=a->next;
a->next=b->next;
free(b);
return 1;
}
void main ()
{
int n;
Linklist L,p;
L=(Linklist)malloc(sizeof(Node));
L->next=NULL;
for(n=10;n>0;n--)
Datainsert(L,n);
for(n=1;n<=5;n++)
NodeDelete(L,1);
p=L->next;
while(p)
{
n++;
printf("%d ",p->data);
p=p->next;
}
}