#include <stdio.h>
#include <stdlib.h>
typedef struct zy
{
int num;
struct zy *next; //指向结构体类型的指针
}yh;
int ListLength(yh *head)
{
yh *p=head;
int size=0;
while(p->next!=NULL)
{
p=p->next;
size++;
}
return size;
}
int ListInsert(yh *head,int i,int x)
{
yh *p,*q;
int j;
p=head; // p指向首元节点
j=-1;
while(p->next!=NULL&&j<i-1)
{
p=p->next;
j++;
}
if((q=(yh*)malloc(sizeof(yh)))==NULL)exit(1);
//为q申请内存空间
q->num=x;
q->next=p->next;
p->next=q;
return 1;
}
void TraveList(yh *head)
{
yh *p;
int count=0;
p=head->next;
while(p->next!=NULL)
{
count++;
printf("%d\n",p->num);
p=p->next;
}
printf("Length is %d\n",count);
}
void DeleteList(yh *head,int n)
{
yh *p,*q;
p=head;
int count=0;
while(count<n-1)
{
count++;
p=p->next;
}
q=p->next;
p->next=q->next;
free(q);
q->next=NULL;
}
void main()
{
yh *head,*q;
q=head;
if((head=(yh*)malloc(sizeof(yh)))==NULL)exit(1); //初始化头结点,head是一个结点但是没有存储数据
head->next=NULL;
int i,x;
for(i=0;i<10;i++)
{
ListInsert(head,i,i+1);
}
//printf("%d",head->next->next->num);
//TraveList(head);
DeleteList(head,9);
TraveList(head);
}