chain.c
#include<stdio.h>
#include<malloc.h>
typedef struct node Node;
struct node
{
Node *pnext;
int num;
};
Node* listcreat()
{
Node* head;
head=(Node*)malloc(sizeof(Node));
head->num=0;
head->pnext = NULL;
return head;
}
void listadd(Node* ph,int num)
{
Node* ptem=ph;
Node* padd;
if(ph==NULL)
{
printf("the list is null");
return ;
}
while(ptem->pnext != NULL)
ptem=ptem->pnext;
padd=(Node*)malloc(sizeof(Node));
padd->num=num;;
ptem->pnext=padd;
padd->pnext=NULL;
}
int listlen(Node* ph)
{
Node* ptem=ph;
int i=1;
if (ptem == NULL)
return 0;
while(ptem->pnext != NULL)
{
ptem=ptem->pnext;
i++;
}
return i;
}
void dellist(Node* ph,int i)
{
Node* ptem=ph;
Node* pcur;
int len=0;
int j=0;
len=listlen(ph);
if((len<=1)&&(i<1)&&(i>len))
return ;
while(j<i-1)
{ pcur=ptem;
ptem=ptem->pnext;
j++;
}
pcur->pnext=ptem->pnext;
free(ptem);
return;
}
void listscan(Node*ph)
{
int i=1;
Node* ptem=ph;
if(ph==NULL)
return ;
while(ptem->pnext !=NULL)
{
printf("the %d number:is %d \n",i,ptem->num);
ptem=ptem->pnext;
i++;
}
printf("the last %d number:is %d \n",i,ptem->num);
return;
}
int main()
{
Node*head, ptem,pnode;
head=(Node*)listcreat();
if (head==NULL)
{
printf("creat error");
return 0;
}
listadd(head,2);
// listadd(head,3);
listscan(head);
return 0;
}