链表-删除
#include<stdio.h>
#include<stdlib.h>
typedef struct _node{
int num;
struct _node *next;
}Node;
typedef struct{
Node *head;
Node *tail;
}List;
void add(List *plist);
void print(List *plist);
void clean_up(List *plist);
int main()
{
List list;
list.head;
list.tail;
list.head=list.tail=NULL;
Node *p;
Node *q;
add(&list);
printf("in the outset the LIST is:\n");
print(&list);
int number;
int count;
scanf("%d",&number);
for(p=list.head,q=NULL,count=0;p;q=p,p=p->next)
{
count++;
if((count==number)&&(count!=1))
{
q->next=p->next;
free(p);
break;
}
if((count==number)&&(count==1))
{
list.head=p->next;
free(p);
break;
}
}
printf("now the LIST is:\n");
print(&list);
clean_up(&list);
}
void add(List *plist)
{
int number;
printf("please input the number untill 0\n");
scanf("%d",&number);
while(number!=0)
{
Node *p=(Node *)malloc(sizeof(Node));
p->num=number;
p->next=NULL;
if(plist->head)
{
plist->tail->next=p;
plist->tail=p;
}
else{
plist->head=p;
plist->tail=p;
}
scanf("%d",&number);
}
}
void print(List *plist)
{
Node *p;
for(p=plist->head;p;p=p->next)
{
printf("%d--",p->num);
}
printf("\n");
}
void clean_up(List *plist)
{
Node *p;
Node *q;
for(q=NULL,p=plist->head;p;p=q)
{
q=p->next;
free(p);
}
}