将链表逆置
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct _node{
int number;
struct _node *next;
}Node;
typedef struct {
Node *head;
Node *tail;
}List;
void add(List *plist)
{
int number;
bool first=true;
printf("input the number untill -1\n");
scanf("%d",&number);
while(number!=-1)
{
Node *p=(Node *)malloc(sizeof(Node));
p->number=number;
if(first)
{
p->next=NULL;
plist->head->next=p;
plist->tail=p;
first=false;
}
else{
p->next=plist->head->next;
plist->head->next=p;
}
scanf("%d",&number);
}
}
void print(List *plist)
{
Node *p;
for( p=plist->head->next ; p ;p=p->next )
{
printf("%d--",p->number);
}
printf("\n");
}
void clean_up(List *plist)
{
Node *p;
Node *q;
for( p=plist->head,q=NULL; p; p=q )
{
q=p->next;
free(p);
}
}
void inversion(List *plist)
{
Node *p;
Node *q;
bool first=true;
for( p=plist->head->next; p; p=q )
{
if(first)
{
q=p->next;
p->next=NULL;
first=false;
}
else{
q=p->next;
p->next=plist->head->next;
plist->head->next=p;
}
}
}
int main()
{
List list;
list.head=(Node *)malloc(sizeof(Node));
list.tail=NULL;
add(&list);
printf("in the outset the LIST is:\n");
print(&list);
inversion(&list);
printf("now the LIST is:\n");
print(&list);
clean_up(&list);
}