单链表逆置
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
int date;
struct node* next;
}*linklist;
linklist creat();
linklist insert_head(linklist l,int element);
void output(linklist l);
linklist reverse(linklist l);
#endif
#include "head.h"
int main(int argc, const char *argv[])
{
linklist l=NULL;
int n,element;
printf("how many do you want:");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("please input:");
scanf("%d",&element);
l=insert_head(l,element);
}
output(l);
l=reverse(l);
output(l);
return 0;
}
#include "head.h"
linklist creat()
{
linklist s=(linklist)malloc(sizeof(struct node));
if(NULL==s)
{
return NULL;
}
s->date=0;
s->next=NULL;
}
linklist insert_head(linklist l,int element)
{
linklist s=creat();
if(NULL==l)
{
l=s;
s->date=element;
}
else
{
s->next=l->next;
l->next=s;
s->date=l->date;
l->date=element;
}
return l;
}
void output(linklist l)
{
if(NULL==l)
{
puts("error");
return;
}
else
{
while(l!=NULL)
{
printf("%d\t",l->date);
l=l->next;
}
}
puts("");
}
linklist reverse(linklist l)
{
if(NULL==l)
{
puts("error");
return NULL;
}
else if(l->next==NULL)
{
return l;
}
else
{
linklist p=l->next;
l->next=NULL;
while(p!=NULL)
{
linklist q=p;
p=p->next;
q->next=l;
l=q;
}
return l;
}
}