#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>
#define OVERFLOW 0
#define OK 1
//reverse the list with no extra space
typedef struct student
{
int num;
struct student *next;
}node,*slist;
slist creat(int n)
{
slist h,p,q;
int a;
h = NULL;
printf("input these integers:\n");
for(int i=0; i<n; i++)
{
scanf("%d", &a);
p = (slist)malloc(sizeof(node));
p->num = a;
p->next = NULL;
if(h == NULL)
{
h = p;
q = p;
}
else
{
q->next = p;
q = p;
}
}
return h;
}
slist reverse(slist p)
{
slist s,h_temp,q;
s = q = p->next;
h_temp = p;
p->next = NULL; //while circle before the original condition
while(q != NULL)
{
s = q->next;
q->next = h_temp;
h_temp = q;
q = s; //return the original condition
}
return h_temp;
}
int main()
{
int n;
slist h;
printf("input the counts:\n");
scanf("%d", &n);
h = creat(n);
h = reverse(h);
while(h != NULL)
{
printf("%d ", h->num);
h = h->next;
}
return 0;
}
头插法实现链表倒置
最新推荐文章于 2024-06-22 16:35:45 发布