SDUT2118数据结构实验之链表三:链表的逆置

来源:互联网 发布:知乎类似洛丽塔小说 编辑:程序博客网 时间:2024/06/05 15:13
#include<bits/stdc++.h>using namespace std;struct node{    int data;    struct node *next;}*head,*tail,*p,*q;int n,t;void built(){    head=(struct node *)malloc(sizeof(struct node));    tail=head;    while(scanf("%d",&t)&&t!=-1)    {        p=(struct node *)malloc(sizeof(struct node));        p->data=t;        tail->next=p;        tail=p;    }    tail->next=NULL;}void print(){    p=head->next;    while(p)    {        printf("%d",p->data);        if(p->next)            printf(" ");        p=p->next;    }}void res(){    p=head->next;    head->next=NULL;    while(p)    {        q=p;        p=p->next;        q->next=head->next;        head->next=q;    }}int main(){    built();    res();    print();}

0 0
原创粉丝点击