您的位置:首页 > 理论基础 > 数据结构算法

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

2016-09-27 10:10 495 查看
2118数据结构实验之链表三:链表的逆置.

#include<iostream>
#include<malloc.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node *creat()
{
struct node *head,*tail,*p;;
int i;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
while(p!=NULL)//条件为(p->data!=-1),将出现Runtime Error
{
p=(struct node *)malloc(sizeof (struct node ));
cin>>p->data;
p->next=NULL;
tail->next=p;
tail = p;
if(p->data==-1) break;

}
return head;
};
void rerserve(struct node *head)
{
struct node *p,*q;
p=head->next;
q=p->next;
head->next=NULL;
while(p->data!=-1)
{
p->next=head->next;
head->next=p;
p=q;
if(q->data!=-1)
q=q->next;
}
}
void display(struct node *head)
{
struct node *q;
q=head->next;
while(q!=NULL)
{
cout << q->data;
if(q->next!=NULL)
cout<< " ";
q=q->next;
}
cout<<endl;
}
int main()
{
int n,h;
struct node *head;
head=creat();
rerserve(head);
display(head);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: