软件技术基础上机作业(从以下题目
第二题
// reverse_list。cpp : 定义控制台应用程序的入口点。
//
#include
using namespace std;
struct node
{
int data;
node * next;
};
int main(int argc, char* argv[])
{
int data;
int i=0,count=0;
node *head=NULL;
node *tail=NULL;
for(i=0;i> data;
if(data==-1)
break;
node * p=new node;
p->data=data;
p->next=NULL;
if(h...全部
第二题
// reverse_list。cpp : 定义控制台应用程序的入口点。
//
#include
using namespace std;
struct node
{
int data;
node * next;
};
int main(int argc, char* argv[])
{
int data;
int i=0,count=0;
node *head=NULL;
node *tail=NULL;
for(i=0;i> data;
if(data==-1)
break;
node * p=new node;
p->data=data;
p->next=NULL;
if(head==NULL)
{
head=p;
tail=p;
}
else
{
tail->next=p;
tail=p;
}
count++;
}
cout datanext;
}
p=head;
node * forwardnode=NULL; //保留前一节点
while(1)
{
if(p==NULL)
break;
node * pnext=p->next; //保留下个节点
if(forwardnode!=NULL)
{
p->next=forwardnode; //后续节点设为前一节点
}
else
{
head->next=NULL; //尾节点之后续指针为null
}
forwardnode=p; //保留前一节点
p=pnext; //继续读取后后续节点
}
//修改头节点和尾节点
p=tail;
tail=head;
head=p;
cout datanext;
}
return 0;
}
结果如图,第一次打印为输入的,-1结束输入。
第二次得是逆转之后的结果
。收起