把下面C语言的程序改成C++?求大神,急用!老师要我们必须用C++写。。

#include<stdio.h>
#include<stdlib.h>
typedef struct List_Node{
int info;
struct List_Node *next;
}node;//结点结构体
/******************************/
/* 尾插法建立带头结点的单链表 */
/******************************/
node* Creat_Node()
{
node *head,*pre,*p;
int x;
head=(node*)malloc(sizeof(node));;
head->next=NULL;
pre=head;
printf("输入各结点的值,以0结束:");
while(EOF!=(scanf("%d",&x))&&x!=0)
{
p=(node*)malloc(sizeof(node));
p->info=x;
p->next=pre->next;
pre->next=p;
pre=pre->next;
}
return head;
}
void Print_Node(node *head)
{
node *p=head->next;
printf("输出该链表:");
while(p)
{
printf("%-5d--->",p->info);
p=p->next;
}
if(p==NULL)
{
printf("^\n\n\n");
}
}

void Insert_Sort(node *head)
{
node *p,*pre,*q,*r;
p=head->next;
head->next=NULL;
while(p)
{
pre=p->next;
r=head;
q=head->next;
while(q&&q->info<p->info)
{
r=q;
q=q->next;
}
p->next=r->next;
r->next=p;
p=pre;
}
}

int main()
{
node *head;
head=Creat_Node();
Print_Node(head);
Insert_Sort(head);
Print_Node(head);
return 0;
}

#include<iostream>
#include<iomanip>

using namespace std;

typedef struct List_Node{
 int info;
 struct List_Node *next;
}node;//结点结构体

/******************************/
/* 尾插法建立带头结点的单链表 */
/******************************/

node* Creat_Node()
{
 node *head,*pre,*p;
 int x;
 
 head=new node;
 head->next=NULL;
 pre=head;
 cout<<"输入各结点的值,以0结束:"<<endl;
 
 while(cin>>x&&x!=0)
 {
  p=new node;
  p->info=x;
  p->next=pre->next;
  pre->next=p;
  pre=pre->next;
 }
 
 return head;
}

void Print_Node(node *head)
{
 node *p=head->next;
 
 cout<<"输出该链表:"<<endl;
 while(p)
 {
  cout.setf(ios::left);
  cout<<setw(5)<<p->info<<"--->";
  p=p->next;
 }
 
 if(p==NULL)
  cout<<'^'<<endl<<endl<<endl;
}
 
void Insert_Sort(node *head)
{
    node *p,*pre,*q,*r;
 
    p=head->next;
    head->next=NULL;
    while(p)
    {
        pre=p->next;
        r=head;
        q=head->next;
        while(q&&q->info<p->info)
        {
            r=q;
            q=q->next;
        }
        p->next=r->next;
        r->next=p;
        p=pre;
    }
}

int main()
{
    node *head;
 
    head=Creat_Node();
    Print_Node(head);
    Insert_Sort(head);
    Print_Node(head);
 
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-07-02

建议用codeblocks打开   改的不是很彻底   你再改下吧

相似回答