| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 258 人关注过本帖
标题:帮帮忙 链表问题
只看楼主 加入收藏
vac
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-4-1
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:2 
帮帮忙 链表问题
#include<iostream>
using namespace std;
#define NULL 0
struct number
{
    int num;
    number*next;
};
int n,temp;



int main()
{ number*creat(void);
  number*del(number*,long);

  void print(number*);
  number*head;
  long del_num;
  cout<<"input number:"<<endl;
  head=creat();      
                  //返回头指针
  print(head);                           //输出全部结点
  
  cout<<endl<<"input the deleted number:";
  cin>>del_num;
  while(del_num!=0)
     {
      head=del(head,del_num);
      print(head);
      cout<<"input the deleted number:";
      cin>>del_num;
  }
  return 0;
}

 
/*................................................creat.................................................................................*/

number *creat(void)                   //建立链表函数
{number *head;
number *p1,*p2;
n=0;
p1=p2=new number;
cin>>p1->num;
head=NULL;
while(p1->num!=0)
    {n=n+1;
     if(n==1)head=p1;
     else p2->next=p1;
     p2=p1;
     p1=new number;
     cin>>p1->num;
     p2->next=NULL;
     }
return(head);
}

/*............................................delete................................................................................*/
number *del(number *head,long num)               //删除数字
{number *p1,*p2;
if(head==NULL)
  {cout<<"list null!"<<endl;return(head);}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
   {
    p2=p1;p1=p1->next;
   }
if(num==p1->num)
    {if(p1==head)head=p1->next;
    else p2->next=p1->next;
    cout<<"delete;"<<num<<endl;
    }
return(head);
}

/*..............................................print order...............................................................................*/

void print(number *head)
{
    number *p,*p1,*p2;


    p=head;
    while((p->num>p1->num)&&(p1->next!=NULL))
    {P2=P1;
    P1=P1->next;
    }
    if(p->num>p1->num)
    { temp=p->num;
      p->num=p1->num;
      p1->num=temp;
    }

    if(head!=NULL)
        do{
        cout<<p->num<<" "<<endl;
        p=p->next;
        }while(p!=NULL);
   

}
2011-04-01 22:13
『点点滴滴』
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:168
专家分:1035
注 册:2007-7-9
收藏
得分:20 
程序代码:
#include<iostream>
using namespace std;
#define NULL 0
struct number
{
    int num;
    number*next;
};
int n,temp;



int main()
{
    number*creat(void);
    number*del(number*,long);
   
    void print(number*);
    number*head;
    long del_num;
    cout<<"input number:"<<endl;
    head=creat();     
    //返回头指针
    print(head);                           //输出全部结点
   
    cout<<endl<<"input the deleted number:";
    cin>>del_num;
    while(del_num!=0)
    {
        head=del(head,del_num);
        print(head);
        cout<<"input the deleted number:";
        cin>>del_num;
    }
    return 0;
} 


/*................................................creat.................................................................................*/

number *creat(void)                   //建立链表函数
{
    number *head;
    number *p1,*p2;
    n=0;
    p1=p2=new number;
    cin>>p1->num;
    head=NULL;
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)head=p1;
        else p2->next=p1;
        p2=p1;
        p1=new number;
        cin>>p1->num;
        p2->next=NULL;
    }
    return(head);
}

/*............................................delete................................................................................*/
number *del(number *head,long num)               //删除数字
{
    number *p1,*p2;
    if(head==NULL)
    {
        cout<<"list null!"<<endl;return(head);}
    p1=head;
    while(num!=p1->num&&p1->next!=NULL)
    {
        p2=p1;p1=p1->next;
    }
    if(num==p1->num)
    {
        if(p1==head)head=p1->next;
        else p2->next=p1->next;
        cout<<"delete;"<<num<<endl;
    }
    return(head);
}

/*..............................................print order...............................................................................*/

void print(number *head)
{
    number *p,*p1,*p2;
   
   
    p=head;
    p1=p->next;
    while((p->num>p1->num)&&(p1->next!=NULL))
    {
        p2=p1;
        p1=p1->next;
    }
    if(p->num>p1->num)
    {
        temp=p->num;
        p->num=p1->num;
        p1->num=temp;
    }
   
    if(head!=NULL)
        do{
            cout<<p->num<<" "<<endl;
            p=p->next;
        }while(p!=NULL);
       
       
}
想排序输出的话你得再改改
2011-04-01 23:09
vac
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-4-1
收藏
得分:0 
回复 2楼 『点点滴滴』
不行 360都提醒了
2011-04-01 23:19
快速回复:帮帮忙 链表问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026576 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved