标题:刚刚学习数据结构,写了个链表的基本程序,到处都是错,求大虾指教(真心想 ...
只看楼主
小灰i小白
Rank: 1
等 级:新手上路
帖 子:19
专家分:5
注 册:2011-11-12
结帖率:50%
 问题点数:0 回复次数:3 
刚刚学习数据结构,写了个链表的基本程序,到处都是错,求大虾指教(真心想学啊)
#include<iostream>
#include<stdlib.h>
using namespace std;
class LinkNode{
  public:
        int data;
        LinkNode *link;
};
class List:public LinkNode{
  private:
          LinkNode *first;
  public:
         bool Insert(int i,int& x);           //插入一个节点
         void inputRear();                   //建立链表
         void output();                      // 输出
         };
bool List::Insert(int i,int& x){
     if(first==NULL || i==0){                 //表头      
           LinkNode *newNode=new LinkNode(x);
           if(newNode==NULL){cout<<"No\n";exit(1);}
           newNode->link=first;first=newNode;        
           }
     else {                                 
            LinkNode *current=first;
            for(int k=1;k<i;k++){
                if(current==NULL) break;
                else current=current->link;
            }
            if(newnode==NULL && first!=NULL){cout<<"No\n";return false;}
            else{
                 LinkNode *newNode=new LinkNode(x);
                 if(newNode==NULL){cout<<"No\n";exit(1);}
                 newNode->link=current->link;
                 current->link=newNode;
                 }
            }
            return true;
};
void List::inputRear(){
     LinkNode *last;
     int val;
     LinNode *newNode=new LinkNode(x);   //创建一个新节点
     first=last=newNode;
     if(first==NULL){cout <<"NO";exit(1);}
       cin>>val;
       while(val!=END){
          LinkNode*newNode=new LinkNode(val);
          last->link=newNode;
          last=newNode;
          cin>>val;
          }
};
void List::output(){
     LinkNode *current1=first->link;
     while(current1!=NULL){
        cout<<current1->data<<endl;
        current1=current1->link;
        }
};
int main(){
    List A;
    A.intputRear();
    A.output();
    A.Insert(3,5);
    A.output();
    system("pause");
    return 0;
}
            
 PS:以前基础不好,但真心想学习,求指教,谢谢哈...           
            

[ 本帖最后由 小灰i小白 于 2011-11-19 17:19 编辑 ]
搜索更多相关主题的帖子: private include public 
2011-11-19 16:19
lengji22
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-9-2
得分:0 
#include<iostream>
#include<stdlib.h>
using namespace std;
class LinkNode{
public:
    int data;
    LinkNode *link;
    LinkNode(int x){data = x;}
    LinkNode(){}
};
class List{
private:
    LinkNode *first;
public:
    bool Insert(int i,int x);           //插入一个节点
    void inputRear();                   //建立链表
    void output();                      // 输出
    List(){first = new LinkNode;}
    virtual ~List(){
        if (first !=NULL)
        {
            LinkNode *cur = first->link;
            delete first;
            first = cur;

        }
    }
};
bool List::Insert(int i,int x){
    if(first==NULL || i==0){                 //表头      
        LinkNode *newNode=new LinkNode(x);
        if(newNode==NULL){cout<<"No\n";exit(1);}        
        newNode->link=first;first=newNode;        
    }
    else {                                 
        LinkNode *current=first;
        for(int k=1;k<i;k++){
            if(current==NULL) break;
            else current=current->link;
        }
        if(current==NULL && first!=NULL){cout<<"No\n";return false;}
        else{
            LinkNode *newNode=new LinkNode(x);
            if(newNode==NULL){cout<<"No\n";exit(1);}
            newNode->link=current->link;
            current->link=newNode;
        }
    }
    return true;
};
void List::inputRear(){
    LinkNode *last;
    int val;
    //LinkNode *newNode=new LinkNode;   //创建一个新节点
    last = first;   
    //if(first==NULL){cout <<"NO";exit(1);}
    cout<<"请输入链表数据:";
    cin>>val;
    while(val!='\n'){
        LinkNode*newNode=new LinkNode(val);
        last->link=newNode;
        last=newNode;
        cout<<"请输入链表数据:";
        cin>>val;
    }
};
void List::output(){
    LinkNode *current1=first->link;
    while(current1!=NULL){
        cout<<current1->data<<endl;
        current1=current1->link;
    }
};
int main(){
    List A;
    A.inputRear();
    A.output();
    A.Insert(3, 5);
    A.output();
    system("pause");
    return 0;
}



2011-11-20 21:15
小灰i小白
Rank: 1
等 级:新手上路
帖 子:19
专家分:5
注 册:2011-11-12
得分:0 
回复 2楼 lengji22
谢谢哈,但是运行结果怎么回事啊
2011-11-22 20:23
小灰i小白
Rank: 1
等 级:新手上路
帖 子:19
专家分:5
注 册:2011-11-12
得分:0 
回复 2楼 lengji22
我错在哪里啊
2011-11-22 20:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-355686-1-1.html




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

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