#include<iostream.h>struct LinkNode{
int data;
LinkNode* link;
LinkNode(LinkNode* ptr = NULL){link=ptr;}
LinkNode(const int& item, LinkNode* ptr=NULL)
{data = item; link = ptr;}
};
class List
{
public:
bool Union(List& L,List& L1);
List(){first = new LinkNode;}
List(const int& x){first = new LinkNode(x);}
List(List& L);
~List(){MakeEmpty();}
void MakeEmpty();
bool finddouble(int m, int& count);
int Length()const;
LinkNode* GetHead()const{return first;}
void SetHead(LinkNode* p){first=p;}
LinkNode* Search(int& x);
LinkNode* Locate(int i);
int* GetData(int i);
void SetData(int i,int& x);
bool Insert(int i,int& x);
bool Remove(int i, int& x);
bool IsEmpty()const
{return first->link ==NULL?true:false;}
bool IsFull()const {return false;}
void Sort();
void InputFront(char endFlag);
void OutPut();
private:
LinkNode* first;
};
LinkNode* List::Locate (int i)
{
if(i<0) return NULL;
LinkNode* current=first; int k=0;
while(current!=NULL&&k<i)
{
current=current->link ;k++;
} return current;
};
bool List::Union (List& L,List& L1)
{ int n=L.Length ();
//int m=L1.Length ();
LinkNode* Llastnode=L.Locate (n);
LinkNode* Lfirstnode=L1.GetHead ();
Llastnode->link =Lfirstnode;
return true;
};
void List::InputFront(char endFlag)
{
LinkNode* newNode ,* last; int val;
first = new LinkNode;
if(first == NULL)
{cerr<<"存储分配失败!"<<endl;}
cin >> val; last=first;
while(val !=endFlag)
{
newNode = new LinkNode(val);
if(newNode == NULL)
{cout<<"存储分配失败!"<<endl;}
last->link =newNode; last=newNode;
cin >> val;
};
void List::OutPut ()
{
LinkNode* current=first->link ;
while(current!=NULL)
{
cout<<current->data <<endl;
current=current->link ;
}
};
void main()
{
int m=3,n=0;
LinkNode node;
List list,list1;
char c='\0';
cout<<"请输入链表L中的元素:"<<endl;
list.InputFront(c);
list.OutPut();
//list.Remove(3,n);
//cout<<"删除的元素是:"<< n<<endl;//list.finddouble (list.Length (),n); //n必须初始化为0,用来计重复元素的个数//cout<<"删除重复的元素后的链表为:"<<endl;
//list.OutPut ();
//cout<<"重复元素的个数:"<<n<<"个"<<endl;
cout<<"请输入链表L1中的元素:"<<endl;
list1.InputFront (c);
list1.OutPut();
list.Union (list,list1);
list.OutPut();
}输入两个链表 然后把他们合并
但是输入第一个链表后 就不能输入第二链表了
关闭应用程序的时候机子运行变慢
请高手指点一下 这里是个学习的好地方
马上给分
int data;
LinkNode* link;
LinkNode(LinkNode* ptr = NULL){link=ptr;}
LinkNode(const int& item, LinkNode* ptr=NULL)
{data = item; link = ptr;}
};
class List
{
public:
bool Union(List& L,List& L1);
List(){first = new LinkNode;}
List(const int& x){first = new LinkNode(x);}
List(List& L);
~List(){MakeEmpty();}
void MakeEmpty();
bool finddouble(int m, int& count);
int Length()const;
LinkNode* GetHead()const{return first;}
void SetHead(LinkNode* p){first=p;}
LinkNode* Search(int& x);
LinkNode* Locate(int i);
int* GetData(int i);
void SetData(int i,int& x);
bool Insert(int i,int& x);
bool Remove(int i, int& x);
bool IsEmpty()const
{return first->link ==NULL?true:false;}
bool IsFull()const {return false;}
void Sort();
void InputFront(char endFlag);
void OutPut();
private:
LinkNode* first;
};
LinkNode* List::Locate (int i)
{
if(i<0) return NULL;
LinkNode* current=first; int k=0;
while(current!=NULL&&k<i)
{
current=current->link ;k++;
} return current;
};
bool List::Union (List& L,List& L1)
{ int n=L.Length ();
//int m=L1.Length ();
LinkNode* Llastnode=L.Locate (n);
LinkNode* Lfirstnode=L1.GetHead ();
Llastnode->link =Lfirstnode;
return true;
};
void List::InputFront(char endFlag)
{
LinkNode* newNode ,* last; int val;
first = new LinkNode;
if(first == NULL)
{cerr<<"存储分配失败!"<<endl;}
cin >> val; last=first;
while(val !=endFlag)
{
newNode = new LinkNode(val);
if(newNode == NULL)
{cout<<"存储分配失败!"<<endl;}
last->link =newNode; last=newNode;
cin >> val;
};
void List::OutPut ()
{
LinkNode* current=first->link ;
while(current!=NULL)
{
cout<<current->data <<endl;
current=current->link ;
}
};
void main()
{
int m=3,n=0;
LinkNode node;
List list,list1;
char c='\0';
cout<<"请输入链表L中的元素:"<<endl;
list.InputFront(c);
list.OutPut();
//list.Remove(3,n);
//cout<<"删除的元素是:"<< n<<endl;//list.finddouble (list.Length (),n); //n必须初始化为0,用来计重复元素的个数//cout<<"删除重复的元素后的链表为:"<<endl;
//list.OutPut ();
//cout<<"重复元素的个数:"<<n<<"个"<<endl;
cout<<"请输入链表L1中的元素:"<<endl;
list1.InputFront (c);
list1.OutPut();
list.Union (list,list1);
list.OutPut();
}输入两个链表 然后把他们合并
但是输入第一个链表后 就不能输入第二链表了
关闭应用程序的时候机子运行变慢
请高手指点一下 这里是个学习的好地方
马上给分
解决方案 »
- 为什么在函数调用前需要使用cout 数据才正常
- 关于CImageList::Create
- VC从FTP下载文件后有必要检查完整性吗?怎么做?
- The validator XML Schema Validator is a deleageting validator but no delegate can found for it.
- 萬歲!MSDN雜誌有中文版啦!
- 想针对ActiveX控件进行学习但不知该看什么书
- 怎么实现拔号网络断了之后重拔?
- msdn的例子为什么 不能运行??
- 帮我看看这是什么错误啊?如何解决啊?
- 连接数据库出错了:<
- 怎样去掉窗口边框?
- 用get_innerHTML获得内容后再调用put_innerHTML原封不动回回网页,发现网页中的js控件不能用了,请问谁知道这是怎么回事?
List::Leghth()的实现呢?
MakeEmpty()的实现呢?
#include<iostream.h>struct LinkNode{
int data;
LinkNode* link;
LinkNode(LinkNode* ptr = NULL){link=ptr;}
LinkNode(const int& item, LinkNode* ptr=NULL)
{data = item; link = ptr;}
};
class List
{
public:
bool Union(List& L,List& L1);
List(){first = new LinkNode;}
List(const int& x){first = new LinkNode(x);}
List(List& L);
~List(){MakeEmpty();}
void MakeEmpty();
bool finddouble(int m, int& count);
int Length()const;
LinkNode* GetHead()const{return first;}
void SetHead(LinkNode* p){first=p;}
LinkNode* Search(int& x);
LinkNode* Locate(int i);
int* GetData(int i);
void SetData(int i,int& x);
bool Insert(int i,int& x);
bool Remove(int i, int& x);
bool IsEmpty()const
{return first->link ==NULL?true:false;}
bool IsFull()const {return false;}
void Sort();
void InputFront(char endFlag);
void OutPut();
private:
LinkNode* first;
};
LinkNode* List::Locate (int i)
{
if(i<0) return NULL;
LinkNode* current=first; int k=0;
while(current!=NULL&&k<i)
{
current=current->link ;k++;
} return current;
};
void List::MakeEmpty(){
LinkNode* p;
while(first->link !=NULL)
{
p = first ->link;
first->link = p->link;
delete p;
}
};int List::Length ( ) const
{
//求单链表的长度
LinkNode*p = first->link;
//检测指针p指示第一个结点
int count = 0;
while ( p != NULL ) { //逐个结点检测
p = p->link; count++;
}
return count;
};
bool List::Insert(int i,int& X)
{
LinkNode* current = Locate(i);
if( current ==NULL) return false;
LinkNode* newNode = new LinkNode(X);
if(newNode == NULL)
{cerr<<"存储分配失败!"<<endl; }
newNode->link = current ->link;
current ->link=newNode;
return true;
}; bool List::Remove(int i,int& X)//将链表中第i个元素删掉
{
LinkNode* current = Locate(i-1);
if( current ==NULL|| current->link==NULL) return false;
LinkNode* del = current ->link ;
current ->link = del->link;
X= del->data;
delete del;
return true;
};
void List::OutPut ()
{
LinkNode* current=first->link ;
while(current!=NULL)
{
cout<<current->data <<endl;
current=current->link ;
}
}; void List::InputFront(char endFlag)
{
LinkNode* newNode ,* last; int val;
first = new LinkNode;
if(first == NULL)
{cerr<<"存储分配失败!"<<endl;}
cin >> val; last=first;
while(val !=endFlag)
{
newNode = new LinkNode(val);
if(newNode == NULL)
{cout<<"存储分配失败!"<<endl;}
last->link =newNode; last=newNode;
cin >> val;
}
if(val =endFlag)
cout<<val<<"是结束元素输入的标志"<<endl;
};
bool List::finddouble(int n,int& count) // 找到重复的元素并删除它
{
int i=1,j=0;
int m=n;
for(i=1; i<m; i++)
for(j=i+1;j<=m;j++)
{
LinkNode* current=Locate(i);
LinkNode* del=Locate(j);
if(del==NULL|| current==NULL) return false;
if (current->data==del->data)
{
LinkNode* find= Locate(j-1);
find->link =del->link ;
delete del;
m=m-1;
count++;
}
}
return true;
};
bool List::Union (List& L,List& L1)
{ int n=L.Length ();
//int m=L1.Length ();
LinkNode* Llastnode=L.Locate (n);
LinkNode* Lfirstnode=L1.GetHead ();
Llastnode->link =Lfirstnode;
return true;
};
void main()
{
int m=3,n=0;
LinkNode node;
List list,list1;
char c='\0';
cout<<"请输入链表L中的元素:"<<endl;
list.InputFront(c);
list.OutPut();
//list.Remove(3,n);
//cout<<"删除的元素是:"<< n<<endl;//list.finddouble (list.Length (),n); //n必须初始化为0,用来计重复元素的个数//cout<<"删除重复的元素后的链表为:"<<endl;
//list.OutPut ();
//cout<<"重复元素的个数:"<<n<<"个"<<endl;
cout<<"请输入链表L1中的元素:"<<endl;
list1.InputFront (c);
list1.OutPut();
list.Union (list,list1);
list.OutPut();
}
代码如下:
#include <iostream.h> struct LinkNode{
int data;
LinkNode* link;
LinkNode(LinkNode* ptr = NULL){link=ptr;}
LinkNode(const int& item, LinkNode* ptr=NULL)
{data = item; link = ptr;}
};
class List
{
public:
bool Union(List& L,List& L1);
List()
{/*first = new LinkNode;*/}
List(const int& x){first = new LinkNode(x);}
List(List& L);
~List(){/*MakeEmpty();*/}
// void MakeEmpty();
bool finddouble(int m, int& count);
int Length();
LinkNode* GetHead()const{return first;}
void SetHead(LinkNode* p){first=p;}
LinkNode* Search(int& x);
LinkNode* Locate(int i);
int* GetData(int i);
void SetData(int i,int& x);
bool Insert(int i,int& x);
bool Remove(int i, int& x);
bool IsEmpty()const
{return first->link ==NULL?true:false;}
bool IsFull()const {return false;}
void Sort();
void InputFront(int endFlag);
void OutPut();
private:
LinkNode* first;
};
LinkNode* List::Locate (int i)
{
if(i <0) return NULL;
LinkNode* current=first; int k=0;
while(current!=NULL&&k <i)
{
current=current->link ;k++;
} return current;
}
int List::Length()
{
int i=0;
LinkNode *current=first;
while(current!=NULL)
{
i++;
current=current->link;}
return i;}
bool List::Union (List& L,List& L1)
{ int n=L.Length ();
//int m=L1.Length ();
LinkNode* Llastnode=L.Locate (n);
LinkNode* Lfirstnode=L1.GetHead ();
Llastnode->link =Lfirstnode;
return true; }void List::InputFront(int endFlag)
{
LinkNode* newNode ,* last; int val;
first = new LinkNode;
if(first == NULL)
{cerr <<"存储分配失败!" <<endl;}
cin >> val; last=first;
while(val !=endFlag)
{
newNode = new LinkNode(val);
if(newNode == NULL)
{cout <<"存储分配失败!" <<endl;}
last->link =newNode; last=newNode;
cin >> val;
}}
void List::OutPut()
{
LinkNode* current=first->link;
while(current!=NULL)
{
cout <<current->data <<endl;
current=current->link ;
}
}
void main()
{
int m=3,n=0;
LinkNode node;
List list,list1;
int c=-1;
cout <<"请输入链表L中的元素:" <<endl;
list.InputFront(c);
list.OutPut();
cout <<"请输入链表L1中的元素:" <<endl;
list1.InputFront(c);
list1.OutPut();
list.Union(list,list1);
list.OutPut();
}
{
LinkNode* newNode ,* last; int val;
first = new LinkNode;
if(first == NULL)
{cerr < <"存储分配失败!" < <endl;}
cin >> val; last=first;
while(val !=endFlag)
{
newNode = new LinkNode(val);
if(newNode == NULL)
{cout < <"存储分配失败!" < <endl;}
last->link =newNode; last=newNode;
cin >> val;
}; 你这段代码很多问题啊。1. 节点中的值全部一样的。
——cin >> val; last=first; 放在WHILE
2. 你的结点没连成一个链表啊。