这个软件可以运行链表么
c4droid吧
全部回复
仅看楼主
level 3
buxhbdi 楼主
我编了一个链表,通过编译,但是一运行就这样
2016年07月24日 06点07分 1
level 3
buxhbdi 楼主
代码如下#include <iostream>
using namespace std;
struct Node{
int Data;
Node*next;
};
class list{
Node*head;
public:
list(){
head=NULL;
}
void insertlist(int aData,int bData);
void deletelist(int aData);
void outputlist();
Node*gethead(){return head;}
};
void list::insertlist(int aData,int bData){
Node*p,*q,*s;
s=(Node*)new(Node);
s->Data=bData;
p=head;
if(head==NULL){
head=s;
s->next=NULL;
}
else if(p->Data==aData){
head=s;
s->next=p;
}
else{
while(p->Data!=aData&&p->next!=NULL){
q=p;
p=p->next;
}
if(p->Data==aData){
q->next=s;
s->next=p;
}
else{
q->next=s;
s->next=NULL;
}
}
}
void list::deletelist(int aData){
Node*p,*q;
p=head;
if(head==NULL)
return;
else if(p->Data==aData){
q->next=p->next;
delete p;
}
else{
while(p->Data!=aData&&p->next!=NULL){
q=p;
p=p->next;
}
if(p->Data==aData){
q->next=p->next;
delete p;
}
}
}
void list::outputlist(){
Node*current;
current=head;
while(current->next!=NULL){
cout<<current->Data<<" ";
current=current->next;
}
cout<<endl;
}
int main()
{
list A,B;
int Data[10]={25,41,16,98,5,67,9,55,1,121};
A.insertlist(0,Data[0]); //建立链表A首结点
for(int i=1;i<10;i++)
A.insertlist(0,Data[i]); //顺序向后插入
cout<<"\n链表A:";
A.outputlist();
A.deletelist(Data[7]);
cout<<"删除元素Data[7]后";
A.outputlist();
B.insertlist(0,Data[0]); //建立链表B首结点
for(int i=0;i<10;i++)
B.insertlist(B.gethead()->Data,Data[i]); //在首结点处顺序向后插入
cout<<"\n链表B:";
B.outputlist();
B.deletelist(67);
cout<<"删除元素67后";
B.outputlist();
}
2016年07月24日 06点07分 2
1