65,211
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class list
{
public:
Node *head;
public:
list()
{
head=NULL;
}
list Create(list &L);
void Output(list &L);
int Insert(list &L,int i,Node e);
Node Erase(list &L,int i,Node e);
};
list Create(list &L)
{
L.head=new Node;
int x;
do
{
cout<<"Please input the number(-999 quit)"<<endl;
cin>>x;
Node *p=new Node;
p->data=x;
p->next=L.head->next;
L.head->next=p;
} while (x!=-999);
cout<<"成功建立链表"<<endl;
return L;
}
void Output(list &L)
{
while(L.head->next!=NULL)
{
cout<<L.head->data<<" ";
L.head->next=L.head->next->next;
}
cout<<endl;
}
int main()
{
list L;
Create(L);
Output(L);
return 0;
}
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class list
{
public:
Node *head;
public:
list()
{
head=NULL;
}
list Create(list &L);
void Output(list &L);
int Insert(list &L,int i,Node e);
Node Erase(list &L,int i,Node e);
};
list Create(list &L)
{
L.head=new Node;
L.head->next = NULL;//这里
int x;
do
{
cout<<"Please input the number(-999 quit)"<<endl;
cin>>x;
Node *p=new Node;
p->data=x;
p->next=L.head->next;
L.head->next=p;
} while (x!=-999);
cout<<"成功建立链表"<<endl;
return L;
}
void Output(list &L)
{
while(L.head->next!=NULL)
{
cout<<L.head->next->data<<" ";//这里
L.head->next=L.head->next->next;
}
cout<<endl;
}
int main()
{
list L;
Create(L);
Output(L);
return 0;
}
//和预期结果一样.如果不想要那个-999,自己改改
d:\>a
Please input the number(-999 quit)
1
Please input the number(-999 quit)
2
Please input the number(-999 quit)
3
Please input the number(-999 quit)
4
Please input the number(-999 quit)
5
Please input the number(-999 quit)
6
Please input the number(-999 quit)
7
Please input the number(-999 quit)
8
Please input the number(-999 quit)
9
Please input the number(-999 quit)
-999
成功建立链表
-999 9 8 7 6 5 4 3 2 1