the linklist doesn't work
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include <iostream>
#include <stack>
#include <windows.h>
using namespace std;
typedef struct node
{
int key;
node*next;
node(node*p, int m): next(p),key(m){};
}*linklist,node;
stack<int>liststack;
class link
{
public:
link()
{
p=new node(NULL,0);
n=0;
}
linklist build();
void traverse(node*p);
private:
int n;
linklist p;
};
linklist link::build()
{
cout<<"please input the number n:"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
linklist p1=new node(NULL,0);
int a;
cout<<"please input the node:---"<<endl;
cin>>a;
p1->key=a;
p1->next=p->next;
p->next=p1;
p=p1;
}
return p;
}
void link::traverse(node*p)
{
int i=0;
while(p->next!=NULL)
{
linklist p0=p->next;
int b=p0->key;
cout<<"the---"<<i<<" --value is"<<b;
cout<<endl;
Sleep(100);
liststack.push(b);
p=p->next;
i++;
}
}
int main()
{
link mylink;
linklist q=mylink.build();
mylink.traverse(q);
system("pause");
return 0;
}
| |
I don't know why the
mylink.traverse(q);
doesn't work
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include <iostream>
#include <stack>
#include <windows.h>
using namespace std;
typedef struct node
{
int key;
node*next;
node(node*p, int m): next(p),key(m){};
}*linklist,node;
stack<int>liststack;
class link
{
public:
link()
{
n=0;
}
linklist build(node*m);
void traverse(node*p);
private:
int n;
};
linklist link::build(node*m)
{
m->next=NULL;
m->key=0;
cout<<"please input the number n:"<<endl;
cin>>n;
linklist r=m;
for(int i=0;i<n;i++)
{
linklist p1=new node(NULL,0);
int a;
cout<<"please input the node:---"<<endl;
cin>>a;
p1->key=a;
p1->next=m->next;
r->next=p1;
r=p1;
}
return r;
}
void link::traverse(node*p)
{
int i=0;
while(p->next!=NULL)
{
linklist p0=p->next;
int b=p0->key;
cout<<"the---"<<i<<" --value is"<<b;
cout<<endl;
Sleep(400);
liststack.push(b);
p=p->next;
i++;
}
}
int main()
{
link mylink;
linklist q=mylink.build(q);
mylink.traverse(q);
system("pause");
return 0;
}
| |
This is my new code, which I have modified a little bit, but it keeps printing, I don't know how to correct it
Topic archived. No new replies allowed.