链表倒置的问题啊

youzhizhe09 2009-06-08 06:57:40
#define STACK_SIZE 100;
#include <iostream>
#include <string>
using namespace std;
class student_node
{
public:
int number;string name,sex,mark;
student_node*next;

student_node(int num,string na,string s,string ma):number(num),name(na),sex(s),mark(ma){}

void print(){
cout < <number < <'\t' < <name < <'\t' < <sex < <'\t' < <mark < <endl;}
};

typedef struct{
student_node**base;
int top;
int stacksize;//当前已分配的存储空间
}sqstack;

//堆栈初始化
void initstack(sqstack&s){
s.base=new student_node*[100];
s.top=0;
s.stacksize=0;
}
bool push(sqstack&s,student_node*a){
if(s.stacksize>100){cout < <"ERROR" < <endl;return 0;}
s.stacksize++;
s.base[s.top]=a;
s.top++;
return 1;
}


bool pop(sqstack&s,student_node*&a){
if(s.stacksize==0){cout < <"ERROR" < <endl;return 0;}
s.stacksize--;
a=s.base[s.top];
s.top--;
return 1;
}


bool stackempty(sqstack s){
if(s.stacksize>100) return 0;
else return 1;
}


void reverse_list(student_node*& a)
{
student_node *tmp = NULL;
student_node *tmp2 = a;
sqstack s;
initstack(s);

while(tmp2 != NULL)
{
push(s,tmp2);
tmp2 = tmp2->next;
}

pop(s,a);

tmp = a;

while(stackempty(s))
{
pop(s,tmp2);
tmp->next=tmp2;
tmp=tmp->next;
}


}


void create_list(student_node *&a)
{
int n;int num;string s,ma,na;
cout < <"how many students?" < <endl;
cin>>n;
cout < <"for the first student's information" < <endl;
cin>>num>>na>>s>>ma;
a=new student_node(num,na,s,ma);
student_node *p;
student_node *t=a;

for(int i=2;i <=n;i++)
{ cout < <"for the next student's information" < <endl;
cin>>num>>na>>s>>ma;
p=new student_node(num,na,s,ma);
t->next=p;t=p;
}
t->next=NULL;
}


void print_list(student_node *a)
{
while(a!=NULL)
{

a->print();
a=a->next;
}

}

int main()
{
student_node*head=NULL;
create_list(head);
print_list(head);

reverse_list(head);
print_list(head);
return 0;
}


要用堆栈实现,编译通过,运行错误,希望高手指教
...全文
45 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
日光降临 2009-06-11
  • 打赏
  • 举报
回复

问题出在你的出栈函数.
把你的程序简单修改了下, 你自己看看.
#include <iostream>
#include <string>
#define STACK_SIZE 100;


using namespace std;
class student_node
{
public:
int number;string name,sex,mark;
student_node *next;

student_node(int num,string na,string s,string ma):number(num),name(na),sex(s),mark(ma){}

void print()
{
cout<<number <<"\t" <<name <<"\t" <<sex <<"\t" <<mark <<endl;
}
};

typedef struct
{
student_node**base;
int top;
int stacksize;
}sqstack;


void initstack(sqstack &s)
{
s.base=new student_node* [100];
s.top=0;
s.stacksize=0;
}

int push(sqstack&s, student_node*a)
{
if(s.stacksize >= 100)
{
cout <<"ERROR: the stack is full!" <<endl;
return -1;
}
s.stacksize++;//所有元素入栈后, top最后还要加 1, 所以s.base[s.top] == NULL!
//所以如果要出栈, 记得首先top减 1,

s.base[s.top] = a;
s.top++;
return 0;
}

int pop(sqstack&s,student_node*&a)
{
if(s.stacksize==0)
{
cout <<"ERROR: the stack is empty!" <<endl;
return -1;
}
s.stacksize--;
s.top--;//问题就出在这两句, 现在这样是正确的, 要先减 1, 否则s.base[s.top] == NULL
//也就会出现你说的错误了.
a = s.base[s.top];
return 0;
}

int stackempty(sqstack s)
{
if(s.stacksize > 100)
return 0;
else
return -1;
}


void reverse_list(student_node*& a)
{
student_node *tmp = NULL;
student_node *tmp2 = a;

sqstack s;
initstack(s);

while(tmp2 != NULL)
{
push(s,tmp2);
tmp2 = tmp2->next;
}
cout <<s.stacksize <<endl;
cout <<"111111" <<endl;
pop(s,a);//如果出栈函数不先减 1 的话, 第一次pop 得到的a 就是NULL
tmp = a;

cout <<"222222" <<endl;

cout <<s.stacksize <<endl;
while(s.stacksize!=0)
{
pop(s,tmp2);
cout <<"333333" <<endl;
tmp->next=tmp2;//那么这里也就会出错了
cout <<"444444" <<endl;
tmp=tmp->next;
}

tmp->next=NULL;
}


void create_list(student_node *&a)
{
int n;int num;string s,ma,na;
cout <<"how many students?" <<endl;
cin>>n;
cout <<"for the first student's information" <<endl;
cin>>num>>na>>s>>ma;
a=new student_node(num,na,s,ma);
student_node *p;
student_node *t=a;

for(int i = 2; i <= n; i++)
{
cout <<"for the next student's information" <<endl;
cin>>num>>na>>s>>ma;
p=new student_node(num,na,s,ma);
t->next=p;t=p;
}
t->next=NULL;
}


void print_list(student_node *a)
{
while(a!=NULL)
{
a->print();
a=a->next;
}
}

int main()
{
student_node*head=NULL;
create_list(head);
print_list(head);

reverse_list(head);
print_list(head);
return 0;
}

na2650945 2009-06-08
  • 打赏
  • 举报
回复
能把问题缩小点么。
具体指出问题的类型。
然后代码要缩进。
用代码对话框发。
youzhizhe09 2009-06-08
  • 打赏
  • 举报
回复
哭了
youzhizhe09 2009-06-08
  • 打赏
  • 举报
回复
谁来帮帮我啊

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧