应该是小白问题
数据结构的作业,将一个链式堆栈里的元素倒置,不知道怎么就是报错#include<iostream>
using namespace std;
typedef struct Lnode { //链表节点
int data;
Lnode *next;
}Lnode;
class Llist { //链表类
private:
Lnode *head, *tail;
public:
Llist(int x);
void ReverseLinkList();
void show();
};
Llist::Llist(int x) {
Lnode *nitem;
head = new Lnode;
head->next = NULL;
tail = head;
for (int i = 1; i <= x; i++) {
nitem = new Lnode();
nitem->data = i;
tail->next = nitem;
tail = nitem;
}
}
void Llist::show() {
Lnode *p = head->next;
while (p != NULL) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
void Llist::ReverseLinkList() { //将第一个节点提取出来插入尾部,直到原本的尾节点为止
if (head->next = NULL) {
cout << "链表为空" << endl; return;
}
Lnode *tem = head->next, *ptail = tail;
while (tem != ptail) {
getchar();
head->next = tem->next;
tail->next = tem;
tail = tem;
tem = head->next;
}
}
int main()
{
Llist L1(5);
L1.show();
L1.ReverseLinkList();
L1.show();
return 0;
}
求救!