无限循环,从堆栈中删除重复项?
Infinite loop, removing duplicates from a stack?
打印由 removeDuplicates 函数返回的新堆栈时出现无限循环,但我不知道是什么原因造成的?所有其他功能都运行良好。如果元素重复,函数应保留第一次出现。这是代码:
class Stack {
Node *head;
public: Stack() {
head = NULL;
};
~Stack() {
while (head) {
Node * temp = head;
head = head->next;
delete temp;
}
}
void push(int data);
int pop();
bool isEmpty();
void print();
Stack removeDuplicates();
};
void Stack::push(int data) {
Node *temp = new Node(data);
temp->next = head;
head = temp;
}
int Stack::pop() {
if (head != NULL ) {
int x = head->data;
Node *temp = head;
head = head->next;
delete temp;
return x;
} else {
cout << "The stack is empty!";
return -1;
}
}
bool Stack::isEmpty(){
return head == NULL;
}
void Stack::print() {
Node * temp = head;
while(temp != NULL ) {
cout << temp->data << " ";
temp = temp->next;
}
}
Stack Stack::removeDuplicates(){
Stack st;
Node *temp = NULL;
bool flag;
while (head != NULL) {
if (st.head == NULL) {
st.push(head->data);
temp = st.head;
} else {
flag = true;
while (temp != NULL) {
if (head->data == temp->data)
flag = false;
temp = temp->next;
}
if (flag == true)
st.push(head->data);
}
Node *del = head;
head = head->next;
delete del;
}
return st;
}
head 在函数外声明的是什么?
函数外的代码是否修改了head?
如果进入函数时 head 为 null,那么您的函数将不会执行任何操作,并且根据您也没有提供的打印代码,它可能会因为这个原因而永远循环。
在您的第一个 if 语句中,您有 if (st.head == NULL) ... 然后将 temp 设置为 st.head,例如temp = NULL,那么 temp 什么时候可以在 if 语句的 else 分支中使用?
为什么不在函数内显式初始化所有变量,而不仅仅是声明它们并将它们与类型相关联,因为如果它们是堆栈变量,则它们是未定义的。如果您对代码进行格式化,它也有助于理解和维护代码,以便更容易进行可视化解析。您会惊讶地发现,您可以通过该学科发现多少错误。
Stack Stack::removeDuplicates() {
Stack st;
Node *temp = NULL;
bool flag;
while (head != NULL) {
if (st.head == NULL) {
st.push(head->data);
temp = st.head;
} else {
flag = true;
while (temp != NULL) {
if (head->data == temp->data)
flag = false;
temp = temp->next;
}
if (flag)
st.push(head->data);
}
Node *del = head;
head = head->next;
delete del;
}
return st;
}
打印由 removeDuplicates 函数返回的新堆栈时出现无限循环,但我不知道是什么原因造成的?所有其他功能都运行良好。如果元素重复,函数应保留第一次出现。这是代码:
class Stack {
Node *head;
public: Stack() {
head = NULL;
};
~Stack() {
while (head) {
Node * temp = head;
head = head->next;
delete temp;
}
}
void push(int data);
int pop();
bool isEmpty();
void print();
Stack removeDuplicates();
};
void Stack::push(int data) {
Node *temp = new Node(data);
temp->next = head;
head = temp;
}
int Stack::pop() {
if (head != NULL ) {
int x = head->data;
Node *temp = head;
head = head->next;
delete temp;
return x;
} else {
cout << "The stack is empty!";
return -1;
}
}
bool Stack::isEmpty(){
return head == NULL;
}
void Stack::print() {
Node * temp = head;
while(temp != NULL ) {
cout << temp->data << " ";
temp = temp->next;
}
}
Stack Stack::removeDuplicates(){
Stack st;
Node *temp = NULL;
bool flag;
while (head != NULL) {
if (st.head == NULL) {
st.push(head->data);
temp = st.head;
} else {
flag = true;
while (temp != NULL) {
if (head->data == temp->data)
flag = false;
temp = temp->next;
}
if (flag == true)
st.push(head->data);
}
Node *del = head;
head = head->next;
delete del;
}
return st;
}
head 在函数外声明的是什么?
函数外的代码是否修改了head?
如果进入函数时 head 为 null,那么您的函数将不会执行任何操作,并且根据您也没有提供的打印代码,它可能会因为这个原因而永远循环。
在您的第一个 if 语句中,您有 if (st.head == NULL) ... 然后将 temp 设置为 st.head,例如temp = NULL,那么 temp 什么时候可以在 if 语句的 else 分支中使用?
为什么不在函数内显式初始化所有变量,而不仅仅是声明它们并将它们与类型相关联,因为如果它们是堆栈变量,则它们是未定义的。如果您对代码进行格式化,它也有助于理解和维护代码,以便更容易进行可视化解析。您会惊讶地发现,您可以通过该学科发现多少错误。
Stack Stack::removeDuplicates() {
Stack st;
Node *temp = NULL;
bool flag;
while (head != NULL) {
if (st.head == NULL) {
st.push(head->data);
temp = st.head;
} else {
flag = true;
while (temp != NULL) {
if (head->data == temp->data)
flag = false;
temp = temp->next;
}
if (flag)
st.push(head->data);
}
Node *del = head;
head = head->next;
delete del;
}
return st;
}