问题描述
每当我尝试启动该程序时,它都会告诉我: “微软视觉工作室 存在构建错误。您想继续并运行上次成功的构建吗?” 此代码基于的分配: 创建一个名为“Staque”的数据结构,它只能存储整数。 Staque的工作方式如下:
- 如果您尝试存储在 Staque 中的数字是偶数,则它会被推到 Staque 的前面
- 如果您尝试存储在 Staque 中的数字是奇数,则它会被推送到 Staque 的末尾
- 当您尝试从 Staque 中删除一个数字时,您总是按照 LIFO 规则从 Staque 的前面或后面进行操作。 编写 C++ 代码来实现 Staque。由于数据结构都是关于插入和删除数字的,因此使用链表来实现 Staque 将是一个不错的选择。您的用户界面应该是这样的: 在 Staque 中插入数字 1、3、2、4、6、8 9。 显示 Staque:这就是 Staque 的样子,因为上面的数字是按照上面给出的顺序推入 Staque 的:(前)8 6 4 2 1 3 9(后) 从 Staque 中删除 2 个偶数和 1 个奇数,然后显示 Staque: 由于删除始终遵循 LIFO 顺序,因此要删除的数字首先是 8,然后是从 Staque 后面的 6(2 个偶数)和 9(奇数)。 Staque 将看起来像:(前)4 2 1 3(后)。 为至少 3 个不同的输入系列和相应的 3 个不同的移除系列运行您的程序。
这是我的代码:
'
#include<iostream>
#include<cstdlib>
using namespace std;
struct node {
int info;
struct node* next;
};
class Staque {
private:
struct node* head;
int size;
public:
struct node* createNewNode(int);
void insertAtFront(int);
void insertAtLast(int);
void deleteFromFront();
void deleteFromLast();
void displayList();
Staque() {
head = NULL;
size = 0;
}
};
struct node* Staque::createNewNode(int value) {
struct node* temp;
temp = new(struct node);
temp->info = value;
temp->next = NULL;
return temp;
}
void Staque::insertAtFront(int value) {
struct node* temp,* p;
temp = createNewNode(value);
if (head == NULL) {
head = temp;
head->next = NULL;
}
else {
p = head;
head = temp;
head->next = p;
}
cout << "\nElement inserted at front successfully.";
size++;
}
void Staque::insertAtLast(int value) {
struct node* temp,* s;
temp = createNewNode(value);
if (head == NULL) {
head = temp;
head->next = NULL;
}
else {
s = head;
while (s->next != NULL) {
s = s->next;
}
temp->next = NULL;
s->next = temp;
}
cout << "\nElement inserted at end successfully.";
size++;
}
void Staque::deleteFromFront() {
if (size == 0)
return;
struct node* s;
s = head;
if (head == NULL) {
cout << "\nThe staque is Empty";
return;
}
if (s->info % 2 == 0) {
head = head->next;
free(s);
size--;
cout << "\nEven element deleted.";
if (size == 0)
head = NULL;
}
}
void Staque::deleteFromLast() {
if (size == 0)
return;
struct node* s,* temp;
s = head;
if (head == NULL) {
cout << "\nThe staque is Empty";
return;
}
while (s->next != NULL) {
temp = s;
s = s->next;
}
if (s->info % 2 != 0) {
temp->next = NULL;
free(s);
size--;
cout << "\nOdd element deleted";
if (size == 0)
head = NULL;
}
}
void Staque::displayList() {
struct node* temp;
if (head == NULL) {
cout << "\nThe staque is Empty";
return;
}
temp = head;
cout << "\nElements of staque are: ";
while (temp != NULL) {
cout << temp->info << " ";
temp = temp->next;
}
cout << endl;
}
//main function
int main() {
int choice,value;
Staque sq;
while (1) {
cout << endl << "\nMenu:";
cout << "\n1.Insert ";
cout << "\n2.Delete even number";
cout << "\n3.Delete odd number";
cout << "\n4.display staque";
cout << "\n5.Exit " << endl;
cout << "\nEnter choice : ";
cin >> choice;
switch (choice) {
case 1:
cout << "\nEnter integer to insert: ";
cin >> value;
if (value % 2 != 0) {
sq.insertAtLast(value);
}
else {
sq.insertAtFront(value);
}
break;
case 2:
sq.deleteFromFront();
break;
case 3:
sq.deleteFromLast();
break;
case 4:
sq.displayList();
break;
case 5:
exit(0);
}
}
return 0;
}
'
error C4703: potentially uninitialized local pointer variable 'temp' used
1>Done building project "Staque.vcxproj" -- Failed.
========== Build: 0 succeeded,1 Failed,0 up-to-date,0 skipped ==========
解决方法
替换
while (s->next != NULL) {
temp = s;
s = s->next;
}
与
do {
temp = s;
s = s->next;
} while (s != nullptr);
否则 Staque::deleteFromLast()
不能删除列表中的单个元素的奇数元素。此外,temp
未初始化。