c++ - Staque程序弹出visual studio错误
问题描述
每当我尝试启动该程序时,它都会告诉我:“Microsoft Visual Studio 存在构建错误。您要继续并运行最后一次成功的构建吗?” 此代码基于的分配:创建一个名为“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(后)删除 2 个偶数和 1 个奇数Staque,然后显示 Staque:由于删除总是遵循 LIFO 顺序,因此要删除的数字是从 Staque 后面开始,首先是 8,然后是 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
,未初始化。
推荐阅读
- android - 如何在 Android Studio 中为 android 2.3 构建应用程序
- python - Python Pandas DataFrame 产生一个 keyerror
- ios - 如何访问导航窗格(左窗格)中创建的文件夹的路径?
- symfony - Symfony3 配置组件验证
- python-3.x - 如何以向后的方式将列表分组为子列表
- python - 使用带有令牌的 Google API [Django & AllAuth]
- php - 如何在子主题中覆盖父customizer.php
- c++ - C++/QT 安全终止阻塞线程
- java - 在不同类中使用带有代码的按钮
- python - 需要 PYTHON JSONtoDICT 帮助 - 似乎 python 正在将我的 json-dictionary 转换解释为列表