斯塔克程序弹出视觉工作室错误



每当我尝试启动程序时,它都会告诉我: "Microsoft Visual Studio 存在构建错误。是否要继续并运行上一个成功的生成? 此代码所基于的赋值: 创建一个名为"Staque"的数据结构,它只能存储整数。斯塔克的工作方式如下:

  1. 如果您尝试存储在 Staque 中的数字是偶数,则会将其推到斯塔克前面
  2. 如果您尝试存储在 Staque 中的数字是奇数,则会将其推送到斯塔克的末尾
  3. 当您尝试从 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: 由于删除始终遵循后进先出顺序,因此要删除的数字首先是 8,然后是 6(2 个偶数)和 9(奇数)。然后,斯塔克应如下所示:(前)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未初始化。

最新更新