我想建立一個單向鏈表
問題報錯:
使用了未初始化的記憶體p;
使用了未初始化的區域變數p;
請問我應該怎么修改?
#include<iostream>
#include<vector>
using namespace std;
struct Node
{
double data;
Node * next;
};
void CreateList(Node*& head)
{
Node *s, *p;
s = new Node;
cin >> s->data;
while (s->data!= 0)
{
if (head == NULL)
{
head = s;
}
else
{
p->next = s;
p = s;
s = new Node;
cin >> s->data;
}
}
p->next = NULL;
delete s;
return;
}
void ShowNode(Node *head)
{
while (head)
{
cout << head->data << "\t";
head = head->next;
}
cout << endl;
}
int main()
{
Node* head = NULL;
CreateList(head);
ShowNode(head);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/223335.html
標籤:C++ 語言
