#include<iostream>
using namespace std;
struct node
{
int data;
node* link;
};
//初始化
node* initll()
{
node* temp = new node;
temp->link = NULL;
temp->data = 0;//头结点的数据域表示链表的长度
return temp;
}
//输出链表
void output(node* tou)
{
cout << "当前链表长度为:" << tou->data << endl;
if (tou->data > 0)
{
cout << "当前链表内容为:";
node* temp = tou->link;
for (int i = 0; i < tou->data; i++)
{
cout << temp->data << ","<<endl;
temp = temp->link;
}
}
}
//单链表的插入(书上的步骤)
void insert(node* tou,int pos,int value)
{
node* p = new node;
p->data = value;
node* s = tou;
//定位
for (int i = 1; i < pos; i++)
{
s = s->link;
}
p->link = s->link;
s->link = p;
tou->data++;
}
//pos--位置
//value--保存被删除的数值
//删除链表
void deletell(node* tou,int pos,int value)
{
node* p = tou;
for (int i = 1; i < pos; i++)
{
p = p->link;
}
node* temp = p->link;
value = temp->data;
p->link = temp->link;
delete temp;
tou->data--;
}
//清空链表的长度
void clear(node* tou)
{
if (tou->data > 0)
{
int value=0;
int length = tou->data;
for (int i = 0; i < length; i++)
{
deletell(tou, 1, value);
}
}
}
void main()
{
node* head;
head = initll();
output(head);
int value;
for (int i = 0; i < 50; i++)
{
deletell(head, i, value);
cout << "删除:" << value << endl;
}
cout << "链表长度为:" << head->data << endl;
output(head);
clear(head);
output(head);
cout << endl;
}
在运行过程中程序报错说是主函数中deletell(head, i, value);中的value没有初始化,我都初始化为0程序又运行中止,我应该怎么改?