链表
<pre name="code" class="cpp">#include<iostream>
using namespace std;
struct stNode
{
int data;
stNode *next;
};
void output(stNode *p,int n)
{
stNode *current = p->next;
for (int i = 0; i < n; i++)
{
cout << current->data;
current = current->next;
}
}
void deleteList(stNode *head)
{
//删除链表,从头开始删除,首先删除头节点,然后依次删除
stNode *current = head;
while (current != NULL)
{
stNode *temp = current->next;//用临时指针变量来储存下个节点指针
delete current;//删除当前节点
current = temp;//将当前指针变量赋值为下一个节点指针
}
cout << "调用了删除链表函数" << endl;
}
int main()
{
stNode *head = new stNode;//建立头节点
head->next = new stNode;//建立第一个节点
stNode *current = head->next;//将当前指针指向第一个节点
for (int i = 0; i < 10; i++)
{
current->data = i;
current->next = new stNode;
current = current->next;//将当前指针指向下一个节点
}
output(head,10);
//deleteList(head);
stNode *current2 = head;
while (current2 != NULL)
{
stNode *temp = current2->next;//用临时指针变量来储存下个节点指针
delete current2;//删除当前节点
current2 = temp;//将当前指针变量赋值为下一个节点指针
}
}