废话不多说,直接码,设置函数比较多,比较绕,因为后面还想增加数据结构的一些功能
#include <iostream>
using namespace std;struct node
{
int data;
node* nextnode;
};node* headinsert(node* head)
{
int i;
for (i = 0; i < 3; i++)
{
node* point = new node;
point->data = i;
point->nextnode = head;
head = point;
}
node* ptr = head;
while (ptr != nullptr)
{
cout << ptr->data << endl;
ptr = ptr->nextnode;
}
system("pause");
return head;
}node* tailinsert(node* head)
{
node* tail = head;
int i;
for (i = 0; i < 3; i++)
{
node* point = new node;
point->data = i;
point->nextnode = nullptr;
tail->nextnode = point;
tail = point;
}
node* ptr = head->nextnode;
while (ptr != nullptr)
{
cout << ptr->data << endl;
ptr = ptr->nextnode;
}
system("pause");
return head;
}void deleted(node*& head)
{
node* ptr = head;
while (ptr != nullptr)
{
node* next = ptr->nextnode;
delete ptr;
ptr = next;
}
head = nullptr;
cout << "success for delete" << endl;
system("pause");
}void creatlist()
{
node* head = nullptr;
flag:
cout << "please put your option" << endl;
cout << "'1' use headinsert" << endl;
cout << "'2' use tailinsert" << endl;
cout << "'3' delete list" << endl;
cout << "'4' end" << endl;
int a;
cin >> a;
switch (a)
{
case 1:
{
if (head != nullptr) // 如果链表还存在,删除它
{
deleted(head);
}
head = headinsert(head); // 使用已经创建的head变量
system("cls");
goto flag;
}
break;
case 2:
{
if (head != nullptr) // 如果链表还存在,删除它
{
deleted(head);
}
head = new node;
head->nextnode = nullptr;
head = tailinsert(head); // 使用已经创建的head变量
system("cls");
goto flag;
}
break;
case 3:
{
deleted(head);
system("cls");
goto flag;
}
break;
case 4:
{
if (head != nullptr) // 如果链表还存在,删除它
{
deleted(head);
}
return;
}
break;
}
}int main()
{
creatlist();
return 0;
}