链表
单项动态链表
单向动态链表基本操作: 插入 删除 查找
#include <iostream>
#include <stdlib.h>
using namespace std;
void menu(struct list*head);
struct list*creat_List(struct list* head, int n);
struct list*print_List(struct list* head);
struct list*del_Node(struct list* head, int n);
struct list*insert_Node(struct list* head, int n);
struct list*creat_headNode();
struct list*del_List(struct list* head);
struct list
{
int data;//数据域
int num;//序号
struct list *next;//指针域
};
//建立头结点
//建立头结点
struct list *creat_headNode()
{
struct list* head = new struct list;//动态分配空间给头结点
head->next = NULL;//头节点指向空指针
return head;
}
//续表(尾插)
//续表(尾插)
struct list* creat_List(struct list* head, int n)
{
struct list* pre = head;//前一节点指向下一节点
for (int i = 0; i < n; i++)
{
struct list* pnew = new struct list;
if (head->next == NULL)//若头节点指向空指针(链表为空)
head->next = pnew;
else if (head->next != NULL)//若链表不为空
for (; pre->next != NULL;)
pre = pre->next;//遍历到链表最后一个结点
cout << "请输入第" << i + 1 << "个结点数据\n";
cout << "数据:";
cin >> pnew->data;
cout << "序号:";
cin >> pnew->num;
pre->next = pnew;//前一结点指针域指向新结点
pre = pnew;//pre移动到新结点 //或pre = pre->next;
pnew->next = NULL;//新结点指向空指针(尾结点) //或pre->next = NULL;
}
return head;
}
//遍历输出
//遍历
struct list*print_List(struct list* head)
{
struct list* p = head->next;
if (p == NULL)
{
cout << "链表暂时为空\n";
return head;
}
cout << "序号 " << '\t' << "数据\n";
while (p != NULL)//指针域不为空
{
cout << p->num << '\t' << p->data << endl;
p = p->next;//指向下一节点
}
return head;
}
//删除结点
//删除结点
struct list* del_Node(struct list* head, int n)
{
struct list* pnow;//当前节点(要删除的结点)
struct list* pre;//当前节点的上一结点
for (pre = head, pnow = head->next; pnow != NULL;)
{
if (n == pnow->num)
{
pre->next = pnow->next;//上一节点跳过当前结点
delete pnow;
break;
}
else
{
pre = pnow, pnow = pre->next;
}
}
return head;
}
任意位置插入结点
//任意位置插入结点
struct list* insert_Node(struct list* head, int n)
{
struct list* pre; //前
struct list* pnext;//后
if (n == 0)//在头指针后插入
{
struct list*pnew = new struct list;
cout << "请输入新结点的数据\n";
cout << "数据:";
cin >> pnew->data;
cout << "序号:";
cin >> pnew->num;
pnew->next = head->next;
head->next = pnew;
return head;
}
for (pre = head->next, pnext = pre->next; pre != NULL;)
{
if (pre->num == n)
{
struct list* pnew = new struct list;// 中(将要插入的位置)
cout << "请输入新结点的数据\n";
cout << "数据:";
cin >> pnew->data;
cout << "序号:";
cin >> pnew->num;
pnew->next = pre->next;//插入结点指向下一结点
pre->next = pnew;//上一节点直指向插入结点
break;
}
else
{
pnext = pre, pre = pre->next;
}
}
return head;
}
删除链表(从头开始释放内存
//删除链表(从头开始释放内存)
struct list*del_List(struct list*head)
{
struct list* phead = head;//指向头结点
if (head->next == NULL)
{
cout << "链表暂时为空\n";
return head;
}
else if (head->next != NULL)
for (; phead->next != NULL;)//遍历删除
{
struct list* pnow = phead->next;//指向要删除的结点
phead->next = pnow->next;//头节点跳过要删除的结点指向下一结点
delete pnow;
}
return head;
}
菜单
//菜单
void menu(struct list*head)
{
int choice;
int n;
cout << "1.续表 2.插入结点 3.删除节点 4.输出链表 5.删除链表 6.退出\n";
cin >> choice;
switch (choice)
{
case 1:
cout << "请输入要创建的结点数:";
cin >> n;
head = creat_List(head, n); menu(head); break;
case 2:
cout << "请输入要在哪个结点后插入:" << endl;
cin >> n;
head = insert_Node(head, n); menu(head); break;
case 3:
cout << "请输入要删除的结点序号:" << endl;
cin >> n;
head = del_Node(head, n);
menu(head); break;
case 4:head = print_List(head); menu(head); break;
case 5:head = del_List(head); menu(head); break;
case 6:head = del_List(head); delete head; exit(0); break;
}
}
int main()
{
struct list* headNode = creat_headNode();
menu(headNode);
return 0;
}