本文通过C++编程实现对单链表的创建、插入、删除等操作
#include <iostream>
#include <string>
using namespace std;
/* Link struct body */
typedef struct Node
{
int data;
struct Node *next;
}LinkList;
LinkList *CreateLinkList(void); //创建链表
void InitLink_Head(LinkList *head, int num); //初始化链表,从链表头部处插入节点——倒序输出
void InitLink_Tail(LinkList *head, int num); //初始化链表,从链表尾部处插入节点——正序输出
void InsertNode(LinkList *head, int pos, int value);//在位置i处,插入节点
void DeleteNode(LinkList *head, int pos); //删除位置i位置处的节点
void OutputLink(LinkList *head); //输出链表
void DeleteLink(LinkList *head); //删除链表
int main()
{
int n;
LinkList *head;
cout << "Input Link node numbers and beyond 5:";
cin >> n;
/* 创建链表 */
head = CreateLinkList();
/* 从链表头部插入 */
//InitLink_Head(n);
/* 从链表尾部部插入 */
InitLink_Tail(head, n);
OutputLink(head);
//InsertNode(head, 3, 520);
DeleteNode(head, 4);
OutputLink(head); //打印插入/删除节点后的链表
DeleteLink(head);
return 0;
}
/*
@Description:初始化链表
@Return:返回链表头
*/
LinkList *CreateLinkList(void)
{
LinkList *head; //Link head node
head = (LinkList *)malloc(sizeof(LinkList));
if (NULL == head)
{
cout << "Head memory malloc failed!" << endl;
}
head->next = NULL;
return head;
}
/*
@Description:初始化链表,从链表头部处插入节点
@head:链表头
@num:插入元素个数
@Return:None
*/
void InitLink_Head(LinkList* head, int num)
{
int value = 0;
LinkList *node; //ordinary node
/* 从链表头插入 */
for (int i = 0; i < num; i++)
{
cout << "Input Link data:";
cin >> value;
node = (LinkList *)malloc(sizeof(LinkList));
if (NULL == node)
{
cout << "Link node malloc failed!" << endl;
}
node->data = value;
node->next = head->next;
head->next = node;
}
}
/*
@Description:初始化链表,从链表尾部处插入节点
@head:链表头
@num:插入元素个数
@Return:None
*/
void InitLink_Tail(LinkList *head, int num)
{
int value = 0;
LinkList *tail; //Link tail node
tail = head;
/* Insert node */
for (int i = 0; i < num; i++)
{
cout << "Input link data:";
cin >> value;
LinkList *node;
node = (LinkList *)malloc(sizeof(LinkList));
if (NULL == node)
{
cout << "Link node malloc failed!" << endl;
}
node->data = value;
tail->next = node;
tail = node;
}
tail->next = NULL; //Link tail pointer null
}
/*
@Description:在位置i处,插入节点
@pos:插入位置
@value:插入值
@Return:None
*/
void InsertNode(LinkList* head, int pos, int value)
{
LinkList *NewNode; //Insert node
LinkList *node;
node = head;
for (int i = 1; i < pos; i++)
{
node = node->next; //move link pointer to insert position
}
NewNode = (LinkList *)malloc(sizeof(LinkList));
if (NULL == NewNode)
{
cout << "Link insert node failed!" << endl;
}
NewNode->data = value;
NewNode->next = node->next;
node->next = NewNode;
}
/*
@Description:删除位置i处的节点
@head:链表头
@pos:删除位置
@Return:None
*/
void DeleteNode(LinkList *head, int pos)
{
LinkList *node, *temp_node;
node = head;
for (int i = 1; i < pos; i++)
{
node = node->next;
}
temp_node = node->next;
node->next = node->next->next;
free(temp_node);
}
/*
@Description:打印链表
@head:链表头
@Return:None
*/
void OutputLink(LinkList *head)
{
LinkList *node;
node = head->next;
for(node; node != NULL; node = node->next)
{
cout << node->data << endl;
}
}
/*
@Description:删除链表
@head:链表头
@Return:返回链表头
*/
void DeleteLink(LinkList *head)
{
LinkList *node , *temp_node;
node = head->next;
while (node != NULL)
{
temp_node = node->next;
free(node);
node = temp_node;
}
head->next = NULL;
cout << "Delete link success!" << endl;
}