#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef struct Node {
int data;
struct Node* next;
};
//初始化,创建一个有头结点的链表
Node* InitList()
{
Node* L = (Node*)malloc(sizeof(Node));
L->data = 0;
L->next = L;//单循环链表,没有数据元素时,头结点指向自己
return L;
}
//头插法创建链表
void Headinsert(Node* L,int data)
{
Node* node = (Node*)malloc(sizeof(Node));//新建node节点并为它开辟空间
node->data = data;
node->next = L->next;//指针断开
L->next = node;//指针链接
}
void Print(Node* L)//参数是传入的头指针
{
Node* node = L->next;
while (node!=L)//循环链表遍历结束的标志
{
cout << node->data;
node = node->next;
}
cout << endl;
}
int main()
{
Node* L = InitList();
Headinsert(L, 1);
Headinsert(L, 2);
Headinsert(L, 3);
Headinsert(L, 4);
Headinsert(L, 5);
Headinsert(L, 6);
Print(L);
system("pause");
return 0;
}
接下来我们在此代码的前提下,再加一段尾插法的代码
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef struct Node {
int data;
struct Node* next;
};
//初始化,创建一个有头结点的链表
Node* InitList()
{
Node* L = (Node*)malloc(sizeof(Node));
L->data = 0;
L->next = L;//单循环链表,没有数据元素时,头结点指向自己
return L;
}
//头插法创建链表
void Headinsert(Node* L, int data)
{
Node* node = (Node*)malloc(sizeof(Node));//新建node节点并为它开辟空间
node->data = data;
node->next = L->next;//指针断开
L->next = node;//指针链接
L->data++;//记录数据个数寻找最后一个结点
}
void TailInsert(Node* L, int data)
{
Node* n = L;//前插时找到的最后一个结点
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
while (n->next != L)//n等于L时,说明已经遍历到最后一个结点
{
n = n->next;
}
node->next = L;
n->next = node;//尾插法
}
void Print(Node* L)//参数是传入的头指针
{
Node* node = L->next;
while (node != L)//循环链表遍历结束的标志
{
cout << node->data;
node = node->next;
}
cout << endl;
}
int main()
{
Node* L = InitList();
Headinsert(L, 1);
Headinsert(L, 2);
Headinsert(L, 3);
Headinsert(L, 4);
Headinsert(L, 5);
Headinsert(L, 6);
TailInsert(L, 7);
TailInsert(L,8);
Print(L);
system("pause");
return 0;
}
再加一条删除链表数据的功能
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
using namespace std;
typedef struct Node {
int data;
struct Node* next;
};
//初始化,创建一个有头结点的链表
Node* InitList()
{
Node* L = (Node*)malloc(sizeof(Node));
L->data = 0;
L->next = L;//单循环链表,没有数据元素时,头结点指向自己
return L;
}
//头插法创建链表
void Headinsert(Node* L, int data)
{
Node* node = (Node*)malloc(sizeof(Node));//新建node节点并为它开辟空间
node->data = data;
node->next = L->next;//指针断开
L->next = node;//指针链接
L->data++;//记录数据个数寻找最后一个结点
}
void TailInsert(Node* L, int data)
{
Node* n = L;//前插时找到的最后一个结点
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
while (n->next != L)//n等于L时,说明已经遍历到最后一个结点
{
n = n->next;
}
node->next = L;
n->next = node;//尾插法
}
//实现删除功能
int DeleteList(Node* L, int data)//参数分别是头指针和删除的结点
{
Node* preNode = L;//记录要删除结点之前的一个结点
Node* node = L->next;//node 结点指向L的第一个节点
while (node != L)
{
if (node->data == data)
{
preNode->next = node->next;
free(node);
return TRUE;
}
preNode = node;
node = node->next;
}
return FALSE;
}
void Print(Node* L)//参数是传入的头指针
{
Node* node = L->next;
while (node != L)//循环链表遍历结束的标志
{
cout << node->data;
node = node->next;
}
cout << endl;
}
int main()
{
Node* L = InitList();
Headinsert(L, 1);
Headinsert(L, 2);
Headinsert(L, 3);
Headinsert(L, 4);
Headinsert(L, 5);
Headinsert(L, 6);
TailInsert(L, 7);
TailInsert(L, 8);
DeleteList(L, 4);
DeleteList(L, 7);
Print(L);
system("pause");
return 0;
}