在用结构体写单链表时,需要先了解指针
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
//1.创建一个表头,表示整个链表
struct Node* createlistHead()
{
//链表的基本单元是结构体
//动态申请内存,将结构体指针转变为结构体变量
struct Node* listHead = (struct Node*)malloc(sizeof(struct Node));
//初始化结构体变量
//表头不装数据
listHead->next = NULL;
return listHead;
}
//2.创建节点;为插入做准备
struct Node* createNewNode(int data)
{
struct Node* NewNode = (struct Node*)malloc(sizeof(struct Node));
NewNode->data = data;
NewNode->next = NULL;
return NewNode;
}
//3.打印链表;创建一个移动指针依此打印链表中的元素
void print(struct Node*listHead)
{ //由于表头没有数据,即从表头的下一个节点开始打印
struct Node* pMove = listHead->next;
while (pMove) //如果pMove不空
{
printf("%d\t", pMove->data);
pMove = pMove->next;
}
printf("\n");
}
//4.链表的插入;表头插入
void insertNewNodebyhead(struct Node* listhead, int data)
{
struct Node* NewNode = createNewNode(data);
//调用创建节点的函数
NewNode->next = listhead->next;
listhead->next = NewNode;
}
//5.标尾插入
void insertNewNodebytail(struct Node* listhead, int data)
{
//第一步,创建节点
struct Node* newNode = createNewNode(data);
//第二步,找到最后一个元素
struct Node* tail = listhead;
//移动指针,找到最后一个元素
while (tail->next!=NULL)//如果不空
{
tail = tail->next;
}
//第三步,循环退出,即找到最后一个元素,将最后一个元素的next指向新的节点
tail->next = newNode;
}
//6.指定位置前插入
void insertNewNodebyAppoint(struct Node* listhead, int data, int n)
{
struct Node* appointfront=listhead;
struct Node* appoint = listhead->next;
if (listhead == NULL)//链表为空的话就不用找了
{
printf("链表为空!!\n");
return;
}
else
{
while (appoint->data != n)
{
//如果未找到依此往后面移动
appoint = appoint->next;
appointfront = appointfront->next;
if (appoint == NULL)
{
printf("未能找到相关信息!!\n");
return;
}
}
//如果循环退出,即找到,插入节点
//创建节点
struct Node* newNode = createNewNode(data);
appointfront->next = newNode;
newNode->next = appoint;
}
}
//7.表头删除
void deleteNodebyhead(struct Node* listhead)
{
//从链表的第2个元素开始删除
struct Node* deleteNode = listhead->next;
listhead->next = deleteNode->next;
free(deleteNode);
deleteNode = NULL;
}
//8.表尾删除
void deleteNodebytail(struct Node* listhead)
{
struct Node* tail = listhead;
struct Node* tailfront = NULL;
while (tail->next != NULL) //找到表尾
{
tailfront = tail;
tail = tail->next;
}
free(tail);
tailfront->next = NULL;
tail = NULL;
}
//9.指定位置删除
void deletNodebyAppoint(struct Node* listhead,int data)
{
struct Node* appointfront = listhead;
struct Node* appoint = listhead->next;
if (listhead == NULL)//链表为空的话就不用找了
{
printf("链表为空!!\n");
return;
}
else
{
while (appoint->data != data)
{
//如果未找到依此往后面移动
appoint = appoint->next;
appointfront = appointfront->next;
if (appoint == NULL)
{
printf("未能找到相关信息!!\n");
return;
}
}
appointfront->next = appoint->next;
free(appoint);
appoint = NULL;
}
}
int main()
{
printf("表头插入,输入你要插入元素的个数\n");
int n;
scanf("%d", &n);
struct Node* list = createlistHead();//创建头结点
for (int i = 0; i < n; i++)
{
int m;
scanf("%d", &m);
insertNewNodebyhead(list, m);
}
print(list);//打印list链表
printf("表尾插入,请输入你要插入元素的个数\n");
int a;
scanf("%d", &a);
struct Node* list1 = createlistHead();
for (int i = 0; i < a; i++)
{
int b;
scanf("%d", &b);
insertNewNodebytail(list1, b);
}
print(list1);
printf("在指定位置插入8元素:\n");
insertNewNodebyAppoint(list1, 8, 3);
print(list1);
printf("表头删除\n");
deleteNodebyhead(list1);
print(list1);
printf("表尾删除\n");
deleteNodebytail(list1);
print(list1);
int k;
printf("指定位置删除,输入你想删除list中的元素:");
scanf("%d", &k);
deletNodebyAppoint(list, k);
print(list);
system("pause");
return 0;
}
源代码和运行结果在图片上显示