C语言单链表的基本操作
结构体
typedef struct {
int data;
struct SqNode * Next;
}SqNode,*SqList;
链表的初始化
SqList InitList(SqNode* L)
{
L = (SqNode*)malloc(sizeof(SqNode));
if(!L)
return NULL;
L->Next = NULL;
return L;
}
创建链表(头插法)
SqList HeadCreateList(SqNode *L)
{
printf("请输入您需要创建的顺序表,输入999结束\n");
int Value = 0;
scanf_s("%d", &Value);
getchar();
while (Value != 999)
{
SqNode * s ;
s = (SqNode*)malloc(sizeof(SqNode));
s->data = Value;
s->Next =L->Next;
L->Next = s;
scanf_s("%d", &Value);
getchar();
}
return L;
}
创建链表尾插法
SqList TailCreateList(SqNode* L)
{
SqNode* s;
SqNode* T;
T = L;
int Value;
printf("请输入你要创建的顺序表,输入999结束创建\n");
scanf_s("%d", &Value);
getchar();
while (Value!=999)
{
s = (SqNode*)malloc(sizeof(SqNode));
s->data = Value;
T->Next = s;
T = s;
scanf_s("%d", &Value);
getchar();
}
T->Next = NULL;
return L;
}
遍历链表
SqList PrintfList(SqNode *L)
{
SqNode* s = L->Next;
while (s!=NULL)
{
printf("%d\n", s->data);
s = s->Next;
}
}
获取链表长度
int LengthList(SqNode* L)
{
int i = 0;
SqList s;
s = L->Next;
while (s != NULL)
{
i++;
s = s->Next;
}
return i;
}
按位查找元素
SqList LocateElement(SqNode* L, int Locate)
{
SqNode* s;
s = L->Next;
int i = 0;
while (i < Locate-1 && s != NULL)
{
i++;
s = s->Next;
}
return s;
}
按值查找
SqList SecletElement(SqNode* L, int Value)
{
SqNode* s=NULL;
s = L->Next;
while (s!=NULL && s->data!=Value)
{
s = s->Next;
}
return s;
}
插入元素
SqList InsertElement(SqNode* L, int Locate)
{
if (Locate > LengthList(L)+1)
{
printf("插入失败\n");
}
SqNode* s;
SqNode* r;
int Value = 0;
r = (SqNode*)malloc(sizeof(SqNode));
int i=0;
s = L->Next;
while (s!=NULL && i<Locate-2)
{
s = s->Next;
i++;
}
printf("请输入你要插入的数据:");
scanf_s("%d", &Value);
getchar();
r->data = Value;
r->Next = s->Next;
s->Next = r;
return L;
}
根据位置删除节点
SqNode DeleteElemnent(SqNode* L, int Locate)
{
int i = 0;
SqNode* q;
SqNode* s;
SqNode r;
s = L->Next;
while (i<Locate-2&&s!=NULL)
{
s = s->Next;
i++;
}
q = s->Next;
s->Next = s->Next;
r = *q;
free(q);
return r;
}
完整代码
文件夹结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/74cb7381ff75b898b2a71b3e3c92a28c.jpeg)
线性表–单链表.c文件
#include<stdio.h>
#include<stddef.h>
#include<stdlib.h>
#include"线性表--单链表.h"
SqList InitList(SqNode* L)
{
L = (SqNode*)malloc(sizeof(SqNode));
L->Next = NULL;
return L;
}
SqList HeadCreateList(SqNode *L)
{
printf("请输入您需要创建的顺序表,输入999结束\n");
int Value = 0;
scanf_s("%d", &Value);
getchar();
while (Value != 999)
{
SqNode * s ;
s = (SqNode*)malloc(sizeof(SqNode));
s->data = Value;
s->Next =L->Next;
L->Next = s;
scanf_s("%d", &Value);
getchar();
}
return L;
}
SqList TailCreateList(SqNode* L)
{
SqNode* s;
SqNode* T;
T = L;
int Value;
printf("请输入你要创建的顺序表,输入999结束创建\n");
scanf_s("%d", &Value);
getchar();
while (Value!=999)
{
s = (SqNode*)malloc(sizeof(SqNode));
s->data = Value;
T->Next = s;
T = s;
scanf_s("%d", &Value);
getchar();
}
T->Next = NULL;
return L;
}
SqList PrintfList(SqNode *L)
{
SqNode* s = L->Next;
while (s!=NULL)
{
printf("%d\n", s->data);
s = s->Next;
}
}
int LengthList(SqNode* L)
{
int i = 0;
SqList s;
s = L->Next;
while (s != NULL)
{
i++;
s = s->Next;
}
return i;
}
SqList LocateElement(SqNode* L, int Locate)
{
SqNode* s;
s = L->Next;
int i = 0;
while (i < Locate-1 && s != NULL)
{
i++;
s = s->Next;
}
return s;
}
SqList SecletElement(SqNode* L, int Value)
{
SqNode* s=NULL;
s = L->Next;
while (s!=NULL && s->data!=Value)
{
s = s->Next;
}
return s;
}
SqList InsertElement(SqNode* L, int Locate)
{
if (Locate > LengthList(L)+1)
{
printf("插入失败\n");
}
SqNode* s;
SqNode* r;
int Value = 0;
r = (SqNode*)malloc(sizeof(SqNode));
int i=0;
s = L->Next;
while (s!=NULL && i<Locate-2)
{
s = s->Next;
i++;
}
printf("请输入你要插入的数据:");
scanf_s("%d", &Value);
getchar();
r->data = Value;
r->Next = s->Next;
s->Next = r;
return L;
}
SqNode DeleteElemnent(SqNode* L, int Locate)
{
int i = 0;
SqNode* q;
SqNode* s;
SqNode r;
s = L->Next;
while (i<Locate-2&&s!=NULL)
{
s = s->Next;
i++;
}
q = s->Next;
s->Next = s->Next;
r = *q;
free(q);
return r;
}
线性表–单链表.h文件
#pragma once
#ifndef __线性表-链表__H__
#define __线性表
typedef struct {
int data;
struct SqNode * Next;
}SqNode,*SqList;
SqList InitList(SqNode* L);
SqList HeadCreateList(SqNode* L);
SqList TailCreateList(SqNode* L);
SqList PrintfList(SqNode* L);
int LengthList(SqNode* L);
SqList LocateElement(SqNode* L, int Locate);
SqList SecletElement(SqNode * L, int Value);
SqList InsertElement(SqNode* L, int Locate);
SqNode DeleteElemnent(SqNode* L, int Locate);
#endif
Main函数文件
#include<stdio.h>
#include"线性表--单链表.h"
int main()
{
SqNode *L=NULL;
{
L = InitList(L);
L=TailCreateList(L);
PrintfList(L);
}
{
int Length = LengthList(L);
printf("链表长度为:%d\n", Length);
}
{
PrintfList(L);
}
{
SqNode *s;
int locate = 0;
printf("你要查找第几个元素?\n");
scanf_s("%d", &locate);
s=LocateElement(L, locate);
printf("您查找的元素是:%d\n", s->data);
}
{
SqNode *s ;
int Value;
printf("请输入您要查找的元素:");
scanf_s("%d",&Value);
s=SecletElement(L,Value);
if (s== NULL)
{
printf("查找失败\n");
}
else {
printf("%d\n", s->data);
}
}
{
int Locate = 0;
printf("请输入您要插入的位置:");
scanf_s("%d", &Locate);
getchar();
L=InsertElement(L, Locate);
PrintfList(L);
}
{
printf("请输入你要删除元素的位置:");
int Locate;
SqNode r;
scanf_s("%d", &Locate);
getchar();
r=DeleteElemnent(L, Locate);
printf("您删除的元素是:%d", r.data);
}
}
欢迎大家在评论区赐教,谢谢