单链表
单链表比较静态链表更加灵活,长度可变,删除便捷,唯一较单链表费时就是查询,不能下标查询。但是总体上比静态链表有了质的飞跃。
单链表最核心的就是指针的灵活使用,以及对较为特殊结构的理解。
结构中最后的指针指向同结构的下个元素,这个指针就像链子一样,一个接一个把数据串起来。
typedef struct SingleList
{
int data;
struct SingleList* next;
} SList;
另外单链表分为含有头节点的链表和没有头节点的链表,插入 方法有头插法和尾插法。我这里按照有头节点的链表进行演示,分别进行头插法和尾插法,以及常用的增删改查。
先定义singlelist.h头文件
#pragma once
#include <stdio.h>
#include <stdlib.h>
typedef struct SingleList
{
int data;
struct SingleList* next;
} SList;
//建立空头节点
void CreateSingleList_Head();
//带有头节点的头插法
void InsertSingleList_WithHead(SList* sl,int data);
//带有头节点的尾插法
void InsertSingleList_WithTail(SList* sl, int data);
//打印链表
void PrintSingleList(SList* sl);
//链表长度
int LengthSingleList(SList* sl);
//删除 按元素数值删除 仅删除一次
int DeleteDataSingleList(SList* sl, int data);
//删除 按元素位置删除
int DeletePosSingleList(SList* sl, int pos);
//清空链表仅剩下空头节点
void TruncSingleList(SList* sl);
//修改指定位置的元素值
int ModifyDataSingleList(SList* sl, int pos,int new_data);
//获取指定位置的元素值
int GetDataSingleList(SList* sl, int pos);
再对定义的接口函数进行实现,singlelist.c
#pragma once
#include "singlelist.h"
extern SList* pSLHead;
void CreateSingleList_Head()
{
pSLHead = (SList*)malloc(sizeof(SList));
pSLHead->next = NULL;
}
void InsertSingleList_WithHead(SList* sl, int data)
{
SList* pnew = (SList*)malloc(sizeof(SList));
pnew->data = data;
pnew->next = sl->next;
sl->next = pnew;
}
void InsertSingleList_WithTail(SList* sl, int data)
{
SList* pnew;
//找到末尾元素
while (sl->next)
{
sl = sl->next;
}
pnew = (SList*)malloc(sizeof(SList));
pnew->data = data;
sl->next = pnew;
pnew->next = NULL;
}
void PrintSingleList(SList* sl)
{
int i = 0;
//带有头节点,跳过头结点
sl = sl->next;
while (sl)
{
printf("\t%d:\t%d\n",i++,sl->data);
sl = sl->next;
}
}
int LengthSingleList(SList* sl)
{
int i = 0;
while (sl->next)
{
i++;
sl = sl->next;
}
return i;
}
int DeleteDataSingleList(SList* sl, int data)
{
//return 0:为空
//return -1:没有该元素
//return 1:删除成功
int i;
SList* phead = sl;
SList* pdata; //数据前节点对应指针
SList* pdata_free; //数据删除指针
i = 0;
i = LengthSingleList(phead);
if (!i)
{
return i;
}
pdata = sl;
while (pdata)
{
if (!pdata->next)
{
break;
}
else if(pdata->next->data == data)
{
pdata_free = pdata->next;
pdata->next = pdata->next->next;
free(pdata_free);
return 1;
}
pdata = pdata->next;
}
return -1;
}
int DeletePosSingleList(SList* sl, int pos)
{
//return 0:为空
//return -1:没有该元素
//return 1:删除成功
int i,j;
SList* phead = sl;
SList* ppos=NULL; //数据前节点对应指针
SList* ppos_free=NULL; //数据删除指针
i = 0;
i = LengthSingleList(phead);
if (!i)
{
return i;
}
if (i < pos)
{
return -1;
}
ppos = sl;
for (j = 0; j < pos - 1; j++)
{
ppos = ppos->next;
}
ppos_free = ppos->next;
ppos->next = ppos->next->next;
free(ppos_free);
return 1;
}
void TruncSingleList(SList* sl)
{
SList* phead = sl->next;
SList* pfree = NULL;
while (phead)
{
pfree = phead;
phead = phead->next;
free(pfree);
}
sl->next = NULL;
}
int ModifyDataSingleList(SList* sl, int pos, int new_data)
{
int i,j;
i = 0;
i = LengthSingleList(sl);
if (!i)
{
return i;
}
if (i < pos)
{
return -1;
}
for (j = 0; j < pos ; j++)
{
sl = sl->next;
}
sl->data = new_data;
return 1;
}
int GetDataSingleList(SList* sl, int pos)
{
int i, j;
i = 0;
i = LengthSingleList(sl);
if (!i)
{
return i;
}
if (i < pos)
{
return -1;
}
for (j = 0; j < pos; j++)
{
sl = sl->next;
}
return sl->data;
}
最后写main函数进行测试和实现
#include <stdio.h>
#include<time.h>
#define MAXSIZE 10
#include "singlelist.h"
SList* pSLHead = NULL;
SList* plist = NULL;
int main()
{
int i;
srand((unsigned int)time(NULL));
CreateSingleList_Head(); //建立空头节点
//printf("pSLHead address is %x\n", pSLHead);
plist = pSLHead;
for (i = 0; i < MAXSIZE; i++)
{
//InsertSingleList_WithHead(plist, rand()%100);
//printf("plist address is %x\n",plist);
InsertSingleList_WithTail(plist, i);
}
printf("链表长度:%d\n", LengthSingleList(pSLHead));
PrintSingleList(pSLHead);
printf("-----------------\n");
//删除
//DeleteDataSingleList(pSLHead, 100);
//DeletePosSingleList(pSLHead,11);
//清空
//TruncSingleList(pSLHead);
//修改数值
ModifyDataSingleList(pSLHead,2,20);
PrintSingleList(pSLHead);
printf("%d\n", GetDataSingleList(pSLHead,8));
system("pause");
}