提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言 单链表就是一种简单的数据存储方式,也是数据结构入门的第一课,希望这篇文章能帮到你们学会单链表;
提示:以下是本篇文章正文内容,下面案例可供参考
单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。
代码如下(示例):
typedef struct node
{
elemstyle data;
struct node* next;
}Node;
typedef struct Linklist
{
Node* top;
int cursize;
}LinkList;
2.单链表插入数据
代码如下(示例):
void insert_Linklist(LinkList* plist, int val)
{
assert(plist != NULL);
//创建一个新节点,为插入链表做准备;
Node* newnode = creatNode(val);
newnode->next = plist->top->next;
plist->top->next = newnode;
plist->cursize++;
}
3.单链表删除指定数据
int delete_LinkList(LinkList* plist, int pval)
{
assert(plist != NULL);
Node* curr = plist->top;
Node* pmove = plist->top->next;
while (pmove->data != pval)
{
curr = pmove;
pmove = pmove->next;
}
if (pmove==NULL)
{
printf("链表中没有该数据,删除失败;\n");
return false;
}
else
{
curr->next = pmove->next;
free(pmove);
plist->cursize--;
return true;
}
}
4.单链表长度
int length_list(LinkList* plist)
{
assert(plist != NULL);
return plist->cursize;
}
5.源代码
//构建单链表;
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include
#include
#define true 2
#define false -2
#define ok 1
#define erro -1
typedef int elemstyle;
typedef struct node
{
elemstyle data;
struct node* next;
}Node;
typedef struct Linklist
{
Node* top;
int cursize;
}LinkList;
//创建结点;
Node* creatNode(int data)
{
Node* newnode = (Node*)malloc(sizeof(Node));
//结点的数据域和指针域必须初始化;
newnode->data = data;
newnode->next = NULL;
}
//链表的初始化
LinkList* Init_Linklist(LinkList* plist)
{
assert(plist != NULL);
plist->top = (Node*)malloc(sizeof(Node));
plist->top->data =0;
plist->top->next = NULL;
plist->cursize = 0;//链表的长度;
return plist;
}
//插入数据;
void insert_Linklist(LinkList* plist, int val)
{
assert(plist != NULL);
//创建一个新节点,为插入链表做准备;
Node* newnode = creatNode(val);
newnode->next = plist->top->next;
plist->top->next = newnode;
plist->cursize++;
}
//删除特定值;
int delete_LinkList(LinkList* plist, int pval)
{
assert(plist != NULL);
Node* curr = plist->top;
Node* pmove = plist->top->next;
while (pmove->data != pval)
{
curr = pmove;
pmove = pmove->next;
}
if (pmove==NULL)
{
printf("链表中没有该数据,删除失败;\n");
return false;
}
else
{
curr->next = pmove->next;
free(pmove);
plist->cursize--;
return true;
}
}
//单链表的数据查询;
int find_Linklist(LinkList* plist, int val)
{
assert(plist != NULL);
//构建一个移动的指针,进行查找;
Node* pmove = plist->top->next;
int i = 0;
while (pmove->data!=val)
{
i++;
pmove = pmove->next;
}
if (pmove)
{
return i;//返回值的下标;
}
else
{
return false;
}
}
//函数的长度;
int length_list(LinkList* plist)
{
assert(plist != NULL);
return plist->cursize;
}
//打印函数;
void printList(LinkList* plist)
{
assert(plist != NULL);
Node* pmove = plist->top->next;
while (pmove)
{
printf("%5d", pmove->data);
pmove = pmove->next;
}
}
//链表的释放;
void freeList(LinkList* plist)
{
assert(plist != NULL);
Node* pmove = plist->top->next;
while (pmove)
{
plist->top->next = pmove->next;
}
free(plist->top);
plist->cursize = 0;
}
//调试函数;
int main(void)
{
LinkList plist;
int length;
int k;
int val;
Init_Linklist(&plist);
for (int i = 0; i < 10; i++)
{
insert_Linklist(&plist, i);
}
//特定数据查询;
scanf("%d", &val);
k = find_Linklist(&plist,val);
printf("val的位置是:%d\n", k);
//链表的长度的统计;
length = length_list(&plist);
printf("链表的长度为:%d\n", length);
//链表的打印;
printList(&plist);
}
关注我,下期带你学会顺序表。
有需要问问题的可以留言,或者加V,共同进步;
V:16655560651;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)