数据结构就是存储数据的不同方式。
数据与数据之间的关系
数据的逻辑结构:数据元素与元素之间的关系
集合:关系平等
线性结构:元素之间一对一的关系(表(数组,链表),队列。栈。。。)
树型结构:元素之间一对多的关系(二叉树)
图形结构:元素之间多对多的关系(网状结构)
单链表:链表的头插、尾插、头删尾删、查找、更改、销毁
cs
/*************************************************************************
> File Name: link.c
> Author: yas
> Mail: rage_yas@hotmail.com
> Created Time: Mon 18 Mar 2024 01:42:51 PM CST
************************************************************************/
#include "link.h"
#include<stdio.h>
link_list *create_head_link(void)
{
link_list *head = NULL;
head = malloc(sizeof(link_list));
if (head == NULL)
{
perror("fail to malloc");
return NULL;
}
head->phead = NULL;
head->clen = 0;
return head;
}
/* 头插 */
int push_head_node(link_list *plist, DATA_TYPE data)
{
link_node *pnode = NULL;
pnode = malloc(sizeof(link_node));
if (pnode == NULL)
{
perror("fail to malloc");
return -1;
}
pnode->data = data;
pnode->pnext = NULL;
pnode->pnext = plist->phead;
plist->phead = pnode;
plist->clen++;
return 0;
}
/* 尾插 */
int push_tail_node(link_list *plist, DATA_TYPE data)
{
link_node *pnode = NULL;
pnode = malloc(sizeof(link_node));
if (pnode == NULL)
{
perror("fail to malloc");
return -1;
}
pnode->pnext = NULL;
pnode->data = data;
if (plist->phead == NULL)
{
plist->phead = pnode;
}
else
{
link_node *ptmp = NULL;
ptmp = plist->phead;
while(ptmp->pnext != NULL)
{
ptmp = ptmp->pnext;
}
ptmp->pnext = pnode;
}
plist->clen++;
return 0;
}
/* 尾删 */
int pop_tail_node(link_list *plist)
{
link_node *ptmp = plist->phead;
if(plist->phead == NULL) //空链表
{
return 0;
}
else if (NULL == ptmp->pnext)
{
free(ptmp);
plist->phead = NULL;
}
else
{
while(ptmp->pnext->pnext != NULL)
{
ptmp = ptmp->pnext;
}
free(ptmp->pnext); //释放尾节点
ptmp->pnext = NULL; //新的尾节点
}
plist->clen--;
return 0;
}
/* 头删 */
int pop_head_node(link_list *plist)
{
link_node *ptmp = plist->phead;
if(plist->phead == NULL) //空链表
{
return 0;
}
else if (NULL == ptmp->pnext)
{
free(ptmp);
plist->phead = NULL;
}
else
{
plist->phead = ptmp->pnext;
free(ptmp);
}
plist->clen--;
return 0;
}
/* 查找链表第一个相同的数的地址 */
link_node *find_local(link_list *plist, DATA_TYPE data)
{
link_node *ptmp = plist->phead;
while(ptmp->pnext != NULL)
{
if(ptmp->data != data)
{
ptmp = ptmp->pnext;
}
else if(ptmp->data == data)
{
return ptmp;
}
}
return NULL;
}
/* 更改链表数据 */
int change_link(link_list *plist, DATA_TYPE olddata, DATA_TYPE newdata)
{
link_node *ptmp = NULL;
ptmp = find_local(plist, olddata);
if (ptmp == NULL)
{
return -1;
}
ptmp->data = newdata;
return 0;
}
/* 销毁 */
int destroy_link(link_list *plist)
{
while(plist->clen != 0)
{
pop_tail_node(plist);
}
return 0;
}