本文主要是介绍数据结构——单链表相关操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
zhuzhu1、结构框图:
2、增删改查:
定义链表节点和对象类型
/*************************************************************************> File Name: link.h> Author: yas> Mail: rage_yas@hotmail.com> Created Time: Tue 03 Sep 2024 11:30:03 AM CST************************************************************************/#ifndef _LINK_H
#define _LINK_Htypedef int DataType;//链表的节点类型
typedef struct node
{DataType data; //数据域struct node *pnext;//指针域
}Link_Node_t;//链表的对象类型
typedef struct link
{Link_Node_t *phead;//头结点int clen; //当前节点个数
}Link_t;extern Link_t *create_link();
extern int push_link_head(Link_t *plink,DataType data);
extern void print_link(Link_t *plink);
extern int push_link_tial(Link_t *plink,DataType data);
int pop_head(Link_t *plink);
int pop_tail(Link_t *plink);
int clear_link(Link_t *plink);
int update_link_data(Link_t *plink,DataType old_data,DataType new_data);
int *find_link_data(Link_t *plink,DataType data);#endif
程序
/*************************************************************************
> File Name: link.c
> Author: yas
> Mail: rage_yas@hotmail.com
> Created Time: Tue 03 Sep 2024 11:29:54 AM CST
************************************************************************/#include<stdio.h>
#include<stdlib.h>
#include"link.h"//创建一个链表对象
Link_t *create_link()
{//在堆上开辟sizeof(link_t)大小的空间Link_t *plink = malloc(sizeof(Link_t)); if(NULL == plink)//判断创建成功{perror("fail malloc");return NULL;}plink->phead = NULL;//初始化头指针plink->clen = 0; //初始化当前链表长度return plink; //返回创建好的链表对象
}//头插
int push_link_head(Link_t *plink,DataType data)
{//堆上开空间Link_Node_t *pnode = malloc(sizeof(Link_Node_t));if(NULL == pnode){perror("fail malloc");return -1;}pnode->data = data; //初始化节点->数据域pnode->pnext = NULL; //初始化节点->指针域pnode->pnext = plink->phead; //头插plink->phead = pnode; //头结点指向新节点plink->clen++; return 0;
}//遍历void print_link(Link_t *plink)
{Link_Node_t *p = plink->phead;while(p != NULL){printf("%d\n",p->data);p = p->pnext;}
}//判空int is_empty_link(Link_t *plink)
{return plink->phead == NULL;
}//尾插
int push_link_tial(Link_t *plink,DataType data)
{Link_Node_t *pnode = malloc(sizeof(Link_Node_t));if(NULL == pnode){perror("fail malloc");return -1;}pnode->data = data;pnode->pnext = NULL;if(is_empty_link(plink)){plink->phead = pnode;}else{Link_Node_t *p = plink->phead;while(p->pnext != NULL){p = p->pnext;}p->pnext = pnode;}plink->clen++;return 0;
}//头删
int pop_head(Link_t *plink)
{Link_Node_t *p = plink->phead;if(is_empty_link(plink)){return 0;}else{plink->phead = p->pnext;}plink->clen--;free(p);
}//尾删
int pop_tail(Link_t *plink)
{Link_Node_t *p = plink->phead;if(is_empty_link(plink)){return 0;}else if(plink->clen == 1){pop_head(plink);}else{Link_Node_t *p = plink->phead;while(p->pnext->pnext != NULL){p = p->pnext;}free(p->pnext);p->pnext = NULL;plink->clen--;}return 0;
}//清空链表
int clear_link(Link_t *plink)
{Link_Node_t *p = plink->phead;while(!(is_empty_link(plink))){pop_head(plink);}
}//修改数据
int update_link_data(Link_t *plink,DataType old_data,DataType new_data)
{Link_Node_t *p = plink->phead;if(is_empty_link(plink)){printf("link is empty\n");return 0;}else{while(p->data != old_data){p = p->pnext;}p->data = new_data;}
}//查找数据
int *find_link_data(Link_t *plink, DataType data)
{Link_Node_t *p = plink->phead;while (p != NULL) {if (p->data == data) {printf("%p\n", p); }p = p->pnext; }return NULL;
}
int main()
{Link_t *plink = create_link();if(NULL == plink){return -1;}push_link_head(plink,4);push_link_head(plink,3);push_link_head(plink,2);push_link_head(plink,1);//clear_link(plink);push_link_tial(plink,5);push_link_tial(plink,6);push_link_tial(plink,7);push_link_tial(plink,8);//pop_head(plink);//pop_tail(plink);//print_link(plink);update_link_data(plink,7,9);// find_link_data(plink,5);print_link(plink);return 0;
}
这篇关于数据结构——单链表相关操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!