C语言学习之单向链表操作
来源:互联网 发布:淘宝舞蹈服 编辑:程序博客网 时间:2024/05/21 16:58
该文件为单向链表操作的一些接口:(如发现有错误的地方,及时告知,不胜感激!)
list.h
#ifndef _CHAINLIST_H_
#define _CHAINLIST_H_
typedef struct
{
char key[15];
char name[20];
int age;
}DATATYPE_T;
typedef struct Node
{
DATATYPE_T data;
struct Node *next;
}chainListType;
/* 添加节点到链表末尾 */
chainListType *chainlist_add_end(chainListType *head,DATATYPE_T data);
/* 添加节点到链表首部 */
chainListType *chainlist_add_first(chainListType *head,DATATYPE_T data);
/* 按关键字在链表中查找内容 */
chainListType *chainlist_find(chainListType *head,char *key);
/* 插入节点到链表指定位置 */
chainListType *chainlist_insert(chainListType *head,char *findkey,DATATYPE_T data);
/* 删除指定关键字的节点 */
chainListType *chainlist_delete(chainListType *head,char *key);
/*获取链表的节点数量 */
int chainlist_length(chainListType *head);
#endif
list.c文件如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "chainlist.h"
chainListType *chainlist_add_end(chainListType *head,DATATYPE_T data)
{
chainListType *node,*phead;
node = malloc(sizeof(chainListType));
if(NULL == node)
{
printf("malloc failed\n");
return NULL;
}
node->data = data;
node->next = NULL;
if(head ==NULL)
{
head = node;
return head;
}
phead = head;
while(phead->next != NULL)
{
phead = phead->next;
}
phead->next = node;
return head;
}
chainListType *chainlist_add_first(chainListType *head,DATATYPE_T data)
{
chainListType *node;
node = malloc(sizeof(chainListType));
if(NULL == node)
{
printf("malloc failed\n");
return NULL;
}
node->data = data;
node->next = head;
head = node;
return head;
}
chainListType *chainlist_find(chainListType *head,char *key)
{
chainListType *phead;
phead = head;
while(phead)
{
if(strcmp(phead->data.key,key)==0)
{
return phead;
}
phead = phead->next;
}
return NULL;
}
chainListType *chainlist_insert(chainListType *head,char *findkey,DATATYPE_T data)
{
chainListType *node,*node1;
node = malloc(sizeof(chainListType));
if(NULL == node)
{
printf("malloc failed\n");
return NULL;
}
node->data = data;
node1 = chainlist_find(head,findkey);
if(node1)
{
node1->next = node->next;
node1->next = node;
}
else
{
free(node);
printf("can't find key\n");
}
return head;
}
chainListType *chainlist_delete(chainListType *head,char *key)
{
chainListType *node,*phead;
node = head;
phead = head;
while(phead)
{
if(strcmp(head->data.key,key)==0)
{
node = head->next;
free(head);
head = NULL;
head = node;
if(head!=NULL)
{
return head;
}
else
{
return NULL;
}
}
if(strcmp(phead->data.key,key)==0)
{
node->next = phead->next;
free(phead);
phead = NULL;
return head;
}
else
{
node = phead;
phead = phead->next;
}
}
return NULL;
}
int chainlist_length(chainListType *head)
{
int length = 0;
chainListType *phead;
phead = head;
while(phead)
{
phead = phead->next;
length++;
}
return length;
}
- C语言学习之单向链表操作
- C语言之单向链表
- C语言之单向链表封装
- 最基本的单向链表操作 C语言
- C语言单向链表的操作(持续更新中)
- 单向链表基本操作(C语言实现)
- 使用C语言对单向链表的操作
- C语言中单向链表的相关操作
- C语言描述:单向链表的相关操作
- 单向链表的C语言实现与基本操作
- C语言单向链表
- C语言 单向链表
- C语言单向链表
- c语言-单向链表
- 单向链表(C语言实现)学习记录
- C语言创建单向链表之正向建链
- C语言创建单向链表之逆向建链
- iOS开发之C语言单向链表的建立
- spring 定时器配置
- 左右布局,背景图左侧定宽,自适应
- Two Sum
- linux nfs 挂载 配置
- C标准库源码解剖(9):控制函数assert.h, setjmp.h和signal.h
- C语言学习之单向链表操作
- html ContentType类型汇总
- Lake Counting
- 网上订餐系统饿了么、百度、美团外卖,订餐网站外卖
- 火力篮球游戏源码完整版--采用标准的游戏开发文档
- java中import两种导入类型比较
- 程序员准备面试时常犯的10个错误
- 图的邻接表的数组实现及其应用
- Android笔记-ListView总结(多选框ListViiew,动态加载,多线程更新ListView中的进度条)