C语言学习之单向链表操作

该文件为单向链表操作的一些接口:(如发现有错误的地方,及时告知,不胜感激!)

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;

}



时间: 2024-12-18 07:36:31

C语言学习之单向链表操作的相关文章

单向链表操作 原文:http://canlynet.blog.163.com/blog/static/255013652009113001041903/

/********************************************** 功能:单向链表操作(注意Head指针 需要被改变时传入的是二级指针) 日期:2009.12.29 作者:DC ***********************************************/ #include <stdio.h> #include <stdlib.h> #define OVER_FLOW -2 #define OK 0 typedef int ElemTy

文章分享:简单数据结构学习:单向链表

文章分享:简单数据结构学习:单向链表:https://www.textarea.com/aprikyb/jiandan-shujujiegou-xuexi-danxiang-lianbiao-252/

Perl语言学习笔记 13 目标操作

1.改变目录 chdir "/etc" or die "can't chdir to '/etc'!\n"; 省略参数会回到用户主目录,与cd效果一样: 2.文件名通配 my @all_files = glob "*"; #不包括以点号开头的文件 my @pm_files = glob "*.pm"; 一次匹配多种模式,用空格隔开:my @files = ".* *"; #可以匹配所有的文件,包括以点号开头

C语言之字符单向链表

/* * @Author: suifengtec * @Date:   2017-09-02 16:06:33 * @Last Modified by:   suifengtec * @Last Modified time: 2017-09-02 20:47:13 **/ /* 字符单向链表 gcc -o a.exe main.c && a  */ #include <stdio.h> #include <stdlib.h> #include <stdbool

用C++ “包装” 后的单向链表操作

下面是用以实现类的代码: Node.h 1 #ifndef __NODE_H__ 2 /*单向 链表*/ 3 #define __NODE_H__ 4 5 typedef struct _NODE 6 { 7 int data; 8 struct _NODE *next; 9 }node; 10 11 class Node 12 { 13 public: 14 Node(); 15 ~Node(); 16 //外部使用的公有函数部分 17 public: 18 19 /*重载add,分别为在表后

再谈单向链表操作——单向链表的访问与修改

链表的访问 既然理解了链表的构成,对链表的访问也就不成问题了.不过要特别注意的是,对于数组,我们可以利用下标直接访问任何一个元素(这被称为"随机访问"),而对于单向链表,只能从头结点开始,依次进行"顺序访问". 假设我们已经按照前文创建了一个链表,下面我们设计一个函数,查找是否存在某数据: //参数说明:p为头指针,n为所查找的数据 void search_node(l* p, int n) { //若非尾结点,则继续循环 while(p->next != N

C语言学习之用链表实现通讯录

本程序主要功能是对联系人信息进行,添加.删除.查找.插入.显示功能 说明:调用的链表操作接口请参考:http://blog.csdn.net/qlx846852708/article/details/43482497 这里面有我实现的链表操作的接口的详细实现过程,并进行过测试的哦!!! #include <stdio.h> #include <stdlib.h> #include <string.h> #include "addressBookList.h&q

Snail—数据结构学习之单向链表

// 定义链表的数据结构 typedef struct _LINK_NODE{ int data; struct _LINK_NODE * next; }LINK_NODE; //创建一个单向链表 LINK_NODE * create(){ int inputFlag = 1,value; LINK_NODE *head,*p,*s; head = (LINK_NODE *)malloc(sizeof(LINK_NODE)); p = head; while (inputFlag) { prin

Java单向链表操作详解

转自:http://blog.csdn.net/zxman660/article/details/7786354 —————————————————————————————————————————— /* 先定义一个Node类用来存储节点的值域和指针域 * 即当前节点中的值和后面节点的方法 * 在C中就是相当与定义一个结构体类型一个数据域和指针域的方法 */ class LNode{//这个写法已经非常固定了 设置两个属性分别用set函数和get函数来得到这两个属性 private int da