链表创建及增加节点

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include "zmalloc.h"
  4 //定义节点
  5 typedef struct listNode {
  6     struct listNode *prev;
  7     struct listNode *next;
  8     void *value;
  9 } listNode;
 10
 11 //定义链表
 12 typedef struct list {
 13     listNode *head;
 14     listNode *tail;
 15     void *(*dup)(void *ptr);
 16     void (*free)(void *ptr);
 17     int (*match)(void *ptr, void *key);
 18     unsigned long len;
 19 } list;
 20
 21 //创建一个空链表
 22 list *listCreate(void)
 23 {
 24     struct list *list;
 25
 26     if ((list = zmalloc(sizeof(*list))) == NULL)
 27         return NULL;
 28     list->head = list->tail = NULL;
 29     list->len = 0;
 30     list->dup = NULL;
 31     list->free = NULL;
 32     list->match = NULL;
 33     return list;
 34 }
 35
 36 /* Add a new node to the list, to head, containing the specified ‘value‘
 37  * pointer as value.
 38  *
 39  * On error, NULL is returned and no operation is performed (i.e. the
 40  * list remains unaltered).
 41  * On success the ‘list‘ pointer you pass to the function is returned. */
 42 list *listAddNodeHead(list *list, void *value)
 43 {
 44     listNode *node;
 45
 46     if ((node = zmalloc(sizeof(*node))) == NULL)
 47         return NULL;
 48     node->value = value;
 49     if (list->len == 0) {
 50         list->head = list->tail = node;
 51         node->prev = node->next = NULL;
 52     } else {
 53         node->prev = NULL;
 54         node->next = list->head;
 55         list->head->prev = node;
 56         list->head = node;
 57     }
 58     list->len++;
 59     return list;
 60 }
 61
 62 /* Add a new node to the list, to tail, containing the specified ‘value‘
 63  * pointer as value.
 64  *
 65  * On error, NULL is returned and no operation is performed (i.e. the
 66  * list remains unaltered).
 67  * On success the ‘list‘ pointer you pass to the function is returned. */
 68 list *listAddNodeTail(list *list, void *value)
 69 {
 70     listNode *node;
 71
 72     if ((node = zmalloc(sizeof(*node))) == NULL)
 73         return NULL;
 74     node->value = value;
 75     if (list->len == 0) {
 76         list->head = list->tail = node;
 77         node->prev = node->next = NULL;
 78     } else {
 79         node->prev = list->tail;
 80         node->next = NULL;
 81         list->tail->next = node;
 82         list->tail = node;
 83     }
 84     list->len++;
 85     return list;
 86 }
 87
 88 /* Free the whole list.
 89  *
 90  * This function can‘t fail. */
 91 void listRelease(list *list)
 92 {
 93     unsigned long len;
 94     listNode *current, *next;
 95
 96     current = list->head;
 97     len = list->len;
 98     while(len--) {
 99         next = current->next;
100         if (list->free) list->free(current->value);
101         zfree(current);
102         current = next;
103     }
104     zfree(list);
105 }
106
107 int main()
108 {
109     list *keys = listCreate();
110
111     char buf[1024];
112     strcpy(buf,"i am zhaoja");
113     char buf2[1024];
114     strcpy(buf2,"i am xm");
115
116     listAddNodeHead(keys,buf);
117     listAddNodeHead(keys,buf2);
118
119     printf("keys head is %s\n",keys->head->value);
120     printf("keys tail is %s\n",keys->tail->value);
121     printf("keys len is %d\n",keys->len);
122
123     char buf3[1024];
124     strcpy(buf3,"i am x3");
125     char buf4[1024];
126     strcpy(buf4,"i am x4");
127
128     listAddNodeTail(keys,buf3);
129     listAddNodeTail(keys,buf4);
130
131     printf("keys head is %s\n",keys->head->value);
132     printf("keys tail is %s\n",keys->tail->value);
133     printf("keys len is %d\n",keys->len);
134
135     listRelease(keys);
136     return 0;
137 }
138
139 /*
140 测试代码
141 [[email protected] List]# gcc listcreate.c zmalloc.c
142 [[email protected] List]# ./a.out
143 keys head is i am xm
144 keys tail is i am zhaoja
145 keys len is 2
146 keys head is i am xm
147 keys tail is i am x4
148 keys len is 4
149
150 */
时间: 2024-11-13 05:16:15

链表创建及增加节点的相关文章

无表头单链表的总结----增加节点(原链表为有序的链表)

1 #include"head.h" 2 struct Student* insert(struct Student*head, struct Student*addinfo) 3 { 4 struct Student *p0, *p1, *p2; //开辟三个结构体指针 5 p2=p1 = head; //头指针赋给p1,p2 6 p0 = addinfo; //把 增加信息的指针赋给p0,习惯不用原来指针 7 if (head == NULL) 8 { 9 head = p0 //

创建元素节点,文本节点以及在指定位置增加节点

no1.创建一个新的元素节点,并把它添加到body中 1 window.onload = t; 2 function t(){ 3 var nodeli = document.createElement('li'); 4 document.body.appendChild(nodeli); 5 nodeli.style.cssText = 'width:300px;background:red;'; 6 } 上面的显示效果为: no2.创建一个元素节点,创建一个文本节点,将文本节点追加到元素节点

单向非循环链表:链表创建、节点插入、链表打印、节点长度计算、链表清空、链表销毁

/* 单向非循环链表:    初始化    前插入 后插入    打印    链表长度    清空    销毁*/#include <stdio.h>#include <stdlib.h> #define itemType int typedef struct node{    itemType data;    struct node *pNext;}Node; /* 创建Head节点: 节点的pNext为NULL */int initList(Node **ppN){    N

链表创建和链表遍历算法的演示_C语言

今天搞了一个多小时,头是疼的,应该是没休息好吧,学习了数据结构这一节,感觉收益良多,下面贴上代码和心得: 1 /*24_链表创建和链表遍历算法的演示*/ 2 # include <stdio.h> 3 # include <malloc.h> 4 # include <stdlib.h> 5 6 typedef struct Node 7 { 8 int data;//数据域 9 struct Node * pNext;//指针域 10 }NODE, *PNODE;//

单链表二[不带头节点链表]

不带头节点链表 单向链表是链表的一种.单向链表由一系列内存不连续的节点组成,每个节点都包含指向值的域和指向下个节点的next指针.最后一个节点的next域为NULL值,代表链表结束. 链表示意图如下: 一,结构体 1,结构体定义: struct LinkNode {     void *x;      struct LinkNode *next; }; 2,结构体大小: 1)取结构体大小方法:sizeof(struct LinkNode);在64位系统取出结构体大小为:sizeof(struct

链表(创建,插入,删除和打印输出(转载)

链表(创建,插入,删除和打印输出 /*----------------------------------------------------------------------------- 文件功能:实现了动态建立一个学生信息的链表包括链表的创建.插入.删除.和打印输出学生信息包括姓名和分数本链表是带有头结点的,头结点的内容为空内容-----------------------------------------------------------------------------*//*

链表 创建 插入 删除 查找 合并

最近学习了一下单链表的操作,将代码保存如下,供以后查看. 链表创建: 1.先建立一个不含数据的头指针*head,头指针的链接域为NULL. 2.声明一个用于暂存新申请空间的指针*pc,一个用于保存创建的链表的指针*r,令*r指向*head. 3.在循环中,为指针*pc申请空间,并给数据域赋值,head->next = pc, pc->next = NULL, head = pc. #define _CRT_SECURE_NO_DEPRECATE /*取消scanf,printf不安全之类的错误

greenplum集群安装与增加节点生产环境实战

1.准备环境 1.1集群介绍 系统环境:centos6.5 数据库版本:greenplum-db-4.3.3.1-build-1-RHEL5-x86_64.zip greenplum集群中,2台机器IP分别是 [[email protected] ~]# cat /etc/hosts 127.0.0.1   localhost localhost.localdomain ::1         localhost localhost.localdomain localhost6 localhos

SUSE Ceph 增加节点、减少节点、 删除OSD磁盘等操作 - Storage6

一.测试环境描述 之前我们已快速部署好一套Ceph集群(3节点),现要测试在现有集群中在线方式增加节点 如下表中可以看到增加节点node004具体配置 主机名 Public网络 管理网络 集群网络 说明 admin 192.168.2.39 172.200.50.39 --- 管理节点 node001 192.168.2.40 172.200.50.40 192.168.3.40 MON,OSD node002 192.168.2.41 172.200.50.41 192.168.3.41 MO