码迷,mamicode.com
首页 > 其他好文 > 详细

c链表

时间:2018-11-21 19:44:29      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:技术   rev   aci   open   img   color   spl   结果   分享图片   

一、单项链表的实现

 

struct list *create_list()//建立一个节点

void traverse(struct list *ls)//循环遍历链表

struct list *insert_list(struct list *ls, int n, int data)//在指定位置插入元素

int delete_list(struct list *ls, int n)//删除指定位置元素

int count_list(struct list *ls)//返回链表元素个数

void clear_list(struct list *ls)//清空链表,只保留首节点

int empty_list(struct list *ls)//返回链表是否为空

struct list *locale_list(struct list *ls, int n)//返回链表指定位置的节点

struct list *elem_locale(struct list *ls, int data)//返回数据域等于data的节点

int elem_pos(struct list *ls, int data)//返回数据域等于data的节点位置

struct list *last_list(struct list *ls)//得到链表最后一个节点

void merge_list(struct list *st1, struct list *ls2)//合并两个链表,结果放入st1中

void reverse(struct list *ls)//链表逆置

 
   

 

 

1.建立一个节点

技术分享图片
 1 # include <stdio.h>
 2 # include <stdlib.h>
 3 # include <string.h>
 4 # pragma warning(disable:4996)
 5 
 6 struct list
 7 {
 8     int data; //数据域
 9     struct list *next; //指针域,指向下一个节点的地址
10 };
11 
12 //struct list *create_list2() //建立一个节点
13 //{
14 //    struct list *p = (struct list *)malloc(sizeof(struct list));
15 //    p->data = 0;
16 //    p->next = NULL;
17 //    return p;
18 //}
19 
20 struct list *create_list()//建立一个节点
21 {
22     return calloc(sizeof(struct list), 1);
23 }
24 
25 int main()
26 {
27     struct list *first = create_list();//创建链表的首节点
28     struct list *second = create_list();//创建下一个节点
29     struct list *third = create_list(); //创建下一个节点
30 
31     first->next = second;
32     second->next = third;
33 
34     free(second); //删除第二个节点
35     first->next = third;//删除之后得让第一个指向第三个
36 
37 
38     system("pause");
39     return 0;
40 }
建立节点

技术分享图片

 

c链表

标签:技术   rev   aci   open   img   color   spl   结果   分享图片   

原文地址:https://www.cnblogs.com/zenglingbing/p/9996783.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!