DataStructure-用指针实现List
来源:互联网 发布:mac适合的浏览器 编辑:程序博客网 时间:2024/05/16 05:51
具体实现代码:
#include<stdio.h>#include<malloc.h>typedef int ListItem;typedef struct node *link;//定义节点类型typedef struct node{ ListItem element;//节点存储的元素 link next;//直接下一个元素的指针} Node;typedef struct Ilist *List;typedef struct Ilist//定义表结构{ link first;} Llist;link NewNode(){//新建一个节点类型 link p; if((p=malloc(sizeof(Node)))==0) printf("exhausted memory\n"); else return p;}List ListInit(){//初始化一个表 List L=malloc(sizeof *L);//分配内存 L->first=0; return L;}int LIstEmpty(List L){//判断表是否为空, return L->first==0;}int ListLength(List L){//返回数组的长度 int len=0; link p; p=L->first; while(p){ len++; p=p->next; } return len;}ListItem ListRetrieve(int k,List L){//list中第K位置的元素 int i; link p; if(k<1) printf("out of bounds\n"); p=L->first; i=1; while(i<k&&p){ p=p->next; i++; } return p->element;}int ListLocate(ListItem x,List L){//元素x在list中的位置 int i; link p; p=L->first; while(p && p->element != x){ p=p->next; i++; } return p ? i : 0;}void ListInsert(int k,ListItem x,List L){//在list的第K处插入元素x link p,y; int i; if(k<0) printf("out of bounds\n"); p=L->first; for(i=1;i<k&&p;i++) {p=p->next;} y=NewNode(); y->element=x; if(k){ y->next=p->next; p->next=y; } else{ y->next=L->first; L->first=y; }}ListItem ListDelete(int k,List L){//删除list重第K个元素 ListItem x; link p,q; int i; if(k<1||!L->first) printf("out of bounds\n"); p=L->first; if(k==1) L->first=p->next; else{ q=L->first; for(i=1;i<k-1&&q;i++) q=q->next; p=q->next; q->next=p->next; x=p->element; free(p); return x; }}void printList(List L){//打印list中的所有的元素 link p; for(p=L->first;p;p=p->next) printf("%d\n",p->element );}void main(){ List L=ListInit(); ListInsert(0,0,L); ListInsert(0,2,L); ListInsert(1,1,L); printList(L); printf("list is LIstEmpty? -> %d\n",LIstEmpty(L) ); printf("list length is-> %d\n",ListLength(L)); printf("list %d item is ->%d\n",2,ListRetrieve(2,L) ); printf("list item %d in ->%d\n",2,ListLocate(2,L) ); ListDelete(2,L); printList(L);}
0 0
- DataStructure-用指针实现List
- DataStructure-用数组实现List
- C Datastructure 1 ---- linear list
- 【DataStructure】Descriptioin and usage of List
- 【DataStructure】Another usage of List: Polynomial
- C Datastructure 2 ---- link list(1)
- C Datastructure 3 ---- link list(2)
- C Datastructure 4 ---- link list(3)
- DataStructure----LinerList,线性表C实现
- DataStructure---LinkList 链表C语言实现代码
- Java 数据结构实现(DataStructure)1
- Java 数据结构实现(DataStructure)2
- 【DataStructure】 Five methods to init the List in java
- useful datastructure:
- All DataStructure
- Game - datastructure
- 【datastructure】栈。
- Python4:DataStructure
- Github汇总
- HDOJ 1896 Stones(优先队列)
- C++11新特性:可变参数模板
- 【整理】堆栈空间, 进程内存布局
- Controller的写法
- DataStructure-用指针实现List
- 特效1: 鼠标移动到该区域则显示小图标
- 散列表学习
- jQuery选择器
- 【Java基础】类变量(静态变量)、成员变量、局部变量的区分理解及存储分类
- mock测试
- HDU 5739 Fantasia 2016多校
- 取样问题
- Android Volley完全解析(一),初识Volley的基本用法