有码2----链表基本操作(C)
2016-02-01 21:32
537 查看
包含两部份,”head.c” and “sqList.c”
英文很烂,所有注释都是随性而写。。
head.h:
linkList.c:
英文很烂,所有注释都是随性而写。。
head.h:
/******************** * author : tiaoyu * datetime : 16:22 2016/1/11 * desc : List *******************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define SUCCESS 1 #define FAIL 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; typedef int ElemType; int max(int a, int b){return a>b?a:b;} int min(int a, int b){return a<b?a:b;} //is or not a is equal b Status compare(ElemType a, ElemType b){return a==b?TRUE:FALSE;} //print e Status visit(ElemType e) {printf("%d | ", e);}
linkList.c:
/************************* * author : tiaoyuyu * datatime: 14:30 2016/1/14 * dec: LinkList ************************/ #include "head.h" typedef int ElemType; //LinkList's node typedef struct LNode{ ElemType data; struct LNode *next; }*Link, *Position; //LinkList struct typedef struct { //List's head and tail (LNode) Link head, tail; //List's length (int) int len; }LinkList; //create Node and data is e Status MakeNode(Link &p, ElemType e){ //malloc memory for LNode p = (Link)malloc(sizeof(LNode)); if(!p) exit(ERROR); p->data = e; return SUCCESS; } //free Node void FreeNode(Link &p){ free(p); /*Must set p as NULL, just in case p become being wild pointer(Dangerous)*/ p = NULL; } /**************** pointer use "->" struct use "." so LinkList L; L use ".", L.head... and Link pHead; pHead use "->", pHead->data... *init LinkList creat a head and a tail length is zero head --> tail *****************/ Status InitList(LinkList &L){ Link head, tail; head = (Link)malloc(sizeof(LNode)); if(!head) exit(ERROR); head->data = 0; tail = (Link)malloc(sizeof(LNode)); if(!tail) exit(ERROR); tail->data = 0; L.head = head; L.tail = tail; L.head->next = L.tail; L.tail->next = NULL; L.len = 2; } /*********************** destroy LinkList form head to tail destroy all node; ***********************/ Status DestroyList(LinkList &L){ Link p = L.head; while(NULL != L.head){ L.head = L.head->next; FreeNode(p); p = L.head; } L.head = NULL; L.tail = NULL; L.len = 0; return SUCCESS; } /****************** clear LinkList keep head and tail , clear (head, tail); *******************/ Status ClearList(LinkList &L){ if(0 == L.len) return SUCCESS; Link p = L.head->next; L.head->next = L.tail; while(NULL != p){ Link q = p; FreeNode(p); p = q->next; L.len --; } return SUCCESS; } /****************** insert Node to first h is head, s is insert-node ******************/ Status InsFirst(Link h, Link s){ Link p = h->next; h->next = s; s->next = p; return OK; } //delete head Node and return it to q Status DelFirst(Link h, Link &q){ q = (Link)malloc(sizeof(LNode)); q->data = h->data; q->next = h->next; free(h); h = NULL; return OK; } //append LinkList L to Node s Status Append(LinkList &L, Link s){ Link p = L.head; while(p->next != L.tail){ p = p->next; } p->next = s; s->next = L.tail; return OK; } //delete tail Node and return it to q Status DelLast(LinkList &L, Link &q){ q = (Link)malloc(sizeof(LNode)); q->data = L.tail->data; q->next = NULL; free(L.tail); L.tail = NULL; return OK; } //insert Node s before Node p in LinkList L Status InsBefore(LinkList &L, Link &p, Link s){ Link q = L.head; while(q->next != p){ q = q->next; } q->next = s; s->next = p; return OK; } //insert Node s after Node p in LinkList LNode Status InsAfter (LinkList &L, Link &p, Link s){ Link q = p->next; p->next = s; s->next = q; } //change data of Node p to e Status SetCurElem(Link &p, ElemType e){ p->data = e; return OK; } //get data to Node p ElemType GetCurElem(Link p){ return p->data; } //judge LinkList if empty Status ListEmpty(LinkList L){ return (L.head->next == L.tail); } //get length of Node in LinkList int ListLength(LinkList L){ return L.len; } //get position of Head Node Position GetHead (LinkList L){ return L.head; } //get position of tail Node Position GetLast(LinkList L){ return L.tail; } //get priority Node Position PriorPos(LinkList L, Link p){ Link q = L.head; while(q->next != p){ q = q->next; } return q; } //get next Node Position NextPos(LinkList L, Link p){ Link q = L.head; while(q->next != p){ q = q->next; } return q->next->next; } //get Node of index i, and return p Status LocatePos(LinkList L, int i, Link &p){ p = L.head; while(i--){ p = p->next; } return OK; } //get data equal to e Position LocateElem(LinkList L, ElemType e, Status (*compare)(ElemType, ElemType)){ Link p = L.head; while(p){ if(compare(e, p->data)){ return p; } p = p->next; } return p; } //print LinkList L Status ListTraverse(LinkList L, Status(*visit)(ElemType e)){ Link p; p = L.head; while(p){ visit(p->data); p = p->next; } } int main(){ LinkList L; //init List InitList(L); //DestroyList(...); /** printf("head = %p, tail = %p, len = %d\n", L.head, L.tail, L.len); DestroyList(L); printf("head = %p, tail = %p, len = %d\n", L.head, L.tail, L.len); **/ //DelFirst(...); /** Link q; DelFirst(L.head, q); printf("%d", q->data); **/ //DelFirst(...); /** Link q; DelFirst(L.head, q); printf("%d", q->data); **/ return 0; }
相关文章推荐
- WebStorm常用快捷键
- YTU 2760: 字符串---首字母变大写
- Codeforces Round #341 (Div. 2) ABCDE
- Linux--字符设备驱动结构框图
- 4 sum题解(K sum)
- poj1041 John's trip(欧拉回路+输出路径)
- 51nod 1284 2 3 5 7的倍数
- 2016年1月份 英语新的起点
- 一起talk C栗子吧(第一百一十七回:C语言实例--线程死锁一)
- LayoutInflater.inflate及View.inflate一点小结----(当使用listview,recycleView时发现也有些不同)
- YTU 2750: 猜算式
- day03 作业
- Broadleaf电商系统开发(五) - Http Api
- C++ 可变参数函数
- 11.Swift 中的类和结构体
- 哈夫曼树 1
- C语言二维数组和指针的关系
- HDU 1257 最少拦截系统
- 51nod 1344 走格子
- $scope