数据结构.单链表(C语言实现)
#include<stdio.h>#include<stdlib.h>#include<malloc.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2#define Elemtype inttypedef int Status;typedef struct LNode {Elemtype data;struct LNode *next;}LNode, *LinkList;//No.1 Init listLinkList InitList(){LinkList L = NULL;L = (LinkList)malloc(sizeof(LNode));if (!L){printf("***** Init Link list failed ! **** \n");exit(OVERFLOW);}L->next = NULL;printf("Init Link list ...\n");return L;}//No.2 Destroy Listvoid DestoryList(LinkList L){LinkList p = L;if (p == NULL){free(p);}while (p){L = p->next;free(p);p = L;}}// No.3 ClearListvoid ClearList(LinkList L){LinkList p = L;if (p == NULL){printf("List is NULL, clear list done !\n");exit(-1);}while ( p->next != NULL){p = L->next;free(L);L = p;}printf("clear list done !\n");}//No.4 List emptyStatus ListEmpty(LinkList L){LinkList p = L->next;if ( p == NULL){printf("Link is empty !\n");return FALSE;}else{return TRUE;printf("Link is not empty !\n");}}//No.5 List length, include Head numberint ListLength(LinkList L){int i = 0;LinkList p =L;if ( p == NULL){printf("Link is empty !\n");return FALSE;}while (p != NULL){p = p->next;++i;}return i;}//No.6 GetElemStatus GetElem_L(LinkList L, int i, Elemtype *e){int j=1;LinkList p = NULL;p = L->next;if (p && j < i){p =p->next;j++;}if (!p->next || j > i)return ERROR;*e = p->next->data;printf("node %d value : %d\n",i,*e);return OK;}//No.7 LocateElemint LocateElem(LinkList L, Elemtype e){LinkList p;int i = 1;p = L->next;while ( p != NULL && p->data != e){p = p->next;++i;}if (p){printf("system has found %d in %d node !\n",e,i);return i;}else{printf("%d is not in Link List !\n", e);return 0;}}//No.8 PriorElemint PriorElem(LinkList L, Elemtype current_value, Elemtype *prior_value){LinkList p,q;p = L->next;q = p;if (p ==NULL){printf("link list is empty, no Prior !\n");exit(-1);}while ( p != NULL && p->next->data != current_value){p = p->next;}if (p->next->data == current_value){*prior_value = p->data;printf("prior node value = %d\n",*prior_value);}return OK;}Status ListInsert_L(LinkList L, int i, Elemtype e){int j = 1;LinkList q = (LinkList) malloc(sizeof(LNode));if ( !q ){printf("system can not malloc memory !\n");return (OVERFLOW);}LinkList p = NULL;p = L->next;if (p && j < i-1){p =p->next;j++;}if (!p->next || j > i-1)return ERROR;q -> data = e;q->next = p->next;p->next = q;return OK;}<补充:软件开发 , C语言 ,
上一个:关于C语言指针的问题
下一个:C常见问题之结构的声明和结构变量的定义方式
- 更多C/C++疑问解答:
- 关于c++的cout输出的问题。
- 在学校里学过C和C++,不过学的很一般,现在自学C#,会不会很难?
- 全国计算机二级C语言笔试题
- 已知某树有2个2度结点,3个3度结点,4个4度结点,问有几个叶子结点?
- c++数据结构内部排序问题,整数排序
- 2012九月计算机二级C语言全国题库,,急求急求
- 如果assert只有一个字符串作为参数,是什么意思呢?
- C语言中,哪些运算符具有左结合性,哪些具有右结合性,帮忙总结下,谢谢了!
- 为什么用结构体编写的程序输入是,0输不出来啊~~~
- 将IEEE—754的十六进制转化为十进制浮点类型,用C或C++都行,多谢各位大侠啊,非常感谢!
- 为什么这个程序求不出公式?
- 这个链表倒置的算法请大家分析下
- c语言函数库调用
- C语言unsigned int纠错
- C语言快排求解啊