次序表
sqlite.h
- #ifndef __SEQLIST_H__
- #define __SEQLIST_H__
- typedef struct person {
- char name[32];
- char sex;
- int age;
- int score;
- }DATATYPE;
- // typedef int DATAYPE;
- typedef struct list {
- DATATYPE *head;
- int tlen;
- int clen;
- }SeqList;
- SeqList *CreateSeqList(int len);
- int DestroySeqList(SeqList *list);
- int ShowSeqList(SeqList *list);
- int InsertTailSeqList(SeqList *list, DATATYPE* data);
- int IsFullSeqList(SeqList *list);
- int IsEmptySeqList(SeqList *list);
- int InsertPosSeqList(SeqList *list, DATATYPE* data, int pos);
- int FindSeqList(SeqList *list, char *name);
- int ModifySeqList(SeqList *list, char *old, DATATYPE *newdata);
- int DeleteSeqList(SeqList *list, char *name);
- int ClearSeqList(SeqList *list);
- int GetSizeSeqList(SeqList*list);
- DATATYPE* GetDataType(SeqList*list,int pos);
- #endif
复制代码 seqlite.c
- #include "seqlist.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- SeqList *CreateSeqList(int len)
- {
- SeqList* sl = (SeqList*)malloc(sizeof(SeqList));
- if(NULL == sl)
- {
- printf("malloc 1 error\n");
- return NULL;
- }
- sl->head = (DATATYPE*)malloc(sizeof(DATATYPE)*len);
- if(NULL == sl->head)
- {
- printf("malloc 1 error\n");
- return NULL;
- }
- sl->tlen = len;
- sl->clen = 0;
- return sl;
- }
- int InsertTailSeqList(SeqList *list, DATATYPE* data)
- {
-
- if(IsFullSeqList(list))
- {
- return 1;
- }
- //strcpy
- memcpy(&list->head[list->clen],data,sizeof(DATATYPE));
- list->clen++;
- return 0;
- }
- int IsFullSeqList(SeqList* list)
- {
- return list->clen == list->tlen ;
- }
- int GetSizeSeqList(SeqList*list)
- {
- return list->clen ;
- }
- int ShowSeqList(SeqList *list)
- {
- int i = 0 ;
- int len = GetSizeSeqList(list);
- for(i = 0;i<len;i++)
- {
-
- printf("%s %c %d %d\n",list->head[i].name,
- list->head [i].sex,list->head[i].age,list->head[i].score );
- }
- }
- int IsEmptySeqList(SeqList *list)
- {
- return 0 ==list->clen ;
- }
- int InsertPosSeqList(SeqList *list, DATATYPE *data, int pos)
- {
- if(pos<0 ||pos>list->clen)
- {
- return 1;
- }
- if(IsFullSeqList(list))
- {
- return 1;
- }
- for(int i= list->clen ;i>pos;i--)
- {
- list->head[i]= list->head[i-1];
- }
- memcpy(&list->head[pos],data,sizeof(DATATYPE));
- list->clen ++;
- return 0;
- }
- int FindSeqList(SeqList *list, char *name)
- {
- int len = GetSizeSeqList(list);
- int i = 0 ;
- for(i=0;i<len;i++)
- {
-
- if(0== strcmp(list->head [i].name,name))
- {
-
- return i;
- }
- }
- return -1;
- }
- DATATYPE* GetDataType(SeqList*list,int pos)
- {
- return &list->head[pos];
- }
- int ClearSeqList(SeqList *list)
- {
- list->clen = 0;
- return 0;
- }
- int DestroySeqList(SeqList *list)
- {
- free(list->head);
- free(list);
- return 0;
- }
- int ModifySeqList(SeqList *list, char *old, DATATYPE *newdata)
- {
- int i = FindSeqList(list,old);
- if(-1 == i)
- {
- return 1;
- }
- //list->head[i] = *newdata;
- memcpy(&list->head[i],newdata,sizeof(DATATYPE));
- return 0;
- }
- int DeleteSeqList(SeqList *list, char *name)
- {
- int pos = FindSeqList(list, name);
- if(-1 == pos)
- {
- return 1;
- }
- int len =GetSizeSeqList(list);
- int i = 0 ;
- for(i=pos;i<len;i++)
- {
- memcpy(&list->head[i],&list->head[i+1],sizeof(DATATYPE));
- }
- list->clen--;
- return 0;
- }
复制代码 单链表
linklist.c
- #include "linklist.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- LinkList *CreateLinkList() {
- LinkList *ll = (LinkList *)malloc(sizeof(LinkList));
- if (NULL == ll) {
- printf("CreateLinkList malloc\n");
- return NULL;
- }
- ll->head = NULL;
- ll->clen = 0;
- return ll;
- }
- int InsertHeadLinkList(LinkList *list, DATATYPE *data) {
- LinkNode *newnode = (LinkNode *)malloc(sizeof(LinkNode));
- if (NULL == newnode) {
- printf("InsertHeadLinkList malloc\n");
- return 1;
- }
- memcpy(&newnode->data, data, sizeof(DATATYPE));
- newnode->next = NULL;
- if (IfEmptyLinkList(list)) // empty
- {
- list->head = newnode;
- } else {
- newnode->next = list->head;
- list->head = newnode;
- }
- list->clen++;
- return 0;
- }
- int IfEmptyLinkList(LinkList *list) { return 0 == list->clen; }
- int ShowLinkList(LinkList *list) {
- LinkNode *tmp = list->head;
- while (tmp) {
- printf("%s %d\n", tmp->data.name, tmp->data.age);
- tmp = tmp->next; // i++
- }
- return 0;
- }
- // LinkNode *FindLinkList(LinkList *list, char *name)
- LinkNode *FindLinkList(LinkList *list, PFUN fun, void *arg) {
- LinkNode *tmp = list->head;
- while (tmp) {
- // if(0==strcmp(tmp->data.name,name))
- if (fun(&tmp->data, arg)) {
- return tmp;
- }
- tmp = tmp->next; // i++
- }
- return NULL;
- }
- int InsertTailLinkList(LinkList *list, DATATYPE *data) {
- LinkNode *newnode = (LinkNode *)malloc(sizeof(LinkNode));
- if (NULL == newnode) {
- printf("InsertTailLinkList malloc\n");
- return 1;
- }
- memcpy(&newnode->data, data, sizeof(DATATYPE));
- newnode->next = NULL;
- if (IfEmptyLinkList(list)) {
- list->head = newnode;
- } else {
- LinkNode *tmp = list->head;
- while (tmp->next) {
- tmp = tmp->next;
- }
- tmp->next = newnode;
- }
- list->clen++;
- return 0;
- }
- int InsertPosLinkList(LinkList *list, DATATYPE *data, int pos) {
- int len = GetSizeLinkList(list);
- if (pos < 0 || pos > len) {
- return 1;
- }
- if (0 == pos) {
- return InsertHeadLinkList(list, data);
- } else if (pos == len) {
- return InsertTailLinkList(list, data);
- } else //中间位置
- {
- LinkNode *newnode = (LinkNode *)malloc(sizeof(LinkNode));
- if (NULL == newnode) {
- printf("InsertTailLinkList malloc\n");
- return 1;
- }
- memcpy(&newnode->data, data, sizeof(DATATYPE));
- newnode->next = NULL;
- LinkNode *tmp = list->head;
- int i = 0;
- for (i = 0; i < pos - 1; i++) {
- tmp = tmp->next;
- }
- newnode->next = tmp->next;
- tmp->next = newnode;
- }
- list->clen++;
- return 0;
- }
- int GetSizeLinkList(LinkList *list) { return list->clen; }
- int DeleteHeadLinklist(LinkList *list) {
- if (IfEmptyLinkList(list)) {
- return 1;
- }
- LinkNode *tmp = list->head;
- list->head = tmp->next;
- free(tmp);
- list->clen--;
- return 0;
- }
- int DeleteTailLinkList(LinkList *list) {
- if (IfEmptyLinkList(list)) {
- return 1;
- }
- int len = GetSizeLinkList(list);
- if (1 == len) {
- free(list->head);
- list->head = NULL;
- } else {
- LinkNode *tmp = list->head;
- LinkNode *tmp1 = list->head;
- while (tmp->next) {
- tmp1 = tmp; // tmp1 比tmp晚一步
- tmp = tmp->next;
- }
- free(tmp);
- tmp1->next = NULL;
- }
- list->clen--;
- return 0;
- }
- int DeletePosLinkList(LinkList *list, int pos) {
- int len = GetSizeLinkList(list);
- if (IfEmptyLinkList(list)) {
- return 1;
- }
- if (0 == pos) {
- DeleteHeadLinklist(list);
- } else if (len - 1 == pos) // len 4
- {
- DeleteTailLinkList(list);
- } else //中间
- {
- LinkNode *tmp = list->head;
- LinkNode *tmp1 = list->head;
- for (int i = 0; i < pos; i++) {
- tmp1 = tmp;
- tmp = tmp->next;
- }
- tmp1->next = tmp->next;
- free(tmp);
- list->clen--;
- }
- return 0;
- }
- int ModifyLinkList(LinkList *list, PFUN fun, void *arg, DATATYPE *newdata) {
- LinkNode *tmp = FindLinkList(list, fun, arg);
- if (NULL == tmp) {
- return 1;
- }
- memcpy(&tmp->data, newdata, sizeof(DATATYPE));
- return 1;
- }
- int DestroyLinkList(LinkList *list) {
- int len = GetSizeLinkList(list);
- for (int i = 0; i < len; i++) {
- DeleteHeadLinklist(list);
- }
- free(list);
- return 0;
- }
- LinkNode *FindMidLinkList(LinkList *list) {
- if (IfEmptyLinkList(list)) {
- return NULL;
- }
- LinkNode *pfast = list->head;
- LinkNode *pslow = list->head;
- while (pfast) {
- pfast = pfast->next; // pfast = pfast->next->next;
- if (pfast) {
- pfast = pfast->next;
- pslow = pslow->next;
- } else {
- break;
- }
- }
- return pslow;
- }
- LinkNode *FindRevKLinkList(LinkList *list, int k) {
- if (IfEmptyLinkList(list)) {
- return NULL;
- }
- LinkNode *pfast = list->head;
- LinkNode *pslow = list->head;
- int i = 0;
- for (i = 0; i < k; i++) {
- pfast = pfast->next;
- if (NULL == pfast) {
- return pslow;
- }
- }
- while (pfast) {
- pfast = pfast->next;
- // if (NULL==pfast)
- // {
- // break;
- // }
- pslow = pslow->next;
- }
- return pslow;
- }
- int RevertLinkList(LinkList *list) {
- LinkNode *prev = NULL;
- LinkNode *tmp = list->head;
- LinkNode *next = list->head->next;
- while (1) {
- tmp->next = prev;
- prev = tmp;
- tmp = next;
- if (NULL == tmp) {
- break;
- }
- next = next->next;
- }
- list->head = prev;
- return 0;
- }
- int InsertSortLinkList(LinkList *list)
- {
- LinkNode* phead = list->head;
- LinkNode* pinsert = phead->next;
- LinkNode* pnext = pinsert->next;
- phead->next = NULL;
- while(1)
- {
- phead = list->head;
- while(phead->next != NULL && pinsert->data.age > phead->data.age &&
- pinsert->data.age > phead->next->data.age)
- {
- phead = phead->next;
- }
- if(pinsert->data .age <phead -> data.age) // head insert
- {
- pinsert->next = list->head;
- list->head = pinsert;
- }
- else
- {
- pinsert->next = phead->next;
- phead->next = pinsert;
- }
- pinsert = pnext;
- if(NULL == pinsert)
- { break; }
- pnext = pnext->next;
-
- }
- return 0;
- }
复制代码 linklist.h
- #ifndef __LINKLIST_H__
- #define __LINKLIST_H__
- typedef struct person
- {
- char name[32];
- char sex;
- int age;
- int score;
- }DATATYPE;
- typedef struct node
- {
- DATATYPE data;
- struct node *next;
- }LinkNode;
- //typedef int u32;
- typedef struct list
- {
- LinkNode *head;
- int clen;
- }LinkList;
- typedef int (*PFUN)(DATATYPE*,void* arg);
- LinkList *CreateLinkList();
- int InsertHeadLinkList(LinkList *list, DATATYPE *data);
- int ShowLinkList(LinkList *list);
- //LinkNode *FindLinkList(LinkList *list, char *name);
- LinkNode *FindLinkList(LinkList *list, PFUN fun, void * arg);
- int DeleteHeadLinklist(LinkList *list);
- int DeleteTailLinkList(LinkList *list);
- int DeletePosLinkList(LinkList *list, int pos);
- int ModifyLinkList(LinkList *list, PFUN fun, void * arg,DATATYPE*newdata);
- int DestroyLinkList(LinkList *list);
- int InsertTailLinkList(LinkList *list, DATATYPE *data);
- int IfEmptyLinkList(LinkList *list);
- int InsertPosLinkList(LinkList *list, DATATYPE *data,int Pos);
- int GetSizeLinkList(LinkList *list);
- LinkNode* FindMidLinkList(LinkList *list);
- LinkNode* FindRevKLinkList(LinkList *list,int k);
- int RevertLinkList(LinkList *list);
- int InsertSortLinkList(LinkList *list) ;
- #endif // /__LINKLIST_H__/ !
复制代码 双链表
doulinklist.c
- #include "doulinklist.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- DouLinkList *CreateDouLinkList() {
- DouLinkList *dl = (DouLinkList *)malloc(sizeof(DouLinkList));
- if (NULL == dl) {
- printf("CreateDouLinkList malloc\n");
- return NULL;
- }
- dl->head = NULL;
- dl->clen = 0;
- return dl;
- }
- int IsEmptyDouLinkList(DouLinkList *list) { return 0 == list->clen; }
- int GetSizeDouLinkList(DouLinkList *list) { return list->clen; }
- int InsertHeadDouLinkList(DouLinkList *list, DATATYPE *data) {
- DouLinkNode *newnode = (DouLinkNode *)malloc(sizeof(DouLinkNode));
- if (NULL == newnode) {
- printf("InsertHeadDouLinkList malloc\n");
- return 1;
- }
- memcpy(&newnode->data, data, sizeof(DATATYPE));
- newnode->next = NULL;
- newnode->prev = NULL;
- if (IsEmptyDouLinkList(list)) {
- list->head = newnode;
- } else {
- newnode->next = list->head;
- list->head->prev = newnode;
- list->head = newnode;
- }
- list->clen++;
- return 0;
- }
- int ShowDouLinkList(DouLinkList *list, SHOW_DIR dir) {
- DouLinkNode *tmp = list->head;
- if (DIR_FORWARD == dir) {
- while (tmp) {
- printf("%s %d\n", tmp->data.name, tmp->data.score);
- tmp = tmp->next;
- }
- } else {
- while (tmp->next) {
- tmp = tmp->next;
- }
- while (tmp) {
- printf("%s %d\n", tmp->data.name, tmp->data.score);
- tmp = tmp->prev;
- }
- }
- return 0;
- }
- int InsertTailDouLinkList(DouLinkList *list, DATATYPE *data) {
- if (IsEmptyDouLinkList(list)) {
- return InsertHeadDouLinkList(list, data);
- }
- DouLinkNode *tmp = list->head;
- while (tmp->next) {
- tmp = tmp->next;
- }
- DouLinkNode *newnode = (DouLinkNode *)malloc(sizeof(DouLinkNode));
- if (NULL == newnode) {
- printf("InsertTailDouLinkList malloc\n");
- return 1;
- }
- memcpy(&newnode->data, data, sizeof(DATATYPE));
- newnode->next = NULL;
- newnode->prev = NULL;
- newnode->prev = tmp;
- tmp->next = newnode;
- list->clen++;
- return 0;
- }
- /*
- func:
- param:
- retval:
- */
- int InsertPosDouLinkList(DouLinkList *list, DATATYPE *data, int pos) {
- int len = GetSizeDouLinkList(list);
- if (pos < 0 || pos > len) {
- return 1;
- }
- if (0 == pos) {
- return InsertHeadDouLinkList(list, data);
- } else if (len == pos) {
- return InsertTailDouLinkList(list, data);
- } else {
- DouLinkNode *tmp = list->head;
- for (int i = 0; i < pos; i++) {
- tmp = tmp->next;
- }
- DouLinkNode *newnode = malloc(sizeof(DATATYPE));
- if (NULL == newnode) {
- printf("InsertPosDouLinkList malloc\n");
- return 1;
- }
- memcpy(&newnode->data, data, sizeof(DATATYPE));
- newnode->next = NULL;
- newnode->prev = NULL;
- newnode->next = tmp;
- newnode->prev = tmp->prev;
- tmp->prev = newnode;
- newnode->prev->next = newnode;
- }
- list->clen++;
- return 0;
- }
- int DeleteHeadDouLinkList(DouLinkList *list) {
- if (IsEmptyDouLinkList(list)) {
- return 1;
- }
- DouLinkNode *tmp = list->head;
- list->head = list->head->next;
- if (list->head != NULL) {
- list->head->prev = NULL;
- }
- free(tmp);
- list->clen--;
- return 0;
- }
- int DeleteTailDouLinkList(DouLinkList *list) {
- if (IsEmptyDouLinkList(list)) {
- return 1;
- }
- DouLinkNode *tmp = list->head;
- while (tmp->next) {
- tmp = tmp->next;
- }
- if (tmp->prev != NULL) {
- tmp->prev->next = NULL;
- } else {
- list->head = NULL;
- }
- free(tmp);
- list->clen--;
- return 0;
- }
- int DeletePosDouLinkList(DouLinkList *list, int pos) {
- int len = GetSizeDouLinkList(list);
- if (pos < 0 || pos > len - 1) {
- return 1;
- }
- if (0 == pos) {
- return DeleteHeadDouLinkList(list);
- } else if (pos == len - 1) {
- return DeleteTailDouLinkList(list);
- } else // mid del
- {
- DouLinkNode *tmp = list->head;
- for (int i = 0; i < pos; i++) {
- tmp = tmp->next;
- }
- tmp->next->prev = tmp->prev;
- tmp->prev->next = tmp->next;
- free(tmp);
- list->clen--;
- }
- return 0;
- }
- DouLinkNode *FindDouLinkList(DouLinkList *list, char *name)
- {
- if(IsEmptyDouLinkList(list))
- {
- return NULL;
- }
- DouLinkNode* tmp = list->head;
- while(tmp)
- {
- if(0 == strcmp(tmp->data.name,name))
- {
- return tmp;
- }
- tmp=tmp->next;
- }
- return NULL;
- }
- int ModifyDouLinkList(DouLinkList *list, char *name, DATATYPE* data)
- {
- DouLinkNode* tmp = FindDouLinkList(list, name);
- if(NULL == tmp)
- {
- return 1;
- }
- memcpy(&tmp->data,data,sizeof(DATATYPE));
- return 0;
- }
- int DestroyDouLinkList(DouLinkList *list)
- {
- int len = GetSizeDouLinkList(list);
- for(int i = 0 ;i<len;i++)
- {
- DeleteHeadDouLinkList(list);
- }
- free(list);
- return 0;
- }
复制代码 doulinklist.h
- #ifndef __DOULINKLIST_H__
- #define __DOULINKLIST_H__
- typedef struct person {
- char name[32];
- char sex;
- int age;
- int score;
- }DATATYPE;
- typedef struct dounode {
- DATATYPE data;
- struct dounode *next,*prev;
- }DouLinkNode;
- typedef struct list {
- DouLinkNode *head;
- int clen;
- }DouLinkList;
- typedef enum{DIR_FORWARD,DIR_BACKWARD} SHOW_DIR;
- DouLinkList *CreateDouLinkList();
- int InsertHeadDouLinkList(DouLinkList *list, DATATYPE* data);
- int InsertTailDouLinkList(DouLinkList *list, DATATYPE* data);
- int InsertPosDouLinkList(DouLinkList *list, DATATYPE* data,int pos);
- int ShowDouLinkList(DouLinkList *list,SHOW_DIR dir);
- DouLinkNode *FindDouLinkList(DouLinkList *list, char *name);
- int DeleteHeadDouLinkList(DouLinkList *list) ;
- int DeleteTailDouLinkList(DouLinkList *list);
- int DeletePosDouLinkList(DouLinkList *list,int pos);
- int ModifyDouLinkList(DouLinkList *list, char *name, DATATYPE* data);
- int DestroyDouLinkList(DouLinkList *list);
- int IsEmptyDouLinkList(DouLinkList *list);
- int GetSizeDouLinkList(DouLinkList *list);
- #endif
复制代码 链式栈
linkstack.c
- #include "linkstack.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- LinkStack* CreateLinkStack()
- {
- LinkStack* ls = (LinkStack*)malloc(sizeof(LinkStack));
- if(NULL == ls)
- {
- printf("CreateLinkStack malloc");
- return NULL;
- }
- ls->top = NULL;
- ls->clen= 0 ;
- return ls;
-
- }
- int PushLinkStack(LinkStack*ls,DATATYPE*data)
- {
- LinkStackNode* newnode = (LinkStackNode*)malloc(sizeof(LinkStackNode));
- if(NULL == newnode)
- {
- printf("PushLinkStack malloc");
- return 1;
- }
- memcpy(&newnode->data,data,sizeof(DATATYPE));
- newnode->next = NULL;
- newnode->next = ls->top;
- ls->top = newnode;
- ls->clen++;
- return 0;
- }
- int IsEmptyLinkStack(LinkStack*ls)
- {
- return 0 == ls->clen;
- }
- int PopLinkStack(LinkStack*ls)
- {
- if(IsEmptyLinkStack(ls))
- {
- return 1;
- }
- LinkStackNode* tmp = ls->top;
- ls->top = ls->top->next;
- free(tmp);
- ls->clen--;
- return 0;
- }
- DATATYPE*GetTopLinkStack(LinkStack*ls)
- {
- if(IsEmptyLinkStack(ls))
- {
- return NULL;
- }
- return &ls->top->data;
- }
- int GetSizeLinkStack(LinkStack*ls)
- {
- return ls->clen;
- }
复制代码 linkstack.h
- #ifndef __LINKSTACK_H__
- #define __LINKSTACK_H__
- typedef struct person
- {
- char name[32];
- char sex;
- int age;
- int score;
- }DATATYPE;
- typedef struct stacknode
- {
- DATATYPE data;
- struct stacknode *next;
- }LinkStackNode;
- typedef struct list
- {
- LinkStackNode *top;// 和单向链表中的head,一个意思
- int clen;
- }LinkStack;
- LinkStack* CreateLinkStack();
- int DestroyLinkStack(LinkStack*ls);
- int PushLinkStack(LinkStack*ls,DATATYPE*data);
- int PopLinkStack(LinkStack*ls);
- int IsEmptyLinkStack(LinkStack*ls);
- DATATYPE*GetTopLinkStack(LinkStack*ls);
- int GetSizeLinkStack(LinkStack*ls);
- #endif // /__LINKLIST_H__/ !
-
复制代码 队列
SeqQueue.c
- #include "SeqQueue.h"
- SeqQueue *CreateSeqQueue(int len)
- {
- SeqQueue* sq = (SeqQueue*)malloc(sizeof(SeqQueue));
- if(NULL == sq)
- {
- printf("CreateSeqQueue malloc\n");
- return NULL;
- }
- sq->ptr = (DATATYPE*)malloc(sizeof(DATATYPE)*len);
- if(NULL == sq->ptr )
- {
-
- printf("CreateSeqQueue malloc2\n");
- return NULL;
- }
- sq->tlen = len;
- sq->head = 0 ;
- sq->tail = 0;
- return sq;
- }
- int IsEmptySeqQueue(SeqQueue *queue)
- {
- return queue->head == queue->tail ;
- }
- int IsFullSeqQueue(SeqQueue *queue)
- {
- return (queue->tail +1) % queue->tlen == queue->head;
- }
- int QuitSeqQueue(SeqQueue *queue)
- {
- if(IsEmptySeqQueue(queue))
- {
- return 1;
- }
- queue->head = (queue->head+1)%queue->tlen;
- return 0;
- }
- int EnterSeqQueue(SeqQueue *queue, DATATYPE *data)
- {
- if(IsFullSeqQueue(queue))
- {
- return 1;
- }
- memcpy(&queue->ptr[queue->tail],data,sizeof(DATATYPE));
- queue->tail = (queue->tail +1) % queue->tlen ;
- return 0;
- }
- DATATYPE* GetHeadSeqQueue(SeqQueue* que)
- {
- if(IsEmptySeqQueue(que))
- {
- return NULL;
- }
- return &que->ptr[que->head];
- }
复制代码 SeqQueue.h
- #ifndef __SEQQUEUE_H__
- #define __SEQQUEUE_H__
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef int DATATYPE;
- typedef struct queue {
- DATATYPE *ptr;
- int tlen;
- int head;
- int tail;
- }SeqQueue;
- SeqQueue *CreateSeqQueue(int len);
- int DestroySeqQueue(SeqQueue *queue);
- int QuitSeqQueue(SeqQueue *queue);
- int EnterSeqQueue(SeqQueue *queue, DATATYPE *data);
- int IsEmptySeqQueue(SeqQueue *queue);
- int IsFullSeqQueue(SeqQueue *queue);
- DATATYPE* GetHeadSeqQueue(SeqQueue* que);
- #endif
复制代码 树
tree.c
- #include <stdio.h>
- #include <stdlib.h>
- typedef char DATATYPE;
- typedef struct BiTNode /* 结点结构 */
- {
- DATATYPE data; /* 结点数据 */
- struct BiTNode *lchild,*rchild; /* 左右孩子指针 */
- }BiTNode;
- char data[]="Ab#df###ceg##h###";
- int ind = 0;
- void CreateBiTree(BiTNode** root)
- {
- char c = data[ind++];
- if('#'==c)
- {
- *root = NULL;
- return ;
- }
- *root = (BiTNode*)malloc(sizeof(BiTNode));
- if(NULL == *root)
- {
- perror("malloc");
- return ;
- }
- (*root)->data = c;
- CreateBiTree(&((*root)->lchild));
- CreateBiTree(&((*root)->rchild));
- return ;
-
- }
- void DestroyBiTree(BiTNode * root)
- {
- if(NULL ==root)
- {
- return ;
- }
- DestroyBiTree(root->lchild);
- DestroyBiTree(root->rchild);
- free(root);
- }
- void PreOrderTraverse(BiTNode * root)
- {
- if(NULL == root)
- {
- return ;
- }
- printf("%c",root->data);
- PreOrderTraverse(root->lchild);
- PreOrderTraverse(root->rchild);
- }
- void InOrderTraverse(BiTNode * root)
- {
- if(NULL == root)
- {
- return ;
- }
- InOrderTraverse(root->lchild);
- printf("%c",root->data);
- InOrderTraverse(root->rchild);
- }
- void PostOrderTraverse(BiTNode * root)
- {
- if(NULL == root)
- {
- return ;
- }
- PostOrderTraverse(root->lchild);
- PostOrderTraverse(root->rchild);
- printf("%c",root->data);
- }
- int main(int argc, char **argv)
- {
-
- BiTNode* root=NULL;
- CreateBiTree(&root);
- PreOrderTraverse(root);
- printf("\n");
- InOrderTraverse(root);
- printf("\n");
- PostOrderTraverse(root);
- printf("\n");
- DestroyBiTree(root);
- root = NULL;
- //system("pause");
- return 0;
- }
复制代码 哈希表
hash.c
- #include <stdio.h>
- #include <stdlib.h>
- typedef int DATATYPE;
- typedef struct
- {
- DATATYPE* head;
- int tlen;
- }HS_TABLE;
- HS_TABLE* CreateHsTable(int len)
- {
- HS_TABLE* hs = malloc(sizeof(HS_TABLE));
- if(NULL == hs)
- {
- perror("CreateHsTable malloc1");
- return NULL;
- }
- hs->head = malloc(sizeof(DATATYPE)*len);
- if(NULL == hs->head)
- {
- perror("CreateHsTable malloc2");
- return NULL;
- }
- hs->tlen = len;
- int i = 0 ;
- for(i=0;i<len;i++)
- {
- hs->head[i] = -1;
- }
- return hs;
- }
- int HS_fun(HS_TABLE* hs,DATATYPE* data)
- {
- return *data %hs->tlen;
- }
- int HS_insert(HS_TABLE* hs,DATATYPE* data)
- {
- int ind = HS_fun(hs,data);
- while(hs->head[ind]!= -1)
- {
- printf("values:%d conllision pos:%d\n",*data,ind);
- ind = (ind+1) %hs->tlen;
- }
- hs->head[ind] = *data;
- return 0;
- }
- int HS_search(HS_TABLE* hs,DATATYPE* data)
- {
- int ind = HS_fun(hs,data);
- int oldind = ind;
- while(hs->head[ind]!=*data )
- {
- ind = (ind+1) %hs->tlen;
- if(oldind == ind)
- {
- return -1;
- }
- }
- return ind;
- }
- int main(int argc, char **argv)
- {
-
- HS_TABLE* hs = CreateHsTable(12);
- int data[12]={12,67,56,16,25,37,22,29,15,47,48,34};
- int i = 0;
- for(i=0;i<12;i++)
- {
- HS_insert(hs, &data[i]);
- }
- int want_int = 44;
- int ind = HS_search(hs, &want_int);
- if(-1 == ind)
- {
- printf("cant find %d\n",want_int);
- }
- else
- {
- printf("find it ,pos %d\n",ind);
- }
-
- return 0;
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |