您的位置:首页 > 理论基础 > 数据结构算法

C数据结构部分代码

2013-10-23 14:46 162 查看

//============================================================================// Name        : flibs.h// Author      : Swair// Version     :// Copyright   : Your copyright notice// Description : Study C and C++, Ansi-style//===========================================================================

#include <stdio.h>#include <string.h>#include <iostream>using namespace std;

#define FOREVER while(1)

typedef unsigned int uint32;typedef long int lint;

//#define _List_H;/*==========================链表====================================*/#ifdef _List_Htypedef int ElementType;

//构建节点typedef struct Node{ElementType Element;struct Node *Next;} *List,*Position;

List MakeEmpty(List L);int IsEmpty(List L);int IsLast(Position P,List L);Position Find(ElementType X,List L,Position P);void Delete(ElementType X,List L);Position FindPrevious(ElementType X,List L);void Insert(ElementType X,List L,Position P);void DeleteList(List L);Position Header(List L);Position First(List L);Position Advance(Position P);ElementType Retrieve(Position P);#endif/*_List_H*/

//#define _Cursor_H;/*==========================游标====================================*/#ifdef _Cursor_H#define SpaceSize 1024typedef int ElementType;struct Node;struct Node CursorSpace[SpaceSize];

typedef int PtrToNode;typedef PtrToNode List;typedef PtrToNode Position;

void InitialzeCursorSpace(void);List MakeEmpty(List L);int IsEmpty(const List L);int IsLast(const Position P,const List L);Position Find(ElementType X,const List L);void Delete(ElementType X,List L);Position FindPrevious(ElementType X,const List L);void Insert(ElementType X,List L,Position P);void DeleteList(List L);Position Header(const List L);Position First(const List L);Position Advance(const Position P);ElementType Retreve(const Position P);#endif/*_Cursor_H*///#define _Stack_H;/*==========================栈====================================*/#ifdef _Stack_Htypedef int ElementType;struct Node;typedef struct Node *PtrToNode;typedef PterToNode Stack;

int IsEmpty(Stack S);Stack CreateStack(void);void DisposeStack(Stack S);void MakeEmpty(Stack S);void Push(ElementType X,Stack S);void Pop(Stack S);#endif/*_Stack_H*/

/*==========================队列====================================*///#define _Queue_H#ifdef _Queue_Htypedef int ElementType;typedef struct Node{ ElementType Element; struct Node *Next;} *Position;typedef struct QueueArray{Position Front;Position Rear;} *Queue;

int IsEmpty(Queue Q);int IsFull(Queue Q);Queue Create(int MaxElements);void DisposeQueue(Queue Q);void MakeEmpty(Queue Q);void Enqueue(ElementType X,Queue);ElementType Front(Queue Q);void Dequeue(Queue Q);ElementType FrontAndDequeue(Queue Q);

#endif/*_Queue_H*/

//#define _BTree_H#ifdef _BTree_H/*==========================二叉树====================================*/typedef int ElementType;typedef struct TreeNode{ElementType Element;struct TreeNode *Left;struct TreeNode *Right;} *Position, *SearchTree;

SearchTree MakeEmpty(SearchTree T);Position Find(ElementType X,SearchTree T);Position FindMin(SearchTree T);Position FindMax(SearchTree T);SearchTree Insert(ElementType X,SearchTree T);SearchTree Delete(ElementType X,SearchTree T);ElementType Retrieve(Position P);

void PrintTree(SearchTree T);void PrintElement(ElementType X);#endif /*_BTree_H*/

/*==========================散列表====================================*///#define _HashSep_H#ifdef _HashSep_H

#define MinTableSize 1024typedef char* ElementType;

typedef struct ListNode{ElementType Element;struct ListNode *Next;} *Position,*List;typedef struct HashTbl{int TableSize;List *TheLists;} *HashTable;

HashTable InitializeTable(int TableSize);void DestroyTable(HashTable H);Position Find(ElementType Key,HashTable H);void Insert(ElementType Key,HashTable H);ElementType Retrieve(Position P);

typedef unsigned int Index;Index Hash(const char *Key,int TableSize);

#endif /*_HashSep_H*/

/*==========================二叉堆-优先队列====================================*/#define _BinHeap_H#ifdef _BinHeap_H#define MinPQSize 100#define MinData 100typedef int ElementType;typedef struct HeapStruct{int Capacity;int Size;ElementType *Elements;} *PriorityQueue;PriorityQueue Initialize(int MaxElements);void Destroy(PriorityQueue H);void MakeEmpty(PriorityQueue H);void Insert(ElementType X,PriorityQueue H);ElementType DeleteMin(PriorityQueue H);int IsEmpty(PriorityQueue H);int IsFull(PriorityQueue H);

#endif

int F(int x);

void PrintOut_PS(uint32 N);void PrintOut_NS(uint32 N);void PrintDigit(uint32 N);

int Binary_Search(const int A[],int X,int N);

uint32 Gcd(uint32 M,uint32 N);

lint Pow(lint X,uint32 N);bool IsEven(int N);

lint Fac(uint32 n);void swap(int*x, int*y);void test();
//============================================================================// Name : flibs.cpp// Author : Swair// Version :// Copyright : Your copyright notice// Description : Study C and C++, Ansi-style//===========================================================================

#include "flibs.h"

/*==========================链表====================================*/#ifdef _List_H

//测试链表是否为空int IsEmpty(List L){return L->Next==NULL;}//测试当前位置是否末尾int IsLast(Position P,List L){return P->Next==NULL;}//查找XPosition Find(ElementType X,List L){Position P;P=L->Next;while(P!=NULL&&P->Element!=X)P=P->Next;return P;}

//删除Xvoid Delete(ElementType X,List L){Position P,TmpCell;P=FindPrevious(X,L);if(!IsLast(P,L)){TmpCell=P->Next;P->Next=TmpCell->Next;free(TmpCell);}}//查找X的前驱元素Position FindPrevious(ElementType X,List L){Position P;P=L;while(P->Next!=NULL&&P->Next->Element!=X)P=P->Next;return P;}

//在P位置后插入Xvoid Insert(ElementType X,List L,Position P){Position TmpCell=(Position)malloc(sizeof(struct Node));if(TmpCell==NULL)printf("Out of Space!!!");TmpCell->Element=X;TmpCell->Next=P->Next;P->Next=TmpCell;}

void DeleteList(List L){Position P,Tmp;P=L->Next;L->Next=NULL;while(P!=NULL){Tmp=P->Next;free(P);P=Tmp;}}

Position Header(List L){return L;}

Position First(List L){return L->Next;}

Position Advance(Position P){

}

ElementType Retrieve(Position P){

}#endif/*_List_H*/

/*==========================游标====================================*/#ifdef _Cursor_Hstruct Node{ElementType Element;Position Next;};

#endif/*_Cursor_H*/

/*==========================栈====================================*/#ifdef _Stack_Hstruct Node{ElementType Element;PtrToNode Next;};int IsEmpty(Stack S){return S->Next==NULL;}Stack Create(void){Stack S;S=malloc(sizeof(struct Node));if(S==NULL)printf("Out of space!!!");S->Next==NULL;MakeEmpty(S);return S;}void MakeEmpty(Stack S){if(S==NULL)printf("Must use CreateStack first");elsewhile(!IsEmpty(S))Pop(S);}

void Push(ELementType X,Stack S){PtrToNode TmpCell;TmpCell=malloc(sizeof(struct Node));if(TmpCell==NULL)printf("Out of space!!!");else{TmpCell->Element=X;TmpCell->Next=S->Next;S->Next=TmpCell;}}ElementType Top(Stack S){if(!IsEmpty(S))return S->Next->Element;printf("Empty stack");return 0;}void Pop(Stack S){PtrToNode FirstCell;if(IsEmpty(S))printf("Empty stack");else{FirtCell=S->Next;S->Next=S->Next->Next;free(FirstCell);}}#endif/*_Stack_H*/

/*==========================队列====================================*/#ifdef _Queue_H

int IsEmpty(Queue Q){return Q->Front == Q->Rear;}int IsFull(Queue Q){return Q->Front==Q->Rear->Next;}

void MakeEmpty(Queue Q){Q->Front=Q->Rear;}

void Enqueue(ElementType X,Queue Q){if(IsFull(Q))printf("Full queue");else{Position Tmp=(Position)malloc(sizeof(struct Node));if(Tmp==NULL)printf("Queue Full");else{Tmp->Element=X;Tmp->Next=NULL;Q->Rear->Next=Tmp;Q->Rear=Tmp;}}}

ElementType FrontAndDequeue(Queue Q){if(!IsEmpty){ElementType Tmp;Tmp=Q->Front->Element;Q->Front=Q->Front->Next;return Tmp;}}#endif/*_Queue_H*/

/*==========================二叉树====================================*/#ifdef _BTree_H

SearchTree MakeEmpty(SearchTree T){if(T!=NULL){MakeEmpty(T->Left);MakeEmpty(T->Right);free(T);}return NULL;}Position Find(ElementType X,SearchTree T){if(T==NULL)return NULL;if(X<T->Element)return Find(X,T->Left);else if(X>T->Element)return Find(X,T->Right);elsereturn T;}//递归实现Position FindMin(SearchTree T){if(T==NULL)return NULL;else if(T->Left==NULL)return T;elsereturn FindMin(T->Left);}//非递归实现Position FindMax(SearchTree T){if(T!=NULL)while(T->Right!=NULL)T=T->Right;return T;}SearchTree Insert(ElementType X,SearchTree T){if(T==NULL){Position T=(Position)malloc(sizeof(struct TreeNode));if(T==NULL)printf("Out of space");else{T->Element=X;T->Left=T->Right=NULL;}}else if(X<T->Element)T->Left=Insert(X,T->Left);else if(X>T->Element)T->Right=Insert(X,T->Right);return T;}SearchTree Delete(ElementType X,SearchTree T){Position TmpCell;if(T==NULL)printf("Element not found");else if(X<T->Element)T->Left=Delete(X,T->Left);else if(X>T->Element)T->Right=Delete(X,T->Right);else if(T->Left&&T->Right){TmpCell=FindMin(T->Right);TmpCell=T;if(T->Left==NULL)T=T->Right;else if(T->Right==NULL)T=T->Left;free(TmpCell);}return T;}

void PrintTree(SearchTree T){if(T!=NULL){PrintTree(T->Left);PrintElement(T->Element);PrintTree(T->Right);}}

void PrintElement(ElementType X){printf("%d\n",X);}

#endif /*_BTree_H*/

/*==========================散列表====================================*/#ifdef _HashSep_H

Index Hash(const char *Key,int TableSize){unsigned int HashVal=0;while(*Key!='\0')HashVal=(HashVal<<5)+*Key++;return HashVal%TableSize;}

HashTable InitializeTable(int TableSize){HashTable H;int i;if(TableSize<MinTableSize){printf("Table size too small");return NULL;}H=(HashTable)malloc(sizeof(struct HashTbl));if(H==NULL)printf("Out of space!!!");H->TableSize=TableSize;// H->TableSize=NextPrime(TableSize);

H->TheLists=(List*)malloc(sizeof(List) * H->TableSize);if(H->TheLists==NULL)printf("Out of space!!!");for(i=0;i<H->TableSize;i++){H->TheLists[i]=(List)malloc(sizeof(struct ListNode));if(H->TheLists[i]==NULL)printf("Out of space!!!");elseH->TheLists[i]->Next=NULL;}return H;}Position Find(ElementType Key, HashTable H){Position P;List L;L=H->TheLists[Hash(Key,H->TableSize)];P=L->Next;while(P!=NULL&&P->Element!=Key)P=P->Next;return P;}

void Insert(ElementType Key,HashTable H){Position Pos,NewCell;List L;Pos=Find(Key,H);if(Pos==NULL){NewCell=(Position)malloc(sizeof(struct ListNode));if(NewCell==NULL)printf("Out of spcace!!!");else{L=H->TheLists[Hash(Key,H->TableSize)];NewCell->Next=L->Next;NewCell->Element=Key;L->Next=NewCell;}}}

#endif /*_HashSep_H*/

/*==========================二叉堆-优先队列====================================*/#ifdef _BinHeap_H

PriorityQueue Initialize(int MaxElements){PriorityQueue H;if(MaxElements<MinPQSize)printf("Priority queue size is too small");H=(PriorityQueue)(malloc(sizeof(struct HeapStruct)));if(H==NULL)printf("Out of space!!!");H->Elements=(ElementType*)malloc((MaxElements+1)*sizeof(ElementType));if(H->Elements==NULL) printf("Out of space!!!");H->Capacity=MaxElements;H->Size=0;H->Elements[0]=MinData;return H;}

#endif

//递归int F(int x){if(x==0)return 0;elsereturn 2*F(x-1)+x*x;}

//打印正序数void PrintOut_PS(uint32 N){if(N>=10)PrintOut_PS(N/10);PrintDigit(N%10);}

//打印逆序数void PrintOut_NS(uint32 N){PrintDigit(N%10);if(N>=10)PrintOut_NS(N/10);}

//打印单个数字void PrintDigit(uint32 N){if(N<10)printf("%d",N);elseprintf("Error");}

//二分查找int Binary_Search(const int A[],int X,int N){int low,mid,high;low=0;high=N-1;while(low<=high){mid=(low+high)/2;if(A[mid]<X)low=mid+1;else if(A[mid]>X)high=mid-1;elsereturn mid;}return -1;}//求最大公约数uint32 Gcd(uint32 M,uint32 N){uint32 Rem;while(N>0){Rem=M%N;M=N;N=Rem;}return M;}

//幂运算lint Pow(lint X,uint32 N){if(N==0)return 1;if(IsEven(N))return Pow(X*X,N/2);elsereturn Pow(X,N-1)*X;}//判断奇偶bool IsEven(int N){if(N%2==0)return 1;elsereturn 0;}

void swap(int*x, int*y){ int temp=*y; *y=*x; *x=temp;}

lint Fac(uint32 n){if(n<=0)return -1;else if(n==1)return 1;elsereturn n*Fac(n-1);}

void test(){ printf("***************************Test Starting****************************\n"); printf("************************swap*************************\n"); int a=3; int b=5; printf("Before swap: a=%d, b=%d\n",a,b); swap(&a,&b); printf("Afer swap: a=%d, b=%d\n",a,b);

printf("***********************String*************************\n"); char *ptr="hello,world"; printf("%s:\n",ptr);

unsigned int i=0; for(i=0;i<strlen(ptr);i++) { printf(" %x ",ptr+i); printf(" %c ",ptr[i]); printf(" %c \n",*(ptr+i)); } printf("***********************Pointer****************************\n");

int array[5]={1,2,3,4,5}; printf("%d,%d,%d,%d,%d\n",array[0],array[1],array[2],array[3],array[4]); for(i=0;i<sizeof(array)/sizeof(int);i++) { printf(" %x, %d\n",array+i,*(array+i));

} printf("\n"); char*pt=(char*)array; for(i=0;i<sizeof(array);i++) { printf(" %x: %x\n",pt+i,*(pt+i)); }

int inb=520; printf("%d:\n",inb); for(i=0;i<sizeof(inb);i++) { printf(" %x: %x\n",((char*)&inb)+i,*(((char*)&inb)+i)); }

printf("*******************The Valuables*******************\n"); char *type; printf("char*type%d\n:",sizeof(type)); printf("int%d\n:",sizeof(int));

char*mmm="abc"; int nnn[3]={2,3,4};

printf("%x, %x\n",mmm,mmm+1); printf("%x, %x\n",nnn,nnn+1); printf("***************************Test ending****************************\n"); printf("\n\n");}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: