#ifndef _ArrayList_H_
#define _ArrayList_H_
#define MaxSize 10
typedef int DataType;
typedef struct ArrayList{
DataType *list;
int size;//已有数目
int maxSize;//可容纳的最大数量}ArrayList;voidArrayListInit(ArrayList *arrayList){
arrayList->maxSize=MaxSize;
arrayList->list=(DataType *)malloc(arrayList->maxSize*sizeof(DataType));
arrayList->size=0;}//获取元素(通过索引)
DataType GetElement(ArrayList *arrayList,int index){//判断索引是否超过目前最大的数量if(index>=arrayList->maxSize){printf("error:over the maxsize of the list\n");//后续可以编写扩容函数替代return(DataType)0;}return arrayList->list[index];}//扩容voidEnlarge(ArrayList *arrayList){
int i;
DataType *tempList;
tempList=arrayList->list;
arrayList->list=(DataType *)malloc(2*arrayList->maxSize*sizeof(DataType));for(i=0;i<arrayList->maxSize;i++){
arrayList->list[i]=tempList[i];}
arrayList->maxSize=2*arrayList->maxSize;}//插入元素
int InsertElement(ArrayList *arrayList,int index,DataType data){//判断索引是否越界if(index>arrayList->size){printf("error:over the size of the list\n");return0;}//判断索引是否超过目前最大的数量if(index>=arrayList->maxSize){Enlarge(arrayList);}
arrayList->list[index]=data;(arrayList->size)++;return1;}//删除元素(通过索引)
int DeleteElement(ArrayList *arrayList,int index){
int i;//判断索引是否超过目前最大的数量if(index>=arrayList->size){printf("error:over the maxsize of the list\n");return0;}for(i=index;i<arrayList->size;i++){
arrayList->list[i]=arrayList->list[i+1];}(arrayList->size)--;return1;}
#endif
测试结果
链表
LinkedList.h
#ifndef _LinkedList_H_
#define _LinkedList_H_
typedef int DataType;//节点
typedef struct Node{
DataType data;//数据域
struct Node *next;//下一个节点}Node;//初始化链表voidLinkedListInit(Node **head){(*head)=(Node *)malloc(sizeof(Node));(*head)->next=NULL;}//返回链表的大小
int Size(Node *headNode){
Node *tempNode=headNode;
int num=0;while(tempNode->next){
tempNode=tempNode->next;
num++;}
tempNode=NULL;return num;}//删除节点1(通过索引)
int DeleteNode(Node *headNode,int index){//判断索引是否越界
int num=Size(headNode);if(num<index-1&&index<0){printf("error:index over the range");return0;}
Node *delNode,*tempNode=headNode;for(int i=0;i<index-1;i++)
tempNode=tempNode->next;
delNode=tempNode->next;
tempNode->next=delNode->next;//释放资源和指针free(delNode);
delNode=NULL;
tempNode=NULL;return1;}//在(末尾)增加节点voidAddNode(Node *headNode,DataType data){
Node *tempNode=headNode;//创建插入的节点
Node *insertNode=(Node *)malloc(sizeof(Node));
insertNode->data=data;
insertNode->next=NULL;while(tempNode->next){
tempNode=tempNode->next;}
tempNode->next=insertNode;//释放指针
tempNode=NULL;
insertNode=NULL;}//插入节点(通过索引),返回是否插入成功
int InsertNode(Node *headNode,int index,DataType data){//判断索引是否越界
int num=Size(headNode);if(num<index&&index<0){printf("error:index over the range");return0;}
Node *insertNextNode,*tempNode=headNode;//创建插入的节点
Node *insertNode=(Node *)malloc(sizeof(Node));
insertNode->data=data;for(int i=0;i<index-1;i++)
tempNode=tempNode->next;
insertNextNode=tempNode->next;
tempNode->next=insertNode;
insertNode->next=insertNextNode;//释放指针
tempNode=NULL;
insertNode=NULL;
insertNextNode=NULL;}//获取链表元素(通过索引)
DataType GetElement(Node *headNode,int index){//判断索引是否越界
int i;
DataType element;
Node *tempNode=headNode;
int num=Size(headNode);if(num<index-1&&index<0){printf("error:index over the range");return(DataType)0;}for(i=0;i<index;i++){
tempNode=tempNode->next;}
element=tempNode->data;
tempNode=NULL;return element;}//输出链表voidShow(Node *headNode){
Node *tempNode=headNode;while(tempNode->next){
tempNode=tempNode->next;printf("%d\n",tempNode->data);}//释放指针
tempNode=NULL;}
#endif
测试结果
栈和队列
栈
顺序表存储的栈
ArrayStack.h
#ifndef _ArrayStack_H_
#define _ArrayStack_H_
#define MaxSize 10//初始最大数目
typedef int DataType;//自定义数据类型
typedef struct ArrayStack{
DataType *array;
int size;//已有数目
int maxSize;//可容纳最大数目}ArrayStack;voidArrayStackInit(ArrayStack *arrayStack){
arrayStack->maxSize=MaxSize;
arrayStack->array=(DataType *)malloc(arrayStack->maxSize*sizeof(DataType));
arrayStack->size=0;}//判断栈是否为空
int IsEmpty(ArrayStack *arrayStack){return arrayStack->size==0?1:0;}//弹出元素
DataType Pop(ArrayStack *arrayStack){if(IsEmpty(arrayStack)){printf("error:ArrayStack is NULL\n");return(DataType)0;}
arrayStack->size--;return arrayStack->array[arrayStack->size];}//扩容voidEnlarge(ArrayStack *arrayStack){
DataType *tempArray=arrayStack->array;
int i;
arrayStack->maxSize=2*arrayStack->maxSize;
arrayStack->array=(DataType *)malloc(2*arrayStack->maxSize*sizeof(DataType));for(i=0;i<arrayStack->size;i++){
arrayStack->array[i]=tempArray[i];}
arrayStack->size++;//释放资源free(tempArray);
tempArray=NULL;}//添加元素voidPush(ArrayStack *arrayStack,DataType data){if(arrayStack->size==arrayStack->maxSize){Enlarge(arrayStack);}
arrayStack->array[arrayStack->size]=data;
arrayStack->size++;}
#endif