栈(Stack)
- stack.h
#ifndef _STACK_H_
#define _STACK_H_
// 结构
#define StackSize 100
typedef struct{
DataType data[StackSize];
int top;
}Stack;
// 定义
void initStack(Stack *s);
int stackEmpty(Stack s);
int putStack(Stack *s, DataType e);
int popStack(Stack *s, DataType *e);
int stackLength(Stack s);
void stackClear(Stack *s);
// 实体
// 初始化
void initStack(Stack *s){
s->top = 0;
}
// 判断空栈
int stackEmpty(Stack s){
if(s.top == 0)
return 1;
else
return 0;
}
// 入栈
int putStack(Stack *s, DataType e){
if(s->top>=StackSize){
printf("stack is full \n");
return 0;
}
else{
s->data[s->top]=e;
s->top++;
return 1;
}
}
// 取顶
DataType getStack(Stack s){
return s.data[s.top-1];
}
// 出栈
int popStack(Stack *s, DataType *e){
if(stackEmpty(*s)){
printf("stack is empty \n");
return 0;
}else{
s->top--;
*e = s->data[s->top];
return 1;
}
}
// 栈长度
int stackLength(Stack s){
return s.top;
}
// 情况栈
void stackClear(Stack *s){
s->top=0;
}
#endif
- main.c
#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
#include "stack.h"
int main(){
// 初始化
Stack s;
initStack(&s);
// 入栈
DataType e=10;
putStack(&s, e);
// 长度
printf("lenth=%d\n", stackLength(s));
// 出栈
DataType oe;
popStack(&s,&oe);
printf("%d\n", oe);
// 判断空
if (stackEmpty(s))
printf("stack is empty \n");
else
printf("stack no empty \n");
return 1;
}
列表(List)
list.h
#ifndef _LIST_H_
#define _LIST_H_
// 结构
#define ListSize 100
typedef struct{
datatype data[ListSize];
int length;
}List;
// 定义
void initList(List *l);
int listEmpty(List l);
int insertList(List *l, int idx ,datatype e);
int deleteList(List *l, int idx ,datatype *e);
int listLength(List l);
int listGetE(List l, int idx, datatype *e);
int listFindE(List l, datatype e);
void listrepr(List l);
// 实体
// 初始化
void initList(List *l){
l->length = 0;
}
// 判断空栈
int listEmpty(List l){
if(l.length == 0)
return 1;
else
return 0;
}
// 插值
int insertList(List *l, int idx ,datatype e){
if(idx<0 || idx>l->length){
printf("insert position illegal \n");
return -1;
}
else if(l->length >= ListSize){
printf("list is full \n");
return 0;
}
else{
int j=l->length;
for(;idx<j;j--)
l->data[j]=l->data[j-1];
l->data[idx]=e;
l->length++;
return 1;
}
}
// 删值
int deleteList(List *l, int idx ,datatype *e){
if(listEmpty(*l)){
printf("list is empyt \n");
return 0;
}else if(idx<0 || idx>l->length-1){
printf("insert position illegal \n");
return -1;
}else{
*e = l->data[idx];
int j=idx;
for(;j+1<l->length;j++)
l->data[j]=l->data[j+1];
l->length--;
return 1;
}
}
// list长度
int listLength(List l){
return l.length;
}
// 取值
int listGetE(List l, int idx, datatype *e){
if(idx<0 || idx>l.length-1){
printf("insert position illegal \n");
return -1;
}
*e = l.data[idx];
return 1;
}
// 招值
int listFindE(List l, datatype e){
int i=0;
for(;i<l.length;i++){
if(l.data[i]==e)
return i;
}
return -1;
}
// 输出
void listrepr(List l){
int i;
printf("<List>:[");
for(i=0;i<l.length;i++){
if(i==l.length-1)
printf("%d",l.data[i]);
else
printf("%d,",l.data[i]);
}
printf("]\n");
}
#endif
main.c
#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
#include "list.h"
int main(){
// 初始化
List l;
initList(&l);
// 插值
insertList(&l, 0, 1);
insertList(&l, 1, 2);
insertList(&l, 0, 0);
// 格式输出
listrepr(l);
// 取值
datatype oe;
listGetE(l, 0, &oe);
printf("getE: 0=%d\n", oe);
// 找值
printf("findE: idx=%d\n", listFindE(l, 1));
// 删值
deleteList(&l, 0 , &oe);
printf("delete: 0=%d\n", oe);
// 长度
printf("length=%d\n", listLength(l));
// 格式输出
listrepr(l);
return 1;
}
链表(Link)
- link.h
#ifndef _LINK_H_
#define _LINK_H_
// 结构
#define LinkSize 100
typedef struct Node{
datatype data;
struct Node *next;
}LinkNode, *Link;
// 初始化
void initLink(Link *head){
if((*head=(Link)malloc(sizeof(LinkNode))) == NULL)
exit(-1);
(*head)->next=NULL;
}
// 判断空栈
int linkEmpty(Link head){
if(head->next == NULL)
return 1;
else
return 0;
}
// 插值
int insertLink(Link head, int idx ,datatype e){
Link curnd = head;
int curix = -1;
// 要找到需要插入位置前一个节点
while(curnd->next!=NULL && curix<idx-1){
curnd=curnd->next;
curix++;
}
// NULL条件退出则没有找到位置
if(curix!=idx-1){
printf("insert position illegal \n");
return -1;
}
// 申请新的节点指针
Link newnd=NULL;
if((newnd=(Link)malloc(sizeof(LinkNode))) == NULL)
exit(-1);
// 设置data,修改新节点的next指向curnd的next,修改cur curnd指向新节点
newnd->data= e;
newnd->next=curnd->next;
curnd->next=newnd;
return 1;
}
// 删值
int deleteLink(Link head, int idx ,datatype *e){
Link curnd = head;
int curix = -1;
//
while(curnd->next!=NULL && curix<idx-1){
curnd=curnd->next;
curix++;
}
if(curix!=idx-1){
printf("insert position illegal \n");
return -1;
}
//
Link delnd = NULL;
delnd = curnd->next;
curnd->next=delnd->next;
*e=delnd->data;
free(delnd);
return 1;
}
// link长度
int linkLength(Link head){
Link curnd = head;
int curix = -1;
while(curnd->next!=NULL){
curnd=curnd->next;
curix++;
}
return curix+1;
}
// 销毁
void destroyLink(Link *head){
Link delnd=NULL, curnd = *head;
if(curnd->next!=NULL){
delnd = curnd;
curnd = curnd->next;
free(delnd);
}
free(*head);
}
// 取节点
Link linkGetN(Link head, int idx){
Link curnd = head;
int curix = -1;
//
while(curnd->next!=NULL && curix<idx){
curnd=curnd->next;
curix++;
}
if(curix!=idx){
printf("insert position illegal \n");
return NULL;
}
return curnd;
}
// 找节点
Link linkFindN(Link head, datatype e){
Link curnd = head;
int curix = -1;
//
while(curnd->next!=NULL){
if(curnd->data == e)
return curnd;
curnd=curnd->next;
curix++;
}
if(curnd->data == e)
return curnd;
return NULL;
}
// 输出
void linkrepr(Link head){
int i;
printf("<link>:[");
int length = linkLength(head),val;
for(i=0;i<length;i++){
val = linkGetN(head, i)->data;
if(i==length-1)
printf("%d",val);
else
printf("%d,",val);
}
printf("]\n");
}
#endif
- main.c
#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
#include "link.h"
int main(){
Link head = NULL;
initLink(&head);
insertLink(head, 0 ,1);
insertLink(head, 1 ,2);
insertLink(head, 0 ,0);
printf("length = %d\n", linkLength(head));
if (linkFindN(head,2)!=NULL)
printf("%p = %d\n",linkFindN(head,2) , linkFindN(head,2)->data);
linkrepr(head);
destroyLink(&head);
return 1;
}
队列(Queue)
queue.h
#ifndef _Queue_H_
#define _Queue_H_
// 结构
#define QueueSize 100
typedef struct{
datatype data[QueueSize];
int head, tail;
}Queue;
// 初始化
void initQue(Queue *que){
que->head=que->tail=0;
}
// 判断空
int queEmpty(Queue que){
if(que.head == que.tail)
return 1;
else
return 0;
}
// 入队
int queAppend(Queue *que, datatype e){
// 判断tail位置是否到达head
if(queEmpty(*que)==0 && que->tail%QueueSize==que->head){
printf("que is full \n");
return 0;
}
// 队列为空直接入队
que->data[que->tail]=e;
que->tail= (que->tail+1)%QueueSize;
}
// 出队
int queGet(Queue *que, datatype *e){
if(queEmpty(*que)){
printf("que is empty\n");
return 0;
}
*e = que->data[que->head];
que->head = (que->head+1)%QueueSize;
return 1;
}
// link长度
int queLength(Queue que){
return que.tail-que.head;
}
// 销毁
void clearQue(Queue *que){
que->head=que->tail=0;
}
// 输出
void linkrepr(Queue que){
int i;
printf("<queue>:[");
int length = queLength(que),val;
for(i=0;i<length;i++){
val= que.data[(que.head+i)%QueueSize];
if(i==length-1)
printf("%d",val);
else
printf("%d,",val);
}
printf("]\n");
}
#endif
- main.c
#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
#include "queue.h"
int main(){
Queue que;
initQue(&que);
queAppend(&que,0);
queAppend(&que,1);
queAppend(&que,2);
printf("length = %d\n", queLength(que));
int e;
if(queGet(&que, &e))
printf("qhead = %d\n", e);
queLength(que);
linkrepr(que);
return 1;
}