栈
#include <stdio.h>
#define maxSize 5
struct stack{
int top;
int val[maxSize];
};
int isFull(struct stack *st){
if(st->top==maxSize){
printf("stack is full\n");
return 1;
}
else{
printf("stack size=%d, capacity=%d\n",st->top,maxSize);
return 0;
}
}
void push(struct stack *st, int val){
if(isFull(st)){
printf("push failed\n");
}
else{
st->val[st->top]=val;
printf("push st[%d]: %d\n",st->top,val);
st->top++;
}
}
void pop(struct stack *st){
if(st->top==0){
printf("pop failed\n");
}
else{
st->top--;
printf("pop: %d\n",st->val[st->top]);
}
}
void display(struct stack *st){
printf("stack: ");
int size=st->top;
for(int i=0;i<size;i++){
printf("%d ",st->val[i]);
}
printf("\n");
}
int main() {
struct stack st={0};
push(&st,1);
push(&st,2);
push(&st,3);
push(&st,4);
push(&st,5);
push(&st,6);
display(&st);
pop(&st);
pop(&st);
pop(&st);
push(&st,100);
push(&st,101);
display(&st);
return 0;
}
队列
#include <stdio.h>
#define maxSize 5
struct que{
int front;
int rear;
int size;
int data[maxSize];
};
void InitQue(struct que* q){
if(!q){
printf("Init failed\n");
}
q->front=0;
q->rear=0;
q->size=maxSize;
printf("Init Que\n");
}
int isFull(struct que *q){
if(q && q->front == (q->rear+1)%q->size){
printf("Que is full, ");
return 1;
}
else{
return 0;
}
}
int isEmpty(struct que *q){
if(q && q->front == q->rear){
printf("Que is empty, ");
return 1;
}
else{
return 0;
}
}
void InQue(struct que *q, int val){
if(!q || isFull(q)){
printf("InQue failed\n");
}
else{
q->data[q->rear]=val;
q->rear=(q->rear+1)%q->size;
printf("InQue: %d\n",val);
}
}
void OutQue(struct que *q){
if(!q || isEmpty(q)){
printf("OutQue failed\n");
}
else{
printf("OutQue: %d\n",q->data[q->front]);
q->front=(q->front+1)%q->size;
}
}
void display(struct que *q){
printf("Que: ");
for(int i=q->front;i!=q->rear;i=(i+1)%q->size){
printf("%d ",q->data[i]);
}
printf("\n");
}
int main() {
struct que q;
InitQue(&q);
InQue(&q,1);
InQue(&q,2);
InQue(&q,3);
InQue(&q,4);
InQue(&q,5);
InQue(&q,6);
display(&q);
OutQue(&q);
OutQue(&q);
InQue(&q,7);
InQue(&q,8);
InQue(&q,9);
display(&q);
return 0;
}
运行结果:
Init Que
InQue: 1
InQue: 2
InQue: 3
InQue: 4
Que is full, InQue failed
Que is full, InQue failed
Que: 1 2 3 4
OutQue: 1
OutQue: 2
InQue: 7
InQue: 8
Que is full, InQue failed
Que: 3 4 7 8
库函数
<uthash.h>
用法 | 含义 |
---|---|
HASH_FIND_INT(table, &elem_key, elem) | 在table中查找elem_key键值对应的elem |
HASH_ADD_INT(table, key , elem) | 在table中增加一个key键值变量对应的键值对结构elem |
HASH_DEL(table, elem) | 在table中删除elem |
HASH_SORT(table, cmp) | 排序函数 |
#include<uthash.h>
//结构体定义
typedef struct{
int key; //key
int value; //value
UT_hash_handle hh; //UT_hash_handle 字段必须要存在于你定义的结构体中,用来标记该结构体是哈希表
}HashTable;
typedef HashTable* HashPtr;
//初始化
HashPtr table = NULL;
//统计个数
int num = HASH_COUNT(table);
//查找
HashPtr find(int key) {
if(!table) return NULL;
HashPtr elem = NULL;
HASH_FIND_INT(table, &key, elem);
if( elem != NULL)
return elem; //找到了
else
return NULL; //没找到
}
// 增加
void add(int key, int value)
{
HashPtr elem = NULL;
elem = (HashPtr)malloc(sizeof(HashTable));
elem ->key = key;
elem ->value = value;
HASH_ADD_INT(table, key, elem);
}
// 删除
void delete (HashPtr elem)
{
HASH_DEL(table, elem);
free(elem);
elem = NULL;
}
//排序
int cmp(HashPtr a, HashPtr b)
{
return a->value > b->value;
}
void sort()
{
HASH_SORT(table, cmp);
}