介绍
队列是一种先进先出(First In First Out)的线性表,只允许在线性表的一端进行插入操作,在另一端进行删除操作。允许插入的一端称为队尾,允许删除的一端称为队头。可以将队列想象成一个管道,数据从一边进入,从另一边流出。所以有时候队列也称为管道(pipe)。 栈是一种只能后进先出(Last In First Out)的数据结构,表现为只能在线性表的尾部进行插入和删除操作。其中,把数据插入栈称为压栈(Push),把数据从栈中取出来称为弹栈(Pop)。
队列
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define QUEUE_FULL 1
#define QUEUE_NOT_FULL 0
#define ERROR -1
#define OK 2
typedef int type_t;
typedef struct
{
type_t data[MAXSIZE];
int writePtr;
int readPtr;
int flag;
}Queue;
Queue *queue_create()
{
Queue *q = (Queue *)malloc (sizeof (Queue));
q->writePtr = 0 ;
q->readPtr = 0 ;
q->flag = QUEUE_NOT_FULL;
return q;
}
void queue_destroy(Queue *q)
{
if (q != NULL)
free (q);
}
int enQueue(Queue *q, type_t data)
{
if (q->flag == QUEUE_FULL)
return ERROR;
else
{
q->data[q->writePtr] = data;
q->writePtr = (q->writePtr + 1 ) % MAXSIZE;
if (q->writePtr == q->readPtr)
q->flag = QUEUE_FULL;
return OK;
}
}
int deQueue(Queue *q, type_t *pData)
{
if (q->writePtr == q->readPtr && q->flag == QUEUE_NOT_FULL)
return ERROR;
else
{
*pData = q->data[q->readPtr];
q->readPtr = (q->readPtr + 1 ) % MAXSIZE;
if (q->flag == QUEUE_FULL)
q->flag = QUEUE_NOT_FULL;
return OK;
}
}
int queueLength(Queue *q)
{
if (q->writePtr > q->readPtr)
return q->writePtr - q->readPtr;
else if (q->writePtr < q->readPtr)
return MAXSIZE + q->readPtr - q->writePtr;
else
{
if (q->flag == QUEUE_FULL)
return MAXSIZE;
else
return 0 ;
}
}
int main()
{
int i = 0 ;
int temp = 0 ;
Queue *q = queue_create();
for (i = 0 ; i < 10 ; i++)
enQueue(q, i);
printf ("length : %d\n" , queueLength(q));
for (i = 0 ; i < 10 ; i++)
{
deQueue(q, &temp);
printf ("dequeue : %d\n" , temp);
}
printf ("length : %d\n" , queueLength(q));
queue_destroy(q);
return 0 ;
}
栈(顺序存储1)
/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** *
> File Name: arrStack.c
> Author: mrhjlong
> Mail: mrhjlong@163 .com
> Created Time: 2016 年04 月24 日 星期日 10 时06 分33 秒
** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** /
typedef int type_t;
typedef struct
{
type_t data[MAXSIZE];
int index ;
}Stack;
// 创建
Stack *stack_create ()
{
Stack *s = (Stack *) malloc(sizeof(Stack));
s ->index = 0 ;
return s ;
}
//销毁
void stack_destroy(Stack *s )
{
if (s != NULL)
free(s );
return ;
}
//压栈
int push (Stack *s , type_t data)
{
if (s ->index == MAXSIZE)
return ERROR;
else
{
s ->data[s ->index ] = data;
s ->index ++;
return OK;
}
}
int pop (Stack *s , type_t *pData )
{
if (s ->index == 0 )
{
return ERROR;
}
else
{
s ->index --;
*pData = s ->data[s ->index ];
return OK;
}
}
void stack_print(Stack *s )
{
type_t i;
for (i = 0 ; i < s ->index ; i++)
printf ("%d " , s ->data[i]);
printf ("\n" );
}
//获取栈的顶端元素
int get_head(Stack *s )
{
if (s != NULL)
return s ->data[s ->index - 1 ];
}
//栈顶减一
void pop_null(Stack *s )
{
if (s != NULL)
s ->index --;
}
//打印栈的大小
int stack_size(Stack *s )
{
if (s != NULL)
return s ->index ;
}
int main()
{
Stack *s = stack_create();
type_t a[10 ] = {0} ;
type_t i;
char str[] = "931-3*+ 62/+" ;
for (i = 0 ; i < 11 ; i++)
{
if (push (s , i) == OK)
stack_print(s );
else
printf ("ERROR!\n" );
}
pop (s , a);
pop (s , a + 1 );
pop (s , a + 2 );
for (i = 0 ; i < 3 ; i++)
printf ("%d " , a[i]);
printf ("\n" );
stack_print(s );
return 0 ;
}
栈(顺序存储2)
/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** *
> File Name: arrStack.c
> Author: mrhjlong
> Mail: mrhjlong@163 .com
> Created Time: 2016 年04 月24 日 星期日 10 时06 分33 秒
** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** /
typedef int type_t;
typedef struct
{
type_t *data ;
int index ;
int size;
}Stack;
// 创建
Stack *stack_create (int size)
{
Stack *s = (Stack *) malloc(sizeof(Stack));
s ->data = (type_t *) malloc(size * sizeof(type_t));
s ->index = 0 ;
s ->size = size;
return s ;
}
//销毁
void stack_destroy(Stack *s )
{
if (s != NULL)
{
free(s ->data);
free(s );
}
return ;
}
//压栈
int push (Stack *s , type_t data)
{
if (s ->index == s ->size)
return ERROR;
else
{
s ->data[s ->index ] = data;
s ->index ++;
return OK;
}
}
int pop (Stack *s , type_t *pData )
{
if (s ->index == 0 )
{
return ERROR;
}
else
{
s ->index --;
*pData = s ->data[s ->index ];
return OK;
}
}
void stack_print(Stack *s )
{
type_t i;
for (i = 0 ; i < s ->index ; i++)
printf ("%d " , s ->data[i]);
printf ("\n" );
}
//获取栈的顶端元素
int get_Head(Stack *s )
{
if (s != NULL)
return s ->data[s ->index - 1 ];
}
//栈顶减一
void pop_null(Stack *s )
{
if (s != NULL)
s ->index --;
}
//打印栈的大小
int stack_size(Stack *s )
{
if (s != NULL)
return s ->index ;
}
const char *str = "((unsigned long)&(((type_t*) 0)->member))" ;
int check(const char *str )
{
Stack *s = stack_create(10 );
int len = strlen(str);
int i;
for (i = 0 ; i < len; i++)
{
if (s ->index < 0 )
{
stack_destroy(s );
return ERROR;
}
if (str[i] == '(' )
{
push (s , 1 );
}
if (str[i] == ')' )
{
pop_null(s );
}
}
stack_destroy(s );
if (s ->index == 0 )
return OK;
else
return ERROR;
}
int main()
{
Stack *s = stack_create(10 );
type_t a[10 ] = {0} ;
type_t i;
for (i = 0 ; i < 11 ; i++)
{
if (push (s , i) == OK)
stack_print(s );
else
printf ("ERROR!\n" );
}
pop (s , a);
pop (s , a + 1 );
pop (s , a + 2 );
for (i = 0 ; i < 3 ; i++)
printf ("%d " , a[i]);
printf ("\n" );
stack_print(s );
printf ("\n" );
if (check(str) == OK)
printf ("OK!\n" );
else
printf ("ERROR!\n" );
return 0 ;
}
栈(链式存储)
/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** *
> File Name: listStack.c
> Author: mrhjlong
> Mail: mrhjlong@163 .com
> Created Time: 2016 年04 月24 日 星期日 10 时06 分33 秒
** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** /
typedef int type_t;
typedef struct node
{
type_t data;
struct node *next ;
}QNode;
typedef struct
{
QNode *head ;
int index ;
}Stack;
// 创建
Stack *stack_create ()
{
Stack *s = (Stack *) malloc(sizeof(Stack));
s ->head = NULL;
s ->index = 0 ;
return s ;
}
//销毁
void stack_destroy(Stack *s )
{
if (s != NULL)
free(s );
return ;
}
//压栈
int push (Stack *s , type_t data)
{
// 链表头插法
QNode *pNode = (QNode *) malloc(sizeof(QNode));
}
int pop (Stack *s , type_t *pData )
{
// 删除节点
if (s ->index == 0 )
{
return ERROR;
}
else
{
s ->index --;
*pData = s ->data[s ->index ];
return OK;
}
}
void stack_print(Stack *s )
{
type_t i;
for (i = 0 ; i < s ->index ; i++)
printf ("%d " , s ->data[i]);
printf ("\n" );
}
//获取栈的顶端元素
int get_Head(Stack *s )
{
if (s != NULL)
return s ->data[s ->index - 1 ];
}
//栈顶减一
void pop_null(Stack *s )
{
if (s != NULL)
s ->index --;
}
//打印栈的大小
int stack_size(Stack *s )
{
if (s != NULL)
return s ->index ;
}
int main()
{
Stack *s = stack_create();
type_t a[10 ] = {0} ;
type_t i;
for (i = 0 ; i < 11 ; i++)
{
if (push (s , i) == OK)
stack_print(s );
else
printf ("ERROR!\n" );
}
pop (s , a);
pop (s , a + 1 );
pop (s , a + 2 );
for (i = 0 ; i < 3 ; i++)
printf ("%d " , a[i]);
printf ("\n" );
stack_print(s );
return 0 ;
}