(p130)栈和队列

两个程序的功能都是输入0就出栈(出队列),输入1就是入栈(入队列),输入-1结束

栈:

/*
 * stack.c
 *
 *  Created on: Feb 22, 2016
 *      Author: wing
 */
/*
 * stack.c
 *
 *  Created on: Feb 22, 2016
 *      Author: wing
 */
#include<stdio.h>
#include<stdlib.h>
#define max 10
struct stack{
	int s[max];
	int head;
	int tail;
};
int pop(struct stack *S)
{
	if (S->tail==-1)
	{
		printf("Stack underflow!\n");
		return 0;
	}
	else
	{
		printf("%d\n",S->s[S->tail]);
		(S->tail)--;
		return 0;
	}
}
int push(struct stack *S)
{
	int tmp;
	if (S->tail==max-1)
	{
		printf("Stack overflow!\n");
		return 0;
	}
	else
	{
		scanf("%d",&tmp);
		(S->tail)++;
		S->s[S->tail]=tmp;
		return 0;
	}
}
int main(void)
{
	int n;
	struct stack *S;
	S=(struct stack *)malloc(sizeof(struct stack));
	S->head=0;
	S->tail=-1;
	scanf("%d",&n);
	while (n!=-1)
	{
		switch(n)
		{
		case 0:pop(S);break;
		case 1:push(S);break;
		default:break;
		}
		scanf("%d",&n);
	}
	return 0;
}


用链表来实现就是:

/*
 * stack.c
 *
 *  Created on: Feb 22, 2016
 *      Author: wing
 */
#include<stdio.h>
#include<stdlib.h>
struct node{
	int n;
	struct node *next;
	struct node *prev;
};
int pop(struct node **s)
{
	if (*s==NULL)
	{
		printf("Stack underflow!\n");
		return 0;
	}
	else
	{
		printf("%d\n",(*s)->n);
		if ((*s)->next==NULL)
		{
			free(*s);
			*s=NULL;
		}
		else
		{
			*s=(*s)->next;
			free((*s)->prev);
			(*s)->prev=NULL;
		}
		return 0;
	}
}
int push(struct node **s)
{
	struct node *new;
	int n;
	new=(struct node *)malloc(sizeof(struct node));
	scanf("%d",&n);
	new->n=n;
	new->next=*s;
	new->prev=NULL;
	if ((*s)!=NULL)
		(*s)->prev=new;
	*s=new;
	return 0;
}
int main(void)
{
	int n;
	struct node **s;
	s=(struct node **)malloc(sizeof(struct node *));
	*s=NULL;
	scanf("%d",&n);
		while (n!=-1)
		{
			switch(n)
			{
			case 0:pop(s);break;
			case 1:push(s);break;
			default:break;
			}
			scanf("%d",&n);
		}
		return 0;
}


队列,为了方便判断队列上溢和下溢,这里引入了一个q->n(队列元素个数),另外采用取余操作方便一些:

/*
 * queue.c
 *
 *  Created on: Feb 22, 2016
 *      Author: wing
 */
#include<stdio.h>
#include<stdlib.h>
#define max 5
struct queue{
	int q[max];
	int head;
	int n;
};
int deque(struct queue *q)
{
	if (q->n==0)
	{
		printf("Underflow!\n");
		return 0;
	}
	else
	{
		printf("%d\n",q->q[q->head]);
		q->head=(q->head+1)%max;
		(q->n)--;
		return 0;
	}
}
int enque(struct queue *q)
{
	int tmp;
	if (q->n==max)
	{
		printf("Overflow!\n");
		return 0;
	}
	else
	{
		scanf("%d",&tmp);
		(q->n)++;
		q->q[(q->head+q->n-1)%max]=tmp;
		return 0;
	}
}
int main(void)
{
	struct queue *q;
	int n;
	q=(struct queue *)malloc(sizeof(struct queue));
	q->n=0;
	scanf("%d",&n);
	while (n!=-1)
	{
		switch(n)
		{
		case 0:deque(q);break;
		case 1:enque(q);break;
		default:break;
		}
		scanf("%d",&n);
	}
	return 0;
}

同样的,用链表来实现就是:

/*
 * queue.c
 *
 *  Created on: Feb 22, 2016
 *      Author: wing
 */
#include<stdio.h>
#include<stdlib.h>
struct node{
	int n;
	struct node *next;
	struct node *prev;
};
struct queue{
	struct node *head;
	struct node *tail;
};
int deque(struct queue *q)
{
	if (q->head==NULL)
	{
		printf("Queue underflow!\n");
		return 0;
	}
	else
		if (q->head->next==NULL)
		{
			printf("%d\n",q->head->n);
			free(q->head);
			q->head=NULL;
			q->tail=NULL;
			return 0;
		}
		else
		{
			printf("%d\n",q->head->n);
			q->head=q->head->next;
			free(q->head->prev);
			q->head->prev=NULL;
			return 0;
		}
}
int enque(struct queue *q)
{
	int n;
	struct node *new;
	new=(struct node*)malloc(sizeof(struct node));
	scanf("%d",&n);
	new->n=n;
	if (q->head==NULL)
	{
		q->head=new;
		q->tail=new;
		new->prev=NULL;
		new->next=NULL;
		return 0;
	}
	else
	{
		new->next=NULL;
		new->prev=q->tail;
		q->tail->next=new;
		q->tail=new;
		return 0;
	}

}
int main(void)
{
	int n;
	struct queue q;
	q.head=NULL;
	q.tail=NULL;
	scanf("%d",&n);
	while (n!=-1)
	{
		switch(n)
		{
		case 0:deque(&q);break;
		case 1:enque(&q);break;
		default:break;
		}
		scanf("%d",&n);
	}
	return 0;
}



你可能感兴趣的:(编程,算法,栈,算法导论)