Υλοποίηση στοίβας με λειτουργίες PUSH και POP χρησιμοποιώντας πίνακα

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5

/* Function declaration - Prototype*/
void push(void);
int  pop(void);
void display(void);

int stack[MAXSIZE];
top = -1;

/* Main algorithm */
void main()
{
	int choice;
	int option = 1;

	while (option)
	{
		printf("\n STACK OPERATION USING ARRAY\n\n");
		printf(" ---------------------- Menu ------------------------\n\n");
		printf(" Command	Help\n\n");
		printf(" 1. PUSH	(Add element to the stack)\n");
		printf(" 2. POP		(Delete element from the stack)\n");
		printf(" 3. DISPLAY	(Display the status of the stack)\n");
		printf(" 4. EXIT	\n\n");
		printf(" ----------------------------------------------------\n\n");
		printf(" Please enter a choice from the menu: ");
		scanf("%d", &choice);

		switch (choice)
		{
		case 1: push();
			break;
		case 2:	pop();
			break;
		case 3:	display();
			break;
		case 4:	return;
		}

		fflush(stdin);
		printf("\n Do you want to continue(Type 0 or 1)?: ");
		scanf("%d", &option);

		system("cls"); /*
					   Clear screen for windows console.
					   In case of Linux replace "cls" with "clear"
					   */
	}
}

/* Add element to the stack */
void push()
{
	int num;
	if (top == (MAXSIZE - 1))
	{
		printf(" Stack is full...\n");
		return;
	}
	else
	{
		printf("\n Enter the element to be pushed: ");
		scanf("%d", &num);
		top = top + 1;
		stack[top] = num;
	}
	return;
}

/* Delete element from the stack */
int pop()
{
	int num;
	if (top == -1)
	{
		printf("\n Stack is Empty\n");
		return (top);
	}
	else
	{
		num = stack[top];
		printf("\n Poped element is = %d\n", stack[top]);
		top = top - 1;
	}
	return(num);
}

/* Display the status of the stack */
void display()
{
	int i;
	if (top == -1)
	{
		printf("\n Stack is empty...\n");
		return;
	}
	else
	{
		printf("\n The status of the stack is:\n\n");
		for (i = top; i >= 0; i--)
		{
			printf(" %d\n", stack[i]);
		}
	}
	printf("\n");
}
ċ
stack-implementation-using-array.c
(2k)
Dimitrios Rousis,
3 Μαρ 2014, 6:54 π.μ.
ċ
stack-with-table-exe.zip
(179k)
Dimitrios Rousis,
9 Μαρ 2014, 8:44 π.μ.