/* snq.c, Implementation for Stack and Queue */
/* 1998.12.7. LEE SANG HWAN */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int parser( char *p, char *argv[], int maxargc )
{
int argc = 0;
while ( *p )
{
while ( *p && isspace( *p ) ) p++;
if ( ! *p ) return( argc );
argv[argc++] = p++;
while ( *p && !isspace( *p ) ) p++;
if( *p ) *p++ = 0;
if ( argc >= maxargc ) return( argc );
}
return ( argc );
}
#define SSIZE 5
int stack[SSIZE], top = 0;
void addstack(int element)
{
if(top == SSIZE)
{
printf("Stack Full \n");
return;
}
stack[top++] = element;
return;
}
int deletestack()
{
if(top == 0)
{
printf("Stack Empty \n");
return 0xDEADDEAD;
}
return stack[--top];
}
int topelement()
{
if(top == 0)
{
printf("Stack Empty \n");
return 0xDEADDEAD;
}
return stack[top - 1];
}
#define QSIZE 5
int queue[QSIZE];
int front = 0;
int rear = 0;
void addq(int element)
{
if( ((rear + 1) % QSIZE) == front)
{
printf("queue full\n");
return;
}
queue[rear] = element;
rear = (rear + 1) % QSIZE;
}
int deleteq()
{
int temp;
if(front == rear)
{
printf("empty queue\n");
return 0xDEADDEAD;
}
temp = queue[front];
front = (front + 1) % QSIZE;
return temp;
}
int frontmember()
{
if(front == rear)
{
printf("empty queue\n");
return 0xDEADDEAD;
}
return queue[front];
}
void main()
{
char buf[128];
char *argv[10];
int argc;
char *prompt = "\nSNQ> ";
int temp, i;
printf(prompt);
while(1)
{
gets(buf);
argc = parser( buf, argv, 10 );
if(!strcmp(argv[0], "push"))
{
temp = atoi(argv[1]);
addstack(temp);
printf("Push one element to Stack : %d\n", temp);
}
else if(!strcmp(argv[0], "pop"))
{
temp = deletestack();
if(temp != 0xDEADDEAD)
printf("Delete One Element from Stack : %d\n", temp);
}
else if(!strcmp(argv[0], "top"))
{
temp = topelement();
if(temp!= 0xDEADDEAD)
printf("Top Element : %d\n", temp);
}
else if(!strcmp(argv[0], "showstack"))
{
printf("Members of Stack\n");
for(i = top - 1; i >= 0; i--)
printf("%d\n", stack[i]);
}
else if(!strcmp(argv[0], "add"))
{
temp = atoi(argv[1]);
addq(temp);
printf("Add One Element to Queue : %d\n", temp);
}
else if(!strcmp(argv[0], "delete"))
{
temp = deleteq();
if(temp != 0xDEADDEAD)
printf("Delete One Element from Queue : %d\n", temp);
}
else if(!strcmp(argv[0], "front"))
{
temp = frontmember();
if(temp!= 0xDEADDEAD)
printf("Front Element of Queue %d\n", temp);
}
else if(!strcmp(argv[0], "quit"))
{
printf("stop operation of stack \n");
break;
}
else if(!strcmp(argv[0], "showq"))
{
printf("Members of Queue\n");
for(i = front; i != rear; i = (i + 1) % QSIZE)
printf("%d\n", queue[i]);
}
else
{
printf("Invalid Command : %s\n", argv[0]);
}
printf(prompt);
}
}
<The End>
'C' 카테고리의 다른 글
C 언어 버블 소트 (거품 정렬) 소스 (0) | 2016.08.11 |
---|---|
C, 이진트리 구현 소스 (0) | 2016.08.11 |
C, 소트 구현 소스 bubble, selection, insertion, quick, merge, heap sort (0) | 2016.08.11 |
C, 하노이탑 소스 (0) | 2016.08.11 |
C, 피보나치 수열 소스 (0) | 2016.08.11 |