Last active
November 16, 2016 01:50
-
-
Save JeonghunLee/528db53e117c30de60ef2c872989327e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define MAX 10 | |
int size=0; | |
int head=0; // not used in stack | |
int tail=0; | |
int queue[MAX]; | |
int stack[MAX]; | |
//QUEUE | |
int enqueue(int *pqueue, int *pdata) | |
{ | |
if(size >= MAX) return -1; // check max value , error , not working | |
pqueue[tail] = *pdata; //next item, | |
if(tail == (MAX-1)) tail = 0; | |
else tail++; | |
size++; | |
return 0; | |
} | |
int dequeue(int *pqueue, int *pdata) | |
{ | |
if(size <= 0) return -1; // check minum value , error | |
*pdata = pqueue[head]; | |
if(head == (MAX-1)) head = 0; | |
else head++; | |
size--; | |
return 0; | |
} | |
// STACK | |
int push(int *pstack, int *pdata) | |
{ | |
if(size >= MAX) return -1; // check max value , error , not working | |
pstack[size] = *pdata; | |
size++; | |
return 0; | |
} | |
int pop(int *pstack, int *pdata) | |
{ | |
if(size <= 0) return -1; // check minum value , error | |
size--; | |
*pdata = pstack[size]; | |
return 0; | |
} | |
int main() | |
{ | |
int i=0; | |
int data=0; | |
int off=10; | |
int ret=0; | |
for(i=0;i<MAX+10;i++) | |
{ | |
data = off + i; | |
ret = enqueue(queue,&data); | |
printf("enqueue %d err=%d\n",data,ret); | |
} | |
printf("\n\n"); | |
for(i=0;i<MAX;i++) | |
{ | |
ret = dequeue(queue,&data); | |
printf("dequeue %d err=%d\n",data,ret); | |
} | |
printf("\n\n"); | |
off=5; | |
for(i=0;i<5;i++) | |
{ | |
data = off + i; | |
ret = enqueue(queue,&data); | |
printf("enqueue %d err=%d\n",data,ret); | |
} | |
printf("\n\n"); | |
for(i=0;i<4;i++) | |
{ | |
ret = dequeue(queue,&data); | |
printf("dequeue %d err=%d\n",data,ret); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment