Hey all,
I am new to C/C++ and this week we just started object oriented C++. Before I dive into it I want to have a good understanding of simulating object oriented programming with C. Anyway, I believe I have doctored the code to where the queue and the Stack work seperately, but I am not exactly sure how to take what is entered into the queue and push it into the stack. If anyone has any suggestions that would be awesome.
Thanks in advance guys :)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSTACK 10
#define EMPTYSTACK -1
void enter(void);
void q_store(char *ptr);
int store = 0;
int retrieve = 0;
char *queue[100];
int top = EMPTYSTACK;
char items[MAXSTACK];
void push( char );
char pop();
int empty();
int full();
void push(char c) {
items[++top] = c;
}
char pop() {
return items[top--];
}
int full() {
return top+1 == MAXSTACK;
}
int empty() {
return top == EMPTYSTACK;
}
int main() {
enter();
char ch;
while ((ch = getchar())
!= '\n')
if (!full()) push(ch);
while (!empty())
printf("%c", pop());
printf("\n");
return 0;
}
void enter(void)
{
static char str[100], *ptr;
printf("Enter a palindrome (ENTER only will exit) : ");
gets(str);
ptr = (char *) malloc(strlen(str));
strcpy(ptr,str);
if (*str)
q_store(ptr); // store in queue if string has info
}
void q_store(char *ptr)
{
if (store == 100) {
puts("\nList is full!");
return;
}
queue[store] = ptr;
store++; // point to next available storage position in queue[]
}