0

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[] 
}
2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Salem
0

> I am new to C/C++ and this week we just started object oriented C++
So which is it, C or C++?
C/C++ is walking down the middle of the road with traffic heading towards you from both directions. Sooner or later, it's not going to be a pretty sight.

> gets(str);
> ptr = (char *) malloc(strlen(str));
3 mistakes here,
- NEVER EVER use gets(). It is not safe and cannot be made safe. Use fgets(buff,sizeof buff,stdin) as a replacement.
- your malloc didn't allocate enough space (you forgot to count the \0)
- casting malloc in C is bad form. At best it does nothing the compiler can't figure out for itself. At worst, it hides some potentially serious errors (not including stdlib.h).

> I want to have a good understanding of simulating object oriented programming with C
Then you want to start by getting rid of all those global variables, and creating two structs (stack and queue).
For example,

void push ( stack *s, char c );
char pop ( stack *s );
stack *stackCtor ( ); // constructor
void stackDtor ( stack *s ); // destructor

Everything (almost) has the first parameter being a pointer to the specific instance of the object (think of the 'this' pointer in C++).

When you have those in place, then you can start thinking about treating your stack and queue as true objects. All the data associated with the object is hidden inside the struct (all the outside world sees is a pointer to the object), and all access is via a "member" function.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.