Wrong output when popping from the stack - c

I have made a program using stacks, but the output which i get is kind of wrong. The output i get, has the answer i need and also has 2 values which are not expected.
Here is what i have done:
#include <stdio.h>
#include<malloc.h>
#define MAX 180
struct cakes{
int spongecake;
int meringue;
int chocalate;
int red_velvet;
struct cakes *next;
};
struct stack{
int top;
int cake[10];
};
int isFull(struct stack *);
int isEmpty(struct stack *);
void push(struct stack *,int);
int pop(struct stack *);
void order_out(struct cakes *);
main()
{
struct cakes *head;
head=(struct cakes *)malloc(sizeof(struct cakes));
cake_order(head); //this is a seperate function, it works perfectly.
head->next=(struct cakes *)malloc(sizeof(struct cakes));
order_out(head->next);
}
int isFull(struct stack *q)
{
if(q->top==10-1)
{
return 1;
}
else
{
return 0;
}
}
void push(struct stack *sptr,int x)
{
if(!isFull(sptr))
{
sptr->top++;
sptr->cake[sptr->top]=x;
}
}
int isEmpty(struct stack *q)
{
if(q->top==-1)
{
return 1;
}
else
{
return 0;
}
}
int pop(struct stack *sptr)
{
int ret=NULL;
if(!isEmpty(sptr))
{
ret=sptr->cake[sptr->top];
sptr->top--;
return ret;
}
}
void order_out(struct cakes *theorder)
{
struct stack s;
s.top=-1;
int k=0;
int i=0;
int p=0;
int r=0;
int value1,value2;
int items[10];
theorder->spongecake=1;
theorder->meringue=2;
theorder->chocalate=3;
theorder->red_velvet=4;
for(;i<10;i++)
{
push(&s,theorder->spongecake);
push(&s,theorder->meringue);
push(&s,theorder->chocalate);
push(&s,theorder->red_velvet);
}
while(!isEmpty(&s))
{
printf("\n%d",pop(&s));
}
}
The output which I get is as follows :
As you can see it prints 2 and 1 first. What seems to be the problem?

In my opinion your program is working as follows,
The following loop tries to insert 40 values,
for(;i<10;i++)
{
push(&s,theorder->spongecake);
push(&s,theorder->meringue);
push(&s,theorder->chocalate);
push(&s,theorder->red_velvet);
}
But only 10 values are inserted because of your statement if(q->top==10-1) in your isFull() function. i.e the counter counts 10 elements from 0 to 9. The order in which the elements are pushed is as follows 1, 2, 3, 4, 1, 2, 3, 4, 1, 2. These elements when popped gives you the sequence 2, 1, 4, 3, 2, 1, 4, 3, 2, 1. Thus the output you obtain is actually correct or at least not an aberration.
There are a few more issues I would like to point out,
The function pop() should be as follows,
int pop(struct stack *sptr)
{
int ret=NULL;
if(!isEmpty(sptr))
{
ret=sptr->cake[sptr->top];
sptr->top--;
return ret;
}
return 0;
}
Otherwise the function returns random values when sptr is empty.
The statement if(q->top==10-1) should be if(q->top==9).

There is no problem with stack except the pop implementation where return value NULL is returned when stack is empty. Better return an invalid number as convention(like 0, -1, -999) left to your choice.
As you are restricting the stack size to 10, only 10 values are being inserted. So while inserting only following are inserted 1 2 3 4 1 2 3 4 1 2 and are popped inorder.

Related

Program that reads numbers from an array and then insert them into list doesn't work

I had to write a program which reads int numbers from an array, and then inserts them one by one if they are bigger than the limit number. What I wrote was:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
struct number_list{
int high_number;
struct number_list *next;
};
typedef struct number_list number_list;
void insert_in_list(int *array, int lim);
int main(void){
int number_seq[SIZE]={1, 2, 3, 4, 5, 6, 7, 8, 9};
int limit;
scanf("%d\n", limit);
insert_in_list(number_seq, limit);
}
void insert_in_list(int *array, int lim){
int i;
number_list *start_node;
for(i=0; i<SIZE; i++){
if(array[i]>lim){
start_node=malloc(sizeof(number_list));
start_node->high_number=array[i];
printf("%d\n", start_node->high_number);
start_node->next=start_node;
}
}
}
What happens is that the program doesn't have any compilation errors (except for a warning that %d expects argument of type *int instead of int, which I also didn't understand, so if anyone could help me with that too would be very kind) but when running, after inserting the limit number, displays a segmentation error (core dump created).
I suppose the problem is in the insert_in_list function, but I don't understand what it is.
There are a couple of things that go wrong here:
(a) The insert_in_list function needs an additional parameter that tells into which list to insert.
(b) Why did you name the member of the number_list structure "high_number". Shouldn't it contain just a number and hence simply be called "number"?
(c) You always set the next pointer to the structure itself. If you want to form a list, it should point to the next node.
(d) in main you need to define a pointer to the first node of your list, which should contain null as long as the list is empty. This "anchor" is what represents your list.
(e) As a hint: Pass a pointer to that anchor to your insert function, because, once the insert function creates the first node it has to deposit the address of that first node into the anchor. Hence your insert function should look like this:
void insert_in_list (number_list** pAnchor, int number)
(f) It is no good idea to define the size of your input array as SIZE and use that symbol in your insert_in_list function. Instead, either path the array and its length as two parameter to the function, or -- as I did in the code line above -- pass just one number per call to the insert function and call it for every number you want to insert.
This will solve your problem however I do not understand why inset_in_list returns nothing and you're not using the list in the main function whatsoever
If thats not intensional either have it return a pointer to the list or create the list in the main and pass a pointer of the list to the function insert_in_list
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
typedef struct node *node_pointer;
typedef struct node{
int high_number;
node_pointer next;
}number_list;
void insert_in_list(int *array, int lim);
int main(void){
int number_seq[SIZE]={1, 2, 3, 4, 5, 6, 7, 8, 9};
int limit;
scanf("%d", &limit);
insert_in_list(number_seq, limit);
}
void insert_in_list(int *array, int lim){
extern void print_list(node_pointer list);
int i;
node_pointer list = NULL;
for(i=0; i<10; i++){
if(array[i]>lim){
node_pointer newnode, last = list;
newnode = (node_pointer)malloc(sizeof(number_list));
newnode->high_number = array[i];
newnode->next = NULL;
if (last == NULL){
list = newnode;
}//first node
else{
while (1) {
if (last->next == NULL) {
last->next = newnode;
break;
}
last = last->next;
}
}
}
}
print_list(list);
}
void print_list(node_pointer list) {
node_pointer which;
which = list;
printf("\nList 4 :");
if (which == NULL){ printf("Empty\n"); }
while (which != NULL) {
printf("%d", which->high_number);
which = which->next;
}
}

Queue insertion based on condition

I am trying to insert values to a queue based on a condition. Condition is the value entered previously should be larger than the value which is going to be inserted. My problem is according to my data i have been provided with, one value is small than all the other values, so other values are not inserted in to the queue.
I am stuck here the whole day, without any clue.
Here is what i have done so far :
#include<stdio.h>
#include<malloc.h>
#define MAX 180
struct cakes{
int spongecake;
int meringue;
int chocalate;
int red_velvet;
struct newcake *next;
};
struct Queue{
int front;
int rear;
int count;
int cake[10];
};
void order_out(struct cakes *);
void init(struct Queue *);
int isFull(struct Queue *);
void insert(struct Queue *,int);
int isEmpty(struct Queue *);
int removes(struct Queue *);
main()
{
struct cakes *head;
head=(struct cakes*)malloc(sizeof(struct cakes));
order_out(head);
}
void init(struct Queue *q)
{
q->front=0;
q->rear=10-1;
q->count=0;
}
int isFull(struct Queue *q)
{
if(q->count==10)
{
return 1;
}
else
{
return 0;
}
}
void insert(struct Queue *q,int x)
{
if(!isFull(q))
{
q->rear=(q->rear+1)%10;
q->cake[q->rear]=x;
q->count++;
}
}
int isEmpty(struct Queue *q)
{
if(q->count==0)
{
return 1;
}
else
{
return 0;
}
}
int removes(struct Queue *q)
{
int caked=NULL;
if(!isEmpty(q))
{
caked=q->cake[q->front];
q->front=(q->front+1)%10;
q->count--;
return caked;
}
}
void order_out(struct cakes *theorder)
{
struct Queue s;
int k=0;
int i=1;
int value1;
int value2;
int counter=1;
theorder->spongecake=20;
theorder->meringue=75;
theorder->chocalate=40;
theorder->red_velvet=30;
init(&s);
insert(&s,theorder->chocalate);
value1=theorder->chocalate;
for(;k<10;k++)
{
if(value1>theorder->spongecake)
{
insert(&s,theorder->spongecake);
value1=theorder->spongecake;
}
if(value1>theorder->meringue)
{
insert(&s,theorder->meringue);
value1=theorder->meringue;
}
if(value1>theorder->red_velvet)
{
insert(&s,theorder->red_velvet);
value1=theorder->red_velvet;
}
}
while(!isEmpty(&s))
{
printf("\n%d",removes(&s));
}
}
Any idea on how to make the other values entered in to the queue but also at the same time check whether the previous value is larger than the current value.
EDIT :
I tried adding a count meaning if the count % value1==0, then i tried to make the value1 into a bigger value by multiplying by 5 but still only those 2 values got inserted.
Thank you for your time.

Pushing values to a Stack from a Queue

I am trying to push values in to a stack using a queue, but the thing is am not getting any pop from the stack( No output).
Here is what i have done :
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#define MAX 180
#define TRUE 1
#define FALSE 0
struct cakes {
int spongecake;
int meringue;
int chocalate;
int red_velvet;
struct cakes *next;
};
struct stack{
int top;
int cake[10];
};
struct Queue{
int front;
int rear;
int count;
int cake[10];
};
void conveyer_order(struct cakes *);
void baking_order(struct cakes *);
int isFull(struct stack *);
int isEmpty(struct stack *);
void push(struct stack *,int);
int pop(struct stack *);
void init(struct Queue *);
int isqueueFull(struct Queue*);
void insert(struct Queue*,int);
int isqueueEmpty(struct Queue *);
int removes(struct Queue *);
main()
{
struct cakes *head;
head=(struct cakes *)malloc(sizeof(struct cakes));
conveyer_order(head);
head->next=(struct cakes *)malloc(sizeof(struct cakes));
baking_order(head->next);
}
int isFull(struct stack *k)
{
if(k->top==10-1)
{
return TRUE;
}
else
{
return FALSE;
}
}
int isEmpty(struct stack *k)
{
if(k->top==-1)
{
return TRUE;
}
else
{
return FALSE;
}
}
int pop(struct stack *sptr)
{
int ret=NULL;
if(!isEmpty(sptr))
{
ret=sptr->cake[sptr->top];
sptr->top--;
return ret;
}
}
void push(struct stack *sptr,int x)
{
if(!isFull(sptr))
{
sptr->top++;
sptr->cake[sptr->top]=x;
}
}
void init(struct Queue *q)
{
q->front=0;
q->rear=10-1;
q->count=0;
}
int isqueueFull(struct Queue *q)
{
if(q->count==10)
{
return 1;
}
else
{
return 0;
}
}
void insert(struct Queue *q,int x)
{
if(!isqueueFull(q))
{
q->rear=(q->rear+1)%10;
q->cake[q->rear]=x;
q->count++;
}
}
int isqueueEmpty(struct Queue *q)
{
if(q->count==0)
{
return 1;
}
else
{
return 0;
}
}
int removes(struct Queue *q)
{
int cakeempty=NULL;
if(!isqueueEmpty(q))
{
cakeempty=q->cake[q->front];
q->front=(q->front+1)%10;
q->count--;
return cakeempty;
}
}
void baking_order(struct cakes *theorder)
{
int v=0;
struct stack baking;
struct Queue belt;
baking.top=-1;
int value1=0;
int value2=0;
theorder->spongecake=20;
theorder->chocalate=40;
theorder->red_velvet=30;
theorder->meringue=75;
init(&belt);
while(!isqueueFull(&belt))
{
insert(&belt,theorder->meringue);
insert(&belt,theorder->chocalate);
insert(&belt,theorder->red_velvet);
insert(&belt,(theorder->spongecake));
}
value1=removes(&belt);
while(!isqueueEmpty(&belt))
{
while(!isFull(&baking))
{
value2=removes(&belt);
if(value1>=value2)
{
push(&baking,value2);
value1=value2;
}
else
{
insert(&belt,value2);
}
}
}
while(!isEmpty(&baking))
{
printf("\n%d",pop(&baking));
}
}
I tried printing the values without passing in to the stack and it works , i think the problem is with the 2 while loops.
How can i fix this error ?
Thank you for your time.
The problem is you only ever push 4 items onto the stack, so the stack is never full and your while(!isFull(&baking)) becomes an infinite loop.
Let's see if I can explain why:
Before you start the while loop, value1 is 75.
Then you read value2 as 40.
Check if(value1>=value2)? Yes, so you push 40 and set value1 to 40.
Then restart the loop, read value2 as 30.
Check if(value1>=value2)? Yes, so you push 30 and set value1 to 30.
Then restart the loop, read value2 as 20.
Check if(value1>=value2)? Yes, so you push 20 and set value1 to 20.
Then restart the loop, read value2 as 75.
Check if(value1>=value2)? No, so you put 75 back in the queue.
Then restart the loop, read value2 as 40.
Check if(value1>=value2)? No, so you put 40 back in the queue.
Then restart the loop, read value2 as 30.
Check if(value1>=value2)? No, so you put 30 back in the queue.
Then restart the loop, read value2 as 20.
Check if(value1>=value2)? Yes, so you push 20 and set value1 to 20.
At this point, you have pushed 40, 30, 20, 20.
However, everything left in the queue is either 30, 40 or 75 and if(value1>=value2) will never evaluate to true again.
Therefore your stack is never filled, and you never get out of the while loop.

Program produce Wrong output

I have written a program on queues and dynamic memory allocation. This is what my program needs to do - insert values in to the queue and remove it from the queue; that simple.
But my problem is that it just prints the names of the variables the values are assigned to and the program goes not responding.
Here is my program :
#include <stdio.h>
#define MAX 180
struct cakes{
int spongecake;
int meringue;
int chocalate;
int red_velvet;
struct newcake *next;
};
struct Queue{
int front;
int rear;
int count;
int cake[10];
};
void init(struct Queue *);
int isFull(struct Queue *);
void insert(struct Queue *,int);
int isEmpty(struct Queue *);
int removes(struct Queue *);
void cake_order(struct cakes *);
void order_out(struct cakes *);
main()
{
struct cakes *head;
head=(struct cakes *)malloc(sizeof(struct cakes ));
cake_order(&head); //this is a seperate function and it works perfectly
head->next=(struct cakes *)malloc(sizeof(struct cakes));
order_out(&head->next);
}
void init(struct Queue *q)
{
q->front=0;
q->rear=10-1;
q->count=0;
}
int isFull(struct Queue *q)
{
if(q->count==10)
{
return 1;
}
else
{
return 0;
}
}
void insert(struct Queue *q,int x)
{
if(!isFull(q))
{
q->rear=(q->rear+1)%10;
q->cake[q->rear]=x;
q->count++;
}
}
int isEmpty(struct Queue *q)
{
if(q->count==0)
{
return 1;
}
else
{
return 0;
}
}
int removes(struct Queue *q)
{
int caked=NULL;
if(!isEmpty(q))
{
caked=q->cake[q->front];
q->front=(q->front+1)%10;
q->count--;
return caked;
}
}
void order_out(struct cakes *order)
{
struct Queue s;
int i;
order->spongecake=20;
order->meringue=75;
order->chocalate=40;
order->red_velvet=30;
init(&s);
for(i=0;i<10;i++)
{
insert(&s,order->chocalate);
insert(&s,order->spongecake);
insert(&s,order->meringue);
insert(&s,order->red_velvet);
}
while(!isEmpty(&s))
{
printf("%d",removes(&s));
}
}
What seems to be the problem here?
I am new to C, so yea am a bit slow when debugging in this language.
Thank you for your time.
Here is the output:
Lots of problems here, first it would be better if main was declared properly as in int main() and then it returned a value at the end e.g. return 0; like:
int main()
{
.... // code
return 0; // normally 0 is returned if execution has been successful
}
There seem to be other problems with the code as I wasn't able to compile it, for example there's no closing brace at the end of order_out() (right after the while loop).
Also would be good if you provided the cake_order() function.
It's also missing the includes for say stdlib.h, and on line 45 (head=(struct cakes *)malloc(sizeof(struct cakes ));) I've noticed you cast the result of malloc, which is not necessary.
And if I may further add, don't remember to free() the memory you've allocated with malloc(). I didn't see a single free() statement in your code.

Why my code crashes if I call push function more than 8 times?

// Whenever i am increasing function call times of push more than 8, it get crashed before that everything is working good. your help is required. In the below code i have created a program for stack using Dynamic array keeping in mind there should not be any stack overflow by using realloc function to doubling the values whenever the stack gets filled.
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
typedef struct ArrayStack
{
int top;
int capacity;
int *arr;
}*stack;
stack Creation()
{
stack S;
S=(stack)malloc(sizeof(struct ArrayStack));
if(!S)return NULL;
S->top=-1;
S->capacity=1;
S->arr=(int*)malloc(S->capacity*sizeof(int));
if(!S->arr)return NULL;
return S;
}
int is_Full(stack S)
{
return S->top==S->capacity-1;
}
int is_Empty(stack S)
{
return S->top==-1;
}
void Doubling(stack S)
{
S->capacity*=2;
S->arr=realloc(S->arr,S->capacity);
}
void push(stack S,int data)
{
if(is_Full(S))
Doubling(S);
S->arr[++S->top]=data;
}
int pop(stack S)
{
if(is_Empty(S))
printf("\nStack underflow");
else
return S->arr[S->top--];
}
int main()
{
stack S;
int i=0,size=9;
S=Creation();
**for(i=0;i<size;i++)
push(S,19+1);** // As in this case
return 0;
}
S->arr = malloc(S->capacity * sizeof(int));
S->arr = realloc(S->arr, S->capacity);
You only reallocate enough space for S->capacity / sizeof(int) items.

Resources