I try to implement a simple stack in c with pointers and struct but I think I have a problem with push or print. My program prints only the first Node.
If someone could help me to solve this
Here is my code
stack.h
/***********************************************************************
* stack.h
***********************************************************************/
#include <stdbool.h>
#ifndef _STACK_H_
#define _STACK_H_
typedef struct Stack{
int value;
struct Stack* next;
}Stack;
Stack initStack();
bool push (Stack* s, int n);
int pop (Stack* s);
bool stackEmpty (Stack s);
void printStack (Stack s);
#endif
stack.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "stack.h"
Stack initStack(){
Stack s = {0, NULL};
return s;
}
bool push (Stack* s, int n){
if (stackEmpty(*s)){
s->value = n;
return true;
}else{
Stack stack = {n, s};
s = &stack;
return true;
}
return false;
}
int pop (Stack* s){
int value = s->value;
s = s->next;
return value;
}
bool stackEmpty (Stack s){
if (s.value == 0 && s.next == NULL)
return true;
return false;
}
void printStack (Stack s){
Stack* b = &s;
printf("Stack : \n");
while(b != NULL){
printf("%d\n", b->value);
b = b->value;
}
main
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "stack.h"
int main (int argc, char** argv) {
Stack stack1 = initStack();
push(&stack1, 5);
printStack(stack1);
}
You're taking the address of a local variable here:
Stack stack = {n, s};
s = &stack;
You need to dynamically allocate it:
Stack *stack = malloc(sizeof(*stack));
stack->value = n;
stack->next = s;
s = stack;
For consistency, I would recommend to take and return Stack * instead of Stack, and possibly rename initStack to newStack.
Related
Can anyone please explain here what is the issue in the below code because of that it is not producing any errors as well as any output?
#include <stdio.h>
#include <stdlib.h>
typedef struct Stack{
int size;
int top;
int data;
int *arr;
} Stack;
void push( Stack*s , int data)
{
s->top++;
s->arr[s->top] = data;
}
int main()
{
struct Stack *s;
s->size = 100;
s->top = -1;
s->arr = (int* ) malloc (s->size* sizeof(int));
push( s, 180);
}
this is because you write struct Stack *s; which is just a wild pointer pointing to some space in memory with no reserved space, so you should do instead :
struct Stack *s = (struct Stack* ) malloc (sizeof(struct Stack));
to reserve space for your wild pointer.
even my compiler gave me this warning, so make sure to turn on all compiler warnings on yours.
Variable 's' is uninitialized when used here
this is the full code with only this small modification:
#include <stdio.h>
#include <stdlib.h>
typedef struct Stack{
int size;
int top;
int data;
int *arr;
} Stack;
void push( Stack*s , int data)
{
s->top++;
s->arr[s->top] = data;
}
int main()
{
//struct Stack *s = (struct Stack* ) malloc (sizeof(struct Stack));
struct Stack *s;
s->size = 100;
s->top = -1;
s->arr = (int* ) malloc (s->size* sizeof(int));
push( s, 180);
printf("stack top data = %d\n", s->arr[s->top]);
}
and this is the output:
stack top data = 180
I'm trying to implement a stack in C. I have only implemented the struct that will contain an array and that currently only contains the size of the array and the position of the last item added to the stack
This is a partial implementation that is giving me some trouble.
in stack.h
#include <stdlib.h>
#include <stdbool.h>
typedef struct Stack
{
int max_size;
int top;
// int *contents;
} Stack;
Stack *stack_create(int n);
bool stack_is_empty(Stack *stack);
bool stack_is_full(Stack *stack);
void stack_push(Stack *stack, int value);
in stack.c:
#include <stdio.h>
#ifndef STACK_H
#include "stack.h"
#endif
Stack *stack_create(int n)
{
Stack stack;
Stack *s = &stack;
s->max_size = n;
s->top = 0;
// s->contents = (int *)malloc(sizeof(int) * n);
return s;
}
bool stack_is_empty(Stack *stack)
{
if (stack->top == 0)
{
return true;
}
return false;
}
bool stack_is_full(Stack *stack)
{
if (stack->top == stack->max_size)
{
return true;
}
return false;
}
void stack_push(Stack *stack, int value)
{
if (!stack_is_full(stack))
{
printf("max_size: %d\n", stack->max_size);
printf("top: %d (%p)\n", stack->top++, &stack->top);
printf("value: %d (%p)\n", value, &value);
}
else
{
printf("Can't push. max_size==%d reached.\n", stack- >max_size);
exit(EXIT_FAILURE);
}
}
and in main.c:
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#define SIZE 3
int main()
{
Stack *s = stack_create(SIZE);
printf("stack_is_empty: %d\n", stack_is_empty(s));
stack_push(s, 100);
printf("stack_is_empty: %d\n", stack_is_empty(s));
stack_push(s, 30);
printf("stack_is_empty: %d\n", stack_is_empty(s));
stack_push(s, 20);
printf("stack_is_empty: %d\n", stack_is_empty(s));
return 0;
}
main produces the following output:
stack_is_empty: 1
max_size: 3
top: 100 (0x7ffd5430dfb4)
value: 101 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 30 (0x7ffd5430dfb4)
value: 31 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 20 (0x7ffd5430dfb4)
value: 21 (0x7ffd5430dfb4)
stack_is_empty: 0
Why is value's address the same of stack->top?
Problem 1 : You are allocating memory for the stack locally in stack_create function. As soon as the function goes out of scope memory will be freed. Thus you will have a dangling pointer.
Problem 2 : You are allocating memory only for one instance regardless of value of 'n'
typedef struct Stack
{
int max_size;
int *contents;
int top;
// int *contents;
} Stack;
Stack *stack_create(int n) {
Stack *s;
s = (Stack *)malloc(sizeof(Stack));
s->contents = (int *)malloc(sizeof(int) * n);
s->max_size = n;
s->top = 0;
return s;
}
i am trying to implement a stack by writing all its functions in a separate source file.but i get a lot of errors saying incompatible pointer type.these errors don't show up when i include the functions in the main C file.these are my files.i am new to this.help me correct them.
thank you.
my main code is
#include "myfunctions.h"
int main()
{
int operation,data;
struct stack *One = (struct stack *)malloc(sizeof(struct stack));
One->top = -1;
printf("stack functionality \n");
while(1)
{
if (isEmpty(One))
{
printf("enter 1 to push \n");
scanf("%d",&operation);
}
else if (isFull(One))
{
printf("stack is full.enter 2 to pop or 3 to diplay \n");
scanf("%d",&operation);
}
else
{
printf("enter 1 to push,2 to pop,3 to display 4 to exit \n");
scanf("%d",&operation);
}
switch (operation)
{
case 1: printf("enter data to be pushed \n");
scanf("%d",&data);
push(One,data);
break;
case 2: printf("%d \n",pop(One));
break;
case 3: display(One);
break;
case 4:exit(0);
}
}
}
stackfns code
#include <myfunctions.h>
bool isEmpty(struct stack *b)
{
if(b->top == -1) return true;
else return false;
}
bool isFull(struct stack *b)
{
if(b->top == 9) return true;
else return false;
}
void push(struct stack *b,int data)
{
b->a[++(b->top)] = data;
}
int pop(struct stack *b)
{
return (b->a[(b->top)--]);
}
void display(struct stack *b)
{
int i;
for (i=0;i<10;i++)
printf("%d ",b->a[i]);
printf("\n");
}
header file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
extern bool isEmpty(struct stack *b);
extern bool isFull(struct stack *b);
extern void push(struct stack *b,int data);
extern int pop(struct stack *b);
extern void display(struct stack *b);
#define max_size 10
struct stack
{
int a[max_size];
int top;
};
In the header file you need to first decalre the struct:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define max_size 10
struct stack
{
int a[max_size];
int top;
};
extern bool isEmpty(struct stack *b);
extern bool isFull(struct stack *b);
extern void push(struct stack *b,int data);
extern int pop(struct stack *b);
extern void display(struct stack *b);
In addition, there is no need to expose the internal implementation of the data structure. You can do something like:
.h file:
typedef struct xx_t xx_t;
.c file:
struct xx_t {
...
};
I'm trying to build a stack using a linked list but I get a EXC_BAD_ACCESS error in my linkedListStackInit method;
LinkedList.h
#ifndef LinkedListStack_h
#define LinkedListStack_h
#ifndef __TYPE
#define __TYPE
#define TYPE int
#define TYPE_SIZE sizeof(int)
#endif
#include <stdio.h>
struct Link;
struct LinkedListStack;
void linkedListStackInit(struct LinkedListStack *s);
void push(struct LinkedListStack *s, TYPE data);
void pop(struct LinkedListStack *s);
TYPE top(struct LinkedListStack *s);
int isEmpty(struct LinkedListStack *s);
#endif
LinkedList.c
#include <stdlib.h>
#include "LinkedListStack.h"
struct Link {
TYPE value;
struct Link *next;
};
struct LinkedListStack {
struct Link *firstLink;
};
void linkedListStackInit(struct LinkedListStack *s) {
s->firstLink = 0;
}
void push(struct LinkedListStack *s, TYPE data) {
struct Link *newLink = malloc(sizeof(struct Link));
// Assert?
newLink->next = s->firstLink;
newLink->value = data;
s->firstLink = newLink;
}
void pop(struct LinkedListStack *s) {
struct Link *temp = s->firstLink;
s->firstLink = s->firstLink->next;
free(temp);
}
TYPE top(struct LinkedListStack *s) {
return s->firstLink->value;
}
int isEmpty(struct LinkedListStack *s) {
if(s == NULL) {
return 0;
}
else {
return 1;
}
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "LinkedListStack.h"
int main(int argc, const char * argv[]) {
struct LinkedListStack *s;
linkedListStackInit(s);
return 0;
}
From your main method you are calling the function linkedListStackInit and passing stack (s) to it. But you haven't allocated memory to s before passing it to the linkedListStackInit function. The function linkedListStackInit doesn't allocate the memory either and tries to assign a value to its "firstlink" member. Try to do the following in your linkedListStackInit function and see if you can proceed further.
s = malloc(sizeof(struct LinkedListStack));
I have the following C program:
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
typedef struct node_t node_t;
struct node_t
{
char *name;
node_t **nodes;
};
node_t* init(int p_n)
{
node_t *node = malloc(sizeof(node_t));
node->name = "_ROOT_";
if(p_n > 0 && p_n < 10)
{
node->nodes = malloc(p_n*sizeof(node_t**));
char nbuffer[9];
int i;
for(i = 0; i < p_n; i++)
{
node_t *child = malloc(sizeof(node_t));
sprintf(nbuffer, "NAME %d", i);
child->name = nbuffer;
node->nodes[i] = child;
}
}
return node;
}
int main(int argc, char *argv[])
{
int n = 3;
node_t *ROOT = init(n);
printf("%sNODE {name:%s [%lu]}\n",
"", ROOT->name, sizeof(ROOT->nodes)/sizeof(ROOT->nodes[0])
);
assert(n == sizeof(ROOT->nodes)/sizeof(ROOT->nodes[0]));
free(ROOT);
return 0;
}
The assertion from the beginning of the main method fails and this is my problem. What I am doing wrong in the evaluation of the length of member nodes?
This post is know by me but from some reason it doesn't work for me. Why?
SK
Assuming a 32 bit pointer.
The sizeof(ROOT->nodes) is going to be 4 bytes. A pointer to a pointer it still a pointer.
The sizeof(ROOT->nodes[0]) is also going to be 4 bytes. It too is a pointer.
Hence 4/4 != 3 and your assert fails.
I'm not sure what your objective is, but if you're wanting to do something like this with dynamically allocated storage you might do something like the following. It can be extended with additional APIs to add, remove, etc. This solution maintains a count of the number of elements in the list.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define NAME_SIZE 32
typedef struct node_s {
char *name;
} node_t;
typedef struct node_list_s {
int count;
node_t *nodes;
} node_list_t;
void init(node_list_t *l, int p_n) {
int i = 0;
char name[NAME_SIZE];
if(!l) return;
memset(l, 0, sizeof(node_list_t));
l->nodes = malloc(p_n*sizeof(node_t));
l->count = p_n;
for(i=0;i<l->count;i++) {
snprintf(name,sizeof(name),"NAME %d",i);
l->nodes[i].name = strdup(name);
}
}
void term(node_list_t *l) {
int i = 0;
if(!l) return;
for(i=0;i<l->count;i++) {
if(l->nodes[i].name) free(l->nodes[i].name);
}
if(l->nodes) free(l->nodes);
}
void print(node_list_t *l) {
int i = 0;
if(!l) return;
for(i=0;i<l->count;i++) {
printf("%d %s\n", i, l->nodes[i].name ? l->nodes[i].name : "empty" );
}
}
int main(int argc, char *argv[]) {
node_list_t list;
init(&list, 3);
print(&list);
term(&list);
return 0;
}