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 {
...
};
Related
#include<stdio.h>
#include<stdlib.h>
struct stack
{
int top,size,top1;
int *arr;
};
int isempty(struct stack *ptr)
{
if(ptr->top==-1){return 1;}
else{return 0;}
}
int isfull(struct stack *ptr)
{
if(ptr->top==ptr->size-1){return 1;}
else{return 0;}
}
int push(struct stack *ptr, int data)
{
if(isfull(ptr)){printf("Stack is Full\n");}
else{
ptr->top++;
ptr->arr[ptr->top]=data;
}
}
int pop(struct stack *ptr)
{
if(isempty(ptr)){printf("Stack is empty\n");}
else{
int a=ptr->arr[ptr->top];
ptr->top--;
return a;
}
}
int pop2(struct stack *ptr)
{
struct stack *lol;
printf("!");
lol->top1=-1;
printf("!");
lol->size=100;
printf("!");
lol->arr=(int*)malloc(lol->size*sizeof(int));
}
int main()
{
struct stack *ptr;
ptr->top=-1;
ptr->size=100;
ptr->arr=(int*)malloc(ptr->size*sizeof(int));
push(ptr,1);
push(ptr,2);
push(ptr,3);
push(ptr,4);
push(ptr,5);
pop2(ptr);
printf("%d ",pop(ptr));
printf("%d ",pop(ptr));
printf("%d ",pop(ptr));
printf("%d ",pop(ptr));
printf("%d ",pop(ptr));
}
i am trying to run this code but dont konw why but in function pop2 the second and third printf are not working or in other terms pop2 is not working. it is not throwing any error but just stops after sometime. if i remove pop2 than the whole code is working properly
why i am getting stack is empty every time?
I am trying to make a expression tree from postfix here.
what's the logical error here?? plus we are not allowed to declare any variable globally. so, i had to pass the stackarray and node every time in each time function calling.
i am posting the full code done by me please have a look, i know its might be a simple error but as a beginner please show some kindness.
please help. TIA :)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 10
struct ExpnTreeNode
{
struct ExpnTreeNode *lchild;
char data;
struct ExpnTreeNode *rchild;
};
struct stackarray
{
struct ExpnTreeNode *data[MAXSIZE];
int top;
};
void push(struct ExpnTreeNode *newnode, struct stackarray *s);
struct ExpnTreeNode* pop( struct stackarray *s);
int check(char c);
void operand(struct ExpnTreeNode *newnode,char m, struct stackarray *s);
void operators(struct ExpnTreeNode *newnode,char m, struct stackarray *s);
void printex(struct ExpnTreeNode *node);
int main(void)
{
struct ExpnTreeNode *p=NULL;
struct stackarray s={{NULL},-1} ;
int len;
int x;
int res;
int i;
char arr[30]={'\0'};
printf("\n\tEnter a postfix expression:");
scanf("%d",&arr);
struct ExpnTreeNode *newnode=NULL;
len=strlen(arr);
for(i=0;arr[i]!='\0';i++)
{
x=check(arr[i]);
if(x==1)
{
operand(newnode,arr[i],&s);
}
else if(x==2)
{
operators(newnode,arr[i],&s);
}
}
p=pop(&s);
printex(p);
return 0;
}
void push(struct ExpnTreeNode *newnode, struct stackarray *s)
{
if(s->top==MAXSIZE-1)
{
printf("\n\tStack is Full");//Stack is Full
}
else
{
s->top=s->top+1;
s->data[s->top]=newnode;
}
}
struct ExpnTreeNode* pop( struct stackarray *s)
{
if(s->top==-1)
{
printf("\n\tStack is Empty");
return;
}
s->top=s->top-1;
return(s->data[s->top]);
}
int check(char c)
{
if(c=='*' || c=='/' || c=='+' || c=='-')
{
return 2;
}
else
{
return 1;
}
}
void operand(struct ExpnTreeNode *newnode,char m, struct stackarray *s)
{
struct stack *fs=NULL;
fs=s;
newnode=(struct ExpnTreeNode *)calloc(1,sizeof(struct ExpnTreeNode));
newnode->data=m;
newnode->lchild=NULL;
newnode->rchild=NULL;
push(newnode,&fs);
}
void operators(struct ExpnTreeNode *newnode,char m, struct stackarray *s)
{
newnode=(struct ExpnTreeNode *)calloc(1,sizeof(struct ExpnTreeNode));
struct stack *fs=NULL;
fs=s;
newnode->data=m;
newnode->rchild=pop(&fs);
newnode->lchild=pop(&fs);
push(newnode,&fs);
}
void printex(struct ExpnTreeNode *node)
{
if(node!=NULL)
{
printf("\n\t%c",node->data);
printex(node->lchild);
printex(node->rchild);
}
}
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.
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));
So here's my code... My understanding is that I'm supposed to create a function "map" that takes a function as an argument. It's not going as planned. Any help would be completely amazing.
Here's a compilable (well not compilable, but scaled down) version of the code:
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
int main(int argc, const char *argv[])
{
//it should be apparent that I am quite new to C, I have some java experience.
struct linkedList {
int count;
int num;
struct linkedList *next;
};
struct linkedList *head, *tail, *curr;
int count1=0;
int i=0;
int square(int v) {return v=v*v;}
void map(int (*func)(int v), struct linkedList){
struct linkedList2 *head, *tail, *curr;
for(curr=head; curr!=NULL; curr=curr->next){
curr->num=func(curr->num);
printList();
}
}
void start(){
printf("This program will ONLY accept integer input.\n");
head=NULL;
for(i=1;i<=4;i++) {
count1++;
curr=malloc(sizeof(struct linkedList));
curr->count=count1;
printf("Enter a number: ");
scanf("%d", &curr->num);
if(head==NULL) { head=curr; }
else { tail->next=curr; }
tail=curr;
tail->next=NULL;
}
printf("A list has been created.\n");
}
void printList(){
printf("The list now contains these numbers: ");
for(curr=head;curr!=NULL;curr=curr->next){
printf("%d, ", curr->num);
}
printf("\n");
}
start();
printList();
map(square, linkedList);
printList();
system("PAUSE");
return 0;
}
Defining all of those structs and functions inside of main is not how you're supposed to write C. Move the int main(int argc, const char *argv[]) { to right after the definition of printList so that main only contains the actual code for main.
Moreover, your definition of map appears to have an unfinished prototype. Instead of void map(int (*func)(int v), struct linkedList), in which the second parameter is unused, you want void map(int (*func)(int v), struct linkedList* head) (and then get rid of the declaration of head on the next line). Moreover, linkedList2 here presumably should be changed to linkedList. In addition, your attempt to call map in main with map(square, linkedList) is nonsensical; you want to use map(square, head).