implementation of queue using two stacks - c

#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

Related

I am trying to code queue using stack. There is no error while I am running the code but the stack is not able to store the values

I have initialised two stacks using a structure with which I am creating a queue. But the stack is not able to store the values which is why enqueue or dequeue operations are not working properly.
Here is the code:
#include<stdio.h>
#include<stdlib.h>
struct stack{
int top;
int size;
int *s;
};
int isfull(struct stack *st){
if(st->top==st->size-1){
return 1;
}
return 0;
}
int isempty(struct stack *st){
if(st->top==-1){
return 1;
}
return 0;
}
void push(struct stack *st,int x){
if(isfull(st)){
printf("FULL!!\n");
}
else{
st->top++;
st->s[st->top]=x;
}
}
int pop(struct stack *st){
int x=-1;
if(isempty(st)){
printf("EMPTY!!\n");
}
else{
x=st->s[st->top];
st->top--;
}
return x;
}
void enqueue(struct stack s1,int x){
push(&s1,x);
}
int dequeue(struct stack s1,struct stack s2){
int x=-1;
if(isempty(&s2)){
if(isempty(&s1)){
printf("QUEUE IS EMPTY!!\n");
return x;
}
else{
while(!isempty(&s1)){
push(&s2,pop(&s1));
}
}
}
return pop(&s2);
}
void display(struct stack st){
int i;
for(i=0;i<=st.top;i++){
printf("%d",st.s[i]);
}
}
int main(){
int n,choice;
struct stack s1,s2;
printf("ENTER SIZE OF QUEUE:");
scanf("%d",&n);
s1.size=n;
s2.size=n;
s1.top=-1;
s2.top=-1;
s1.s=(int *)malloc(s1.size*sizeof(int));
s2.s=(int *)malloc(s2.size*sizeof(int));
while(1){
printf("1.ENQUEUE\n");
printf("2.DEQUEUE\n");
printf("3.DISPLAY\n");
printf("4.EXIT\n");
printf("ENTER YOUR CHOICE:");
scanf("%d",&choice);
switch(choice){
case(1):
int x;
printf("ENTER DATA:");
scanf("%d",&x);
enqueue(s1,x);
break;
case(2):
int m;
m=dequeue(s1,s2);
printf("ELEMENT DELETED IS:%d\n",m);
break;
case(3):
display(s2);
break;
case(4):
exit(0);
}
}
return 0;
}
What is the error? I think there might be an issue with passing the values to the function.
The main issue is that the enqueue and dequeue don't take pointers as arguments, but struct stack. This means the function gets a copy of the given struct, and that the pointer you pass to push and pop (like &s1) is pointing to that local structure, not to the one in main. By consequence any update to the top member of that stack will not be seen by the caller.
I would suggest to:
Consistently pass pointers to struct typed arguments. This was well done for the push and pop functions, and there is no reason why it should not be done the same way for enqueue and dequeue functions.
Define a struct queue so that you abstract a bit that there are two stacks involved and don't have to pass both of them as argument to dequeue.
Create separate functions for:
creating a new stack
displaying a stack
creating a new queue
displaying a queue
checking if a queue is empty
Here is how your code would then look:
#include <stdio.h>
#include <stdlib.h>
struct stack {
int top;
int size;
int *s;
};
struct stack* newstack(int size) {
struct stack *s = malloc(sizeof(struct stack));
s->size = size;
s->s = malloc(size*sizeof(int));
s->top = -1;
return s;
}
int isfull(struct stack *st) {
return st->top == st->size - 1;
}
int isempty(struct stack *st) {
return st->top == -1;
}
void push(struct stack *st, int x) {
if (isfull(st)){
printf("Full!\n");
} else {
st->top++;
st->s[st->top] = x;
}
}
int pop(struct stack *st) {
int x = -1;
if (isempty(st)){
printf("Empty!\n");
} else {
x = st->s[st->top];
st->top--;
}
return x;
}
void displaystack(struct stack *st) {
for(int i = 0; i <= st->top; i++) {
printf("%d ", st->s[i]);
}
}
struct queue {
struct stack *s1;
struct stack *s2;
};
struct queue* newqueue(int size) {
struct queue *q = malloc(sizeof(struct queue));
q->s1 = newstack(size);
q->s2 = newstack(size);
return q;
}
int isemptyqueue(struct queue *q) {
return isempty(q->s1) && isempty(q->s2);
}
void enqueue(struct queue *q, int x) {
push(q->s1, x);
}
int dequeue(struct queue *q) {
int x = -1;
if (isemptyqueue(q)) {
printf("Queue is empty!\n");
return -1;
}
if (isempty(q->s2)) {
while (!isempty(q->s1)) {
push(q->s2, pop(q->s1));
}
}
return pop(q->s2);
}
void displayqueue(struct queue *q) {
displaystack(q->s1);
printf("| ");
displaystack(q->s2);
printf("\n");
}
int main() {
int n, choice, x, m;
printf("Enter the size of the queue: ");
scanf("%d", &n);
struct queue *q = newqueue(n);
while (choice != 4) {
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &x);
enqueue(q, x);
break;
case 2:
m = dequeue(q);
printf("The deleted element is: %d\n", m);
break;
case 3:
displayqueue(q);
break;
}
}
return 0;
}

Maintaining a queue using structures in C

If I input 'E 5' I need to enqueue 5 onto the queue with no output. If I input 'D' I need to print the dequeued element.
Below is my code:
#include<stdio.h>
#include <stdlib.h>
typedef struct queue{
int start;//first element index
int end;//last element index
int size;//length of queue
int *ptr;//actual queue
}queue;
queue* createQueue(int x){
queue* Q=(queue*)malloc(sizeof(queue));
Q->start=0;
Q->end=0;
Q->size=1;
Q->ptr[0]=x;
return Q;
}
void enqueue(int x, queue*qptr, int i){
qptr->size++;//increase size
qptr->end++;//increase index of last element
qptr->ptr[i]=x;
return;
}
void dequeue(queue*qptr){
if(qptr->size==0){
printf("Empty\n");
return;
}
printf("%d\n",qptr->ptr[qptr->start]);
qptr->start++;
qptr->size--;
}
int main(){
char a; int i=0;int j;queue* qptr;
while(scanf("%c",&a)!=-1){
if(a=='E'){
if(i==0){
scanf("%d",&j);
qptr =createQueue(j);
i++;
}
else{
scanf("%d",&j);
enqueue(j, qptr, i);
i++;
}
}
else if(a=='D'){
if(i==0){printf("Empty\n");}
else {dequeue(qptr);i++;}
}
}
return 0;
}
I don't understand what are my errors because it seems correct to me but the code doesn't compile. Please help me.

Finding path between nodes using BFS in C language

I'm new to C language and it's been harder for me to work with pointers after working in Java😥
I was trying to write a code of finding a path (not necessary minimum) between two nodes in a graph using breadth-first-search.
Here is my code :
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 200
void push(int a);
int pop(void);
void bfs(int a,int b,int len);
int nextnode(int a);
typedef struct node{
int data;
struct node* next;
}node;
int res[MAXSIZE];
int visited[MAXSIZE];
int rear,front;
node* graph[MAXSIZE];
int len;
int path[MAXSIZE];
int nextnode(int a)
{
if(graph[a]==NULL)
return -1;
else
{
struct node* c=graph[a];
while(visited[c->data]!=1 && c!=NULL)
{
c=c->next;
}
if(c==NULL)
return -1;
else
return c->data;
}
}
void push(int a)
{
path[rear]=a;
rear++;
}
int pop()
{
if(front==rear)
return -1;
int num=path[front];
front++;
return num;
}
int main()
{
rear=0;
len=0;
front=0;
int n,e;
int i,a,b;
printf("%s\n%s", "Inputting Graph... ","Enter number of nodes and edges: ");
scanf("%d %d",&n,&e);
printf("%s %d %s\n", "Graph Created with",n,"nodes without any edge.");
printf("%s\n","Enter the edges in 1 2 format if an edge exist from Node 1 to Node 2" );
for(i=1;i<=n;i++)
{
graph[i]=NULL;
visited[i]=0;
}
struct node* new = (struct node*)malloc(sizeof(struct node));
for(i=0;i<e;i++)
{
scanf("%d %d",&a,&b);
new->data=b;
new->next=NULL;
struct node* curr=graph[a];
if(curr==NULL)
{
graph[a]=new;
}
else
{
while(curr->next!=NULL)
{
curr=curr->next;
}
curr->next=new;
}
}
printf("%s\n", "Graph Created Successfully.");
printf("%s", "Enter the node numbers between which the path is to be found between: ");
scanf("%d %d",&a,&b);
bfs(a,b,0);
printf("Length is %d\n",len);
for(i=1;i<=len;i++)
{
printf("%d\n",res[len]);
}
}
void bfs(int a,int b,int len)
{
int c;
visited[a]=1;
int flag=0;
while(a!=-1)
{
c=nextnode(a);
while(c!=-1)
{
c=nextnode(a);
if(c==b)
{
flag=1;
break;
}
push(c);
visited[c]=1;
}
len++;
res[len]=a;
if(flag==1)
{
res[len]=b;
break;
}
a=pop();
}
}
I know it's huge, but please mind going through it once. The problem I'm getting is Segmentation Fault after I input all the values, and before dfs() function call! Please Help.
For understanding: I have used array of Lists. Each array index denotes a node and the list denotes all the edges it is connected to. eg: if my Graph has 1->2, 1->3, 2-3 edges;
graph[1] will have a list 2->3->NULL. And graph[2] will have 3->NULL.
Thank you.
EDIT
As pointed out by Aditi, the error was in the line where nextnode function ran the while loop. After changing the code to
while(c != NULL && visited[c->data] == 1 )
the program ran flawlessly.
Thanks!
I think what you are trying to do is not graph[i] = NULL but graph[i]->next = NULL

Stack implementation placing codes in multiple files

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 {
...
};

Lattices of Partial Order Set Segmentation Fault(core dumped)

Given a Set S = {x1,x2,x3,x4,x5,….} whose size is n (< 20), I need to list all the lattices (subsets of S) on S with size k(should be taken as input, but in my question I fixed it for debugging purposes). The relation over which the partial order is defined is divisibility.
#include <stdio.h>
#include <stdbool.h>
typedef struct{
int data;
struct node* next;
}node;
void addEdge(node* graph[],int a,int b){
node *ptr=(node *)malloc(sizeof(node));
ptr->next=graph[a];
ptr->data=b;
graph[a]=ptr;
}
void buildGraph(node* graph[],int n){
int i,j;
for(i=2;i<=n;i++){
for(j=1;j<=i/2;j++){
if(i%j==0){
if(j!=1) addEdge(graph,i/j,i);
else addEdge(graph,j,i);
}
}
}
}
void printGraph(node* graph[],int n){
int i=0;
node *ptr;
for(i=0;i<=n;i++){
ptr=graph[i];
printf("Row-%d ",i);
while(ptr!=NULL){
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
}
void print(int array[],int n){
int i;
for(i=0;i<n;i++) printf("%d ",array[i]);
printf("\n");
}
bool checkDuplicate(int array[],int n,int a){
int i;
for(i=0;i<n;i++) if(array[i]==a) return false;
return true;
}
void formLattice(node* graph[],int lattice[],int k,int vertex,int level){
//printf("Level-%d \n",level);
if(level>k) return;
if(level==k) {
lattice[level-1]=vertex;
print(lattice,k);
return;
}
lattice[level-1]=vertex;
node* ptr=graph[vertex];
while(ptr!=NULL){
// if(checkDuplicate(lattice,level,ptr->data))
formLattice(graph,lattice,k,ptr->data,level+1);
ptr=ptr->next;
}
}
void printLattice(node* graph[],int n,int k){
int lattice[k],i;
node* ptr;
for(i=1;i<=n;i++){
ptr=graph[i];
while(ptr!=NULL){
lattice[0]=i;
formLattice(graph,lattice,k,ptr->data,2);
ptr=ptr->next;
}
}
}
int main(void) {
int n=10,k=4;
node* graph[n+1];
int i;
for(i=0;i<=n;i++) graph[i]=NULL;
buildGraph(graph,n);
// printGraph(graph,n); // Comment to print out the Hasse Diagram
printLattice(graph,n,k);
return 0;
}
Showing Segementation Fault(core dumped)
Any idea why is this happening so?
Thanks in Advance
The code of formLattice() has evidences that only those vertexes are probed for lattice[level] that are connected to lattice[level-1]. Not every lattice, of course, has a total order on it (namely {1,2,3,6} doesnt). How can the present program choose the vertex 3 for lattice[2] while the vertex 2 is stored into lattice[1]?
In other words, there must be another loop, going for all already chosen vertexes in lattice from 0 to level-1, around the present loop for ptr.

Resources