Implement a Queue using two Stacks in C - c

Why my code is getting crushed when I'm running it. It says passing incompatible pointer type passing in Push() function. How to solve this problem?
Here is the code of my implementation in C. Here is a quick summery How I tried to solve the problem.
First I created a struct for Stack
Wrote Push and Pop function for stack
Wrote a struct for Queue
First Stack for EnQueue and Second Stack for DeQueue operation.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct Stack {
int data;
struct Stack *next;
};
struct Stack *CreateStack () {
return NULL;
}
int isEmptyStack(struct Stack *top) {
return (top == NULL);
}
void Push(struct Stack **top, int data) {
struct Stack *newNode = (struct Stack*) malloc(sizeof(struct Stack));
if(!newNode)
return;
newNode->data = data;
newNode->next = *top;
*top = newNode;
}
int Pop(struct Stack **top) {
struct Stack *temp;
int data;
if(isEmptyStack(*top)) {
printf("Empty Stack.\n");
return INT_MIN;
}
temp = *top;
data = (*top)->data;
*top = (*top)->next;
free(temp);
return data;
}
struct Queue {
struct Stack *S1;
struct Stack *S2;
};
struct Queue *CreateQueue() {
return NULL;
}
void EnQueue(struct Queue *Q, int data) {
Push(Q->S1, data);
}
int DeQueue(struct Queue *Q) {
if(!isEmptyStack(Q->S2)) {
return Pop(Q->S2);
}
else {
while(!isEmptyStack(Q->S1)) {
Push(Q->S2, Pop(Q->S1));
}
return Pop(Q->S2);
}
}
int main() {
struct Queue *Q = CreateQueue();
Q->S1 = Q->S2 = NULL;
EnQueue(Q, 1);
EnQueue(Q, 2);
EnQueue(Q, 3);
printf("%d ", DeQueue(Q));
printf("%d ", DeQueue(Q));
printf("%d ", DeQueue(Q));
return 0;
}

Three problems:
a) calling Push - wrong parameter type: struct Stack **top expected not astruct Stack *top
b) calling Pop - wrong parameter type: struct Stack **top expected not astruct Stack *top
c) Queue *CreateQueue - memory not allocated
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct Stack {
int data;
struct Stack *next;
};
struct Stack *CreateStack () {
return NULL;
}
int isEmptyStack(struct Stack *top) {
return (top == NULL);
}
void Push(struct Stack **top, int data) {
struct Stack *newNode = (struct Stack*) malloc(sizeof(struct Stack));
if(!newNode)
return;
newNode->data = data;
newNode->next = *top;
*top = newNode;
}
int Pop(struct Stack **top) {
struct Stack *temp;
int data;
if(isEmptyStack(*top)) {
printf("Empty Stack.\n");
return INT_MIN;
}
temp = *top;
data = (*top)->data;
*top = (*top)->next;
free(temp);
return data;
}
struct Queue {
struct Stack *S1;
struct Stack *S2;
};
struct Queue *CreateQueue() {
struct Queue *newNode = (struct Queue *) malloc(sizeof(struct Queue ));
return newNode;
}
void EnQueue(struct Queue *Q, int data) {
Push(&Q->S1, data);
}
int DeQueue(struct Queue *Q) {
if(!isEmptyStack(Q->S2)) {
return Pop(&Q->S2);
}
else {
while(!isEmptyStack(Q->S1)) {
Push(&Q->S2, Pop(&Q->S1));
}
return Pop(&Q->S2);
}
}
int main() {
struct Queue *Q = CreateQueue();
Q->S1 = Q->S2 = NULL;
EnQueue(Q, 1);
EnQueue(Q, 2);
EnQueue(Q, 3);
printf("%d ", DeQueue(Q));
printf("%d ", DeQueue(Q));
printf("%d ", DeQueue(Q));
return 0;
}
Output:
1 2 3

Related

second stack inside reverse function with pointers warning

I have the following stack implementation using doubly linked list, and I want to use a second stack inside the reverse function but I get errors. how to do that, let's say I want to return s2.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
//---------------------Stack---------------------
typedef struct Stack {
int size;
Node* head;
Node* tail;
int top;
} Stack;
const Stack stack_init = { .size = 0, .head = NULL, .tail = NULL, .top = -1 };
Node* create_node(int elm) {
Node* node = malloc(sizeof * node);
if (!node) return node;
node->data = elm;
node->prev = NULL;
node->next = NULL;
return node;
}
int is_empty_s(Stack *s) {
return s->tail == NULL;
}
void push(Stack *s, int elm) {
Node* updated_head = create_node(elm);
if (!s->head) {
s->head = updated_head;
s->tail = s->head;
} else {
updated_head->next = s->head;
s->head->prev = updated_head;
s->head = updated_head;
}
s->size++;
s->top = s->head->data;
}
int pop(Stack *s) {
if (!is_empty_s(s)) {
Node* node = s->head;
int elm = node->data;
s->head = s->head->next;
if (s->head) {
s->head->prev = NULL;
s->top = s->head->data;
}
else {
s->tail = NULL;
s->top = -1;
}
s->size--;
free(node);
return elm;
}
}
Stack* reverse_s(Stack *s) { // iterative: using another stack, queue
Stack *s2 = stack_init;
while (s->tail) {
push(s2, pop(s));
}
return s2;
}
int main() {
Stack s1 = stack_init;
// Queue queue1 = queue_init; { .size = 0, .head = NULL, .tail = NULL, .front = -1 };
push(&s1, 5);
push(&s1, 4);
return 0;
}
As you can see the reverse function is not yet completed, I am new to C and this syntax I don't know how to handle it.
In reverse_s, s2 is of type pointer-to-Stack. stack_init has the structure type Stack. The assignment of a Stack value to a Stack * variable is incompatible.
A few options for reverse_s:
Return a Stack structure.
Stack reverse_s(Stack *s) {
Stack s2 = stack_init;
while (s->tail)
push(&s2, pop(s));
return s2;
}
Return a pointer-to-Stack, dynamically allocating memory for the structure.
Stack *reverse_s(Stack *s) {
Stack *s2 = malloc(sizeof *s2);
*s2 = stack_init;
while (s->tail)
push(s2, pop(s));
return s2;
}
Modify the original structure.
void reverse_s(Stack *s) {
Stack s2 = stack_init;
while (s->tail)
push(&s2, pop(s));
memcpy(s, &s2, sizeof *s);
}

I am trying to reverse a linked list using recursion in C, I have some doubts on my recursive function

Below is the program
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
struct node* reverse_ll(struct node* hnode)
{
if(hnode == 0)
{
return 0;
}
if(hnode->next == 0)
{
head=hnode;
return hnode;
}
struct node* ptr=reverse_ll(hnode->next);
ptr->next=hnode;
hnode->next=0;
//return hnode;
}
void display()
{
struct node *ptr;
ptr=head;
if(ptr==0)
{
printf("empty");
}
else
{
while(ptr!=0)
{
printf("%d->",ptr->data);
ptr=ptr->next;
}
printf("null");
}
}
int main()
{
struct node* h;
lastinsert(1);
lastinsert(2);
lastinsert(3);
lastinsert(4);
lastinsert(5);
display();
h=reverse_ll(head);
display();
return 0;
}
In function reverse_ll() even if I comment "return hnode" I am getting the right output How is it possible where does ptr receives its address from when I comment "return hnode"?
output: 1->2->3->4->5->null
5->4->3->2->1->null
reverse_ll() must return a struct node * in the recursive case:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head;
void lastinsert(int data) {
struct node **c = &head;
for(; *c; c = &(*c)->next);
*c = malloc(sizeof(*head));
if (!(*c)) {
printf("malloc failed\n");
return;
}
(*c)->data = data;
(*c)->next = NULL;
}
struct node *reverse_ll(struct node* hnode) {
if(!hnode)
return NULL;
if(!hnode->next) {
head = hnode;
return hnode;
}
struct node *ptr=reverse_ll(hnode->next);
ptr->next=hnode;
hnode->next = NULL;
return hnode;
}
void display() {
if(!head) {
printf("empty");
return;
}
for(struct node *ptr = head; ptr; ptr = ptr->next) {
printf("%d->",ptr->data);
}
printf("null\n");
}
int main() {
for(int i = 1; i <= 5; i++) {
lastinsert(i);
}
display();
reverse_ll(head);
display();
// It's good practice to implement a function that frees you list
// which you would call here.
return 0;
}
and example run:
$ ./a.out
1->2->3->4->5->null
5->4->3->2->1->null

Add to linked list via tail pointer without 3 levels of indirection

I am working on a project that requires an implementation of a linked list. Before I started the project, I was reviewing the classic method of creating a linked list.
I realized that in the past, I had been adding elements to a linked list via the head pointer that traverses the list until it reaches the null pointer.
I figured out that there is no need to do it this way, and to implement it in a way that involves a tail pointer, but the only way I could think of is a one that involves a triple pointer or a global pointer. I was wondering, is there an easier approach that uses less levels of indirection?
#include <stdio.h>
#include <stdlib.h>
typedef struct list {
int val;
struct list *next;
} list;
void printlist(list *head);
void freelist(list *head);
void AddList(list ***tail, int val);
int main(void)
{
/* create first node */
list *head = (list *)malloc(sizeof(*head));
/* assign members of first node */
head->val = 1;
head->next = NULL;
/* create tail */
list **tail = &head->next;
/* add values to list */
AddList(&tail, 2);
AddList(&tail, 3);
AddList(&tail, 4);
AddList(&tail, 5);
AddList(&tail, 6);
AddList(&tail, 7);
AddList(&tail, 8);
AddList(&tail, 9);
/* print list */
printlist(head);
/* free list */
freelist(head);
/* exit program */
system("pause");
return 0;
}
void AddList(list ***tail, int val)
{
list *node = (list *)malloc(sizeof(*node));
node->val = val;
node->next = NULL;
**tail = node;
*tail = &node->next;
}
void printlist(list *head)
{
while (head) {
printf("%d\n", head->val);
head = head->next;
}
}
void freelist(list *head)
{
list *tmp = head;
while (head) {
head = head->next;
free(tmp);
tmp = head;
}
}
The tail pointer should point to the last node, not the next pointer of the last node:
In main:
/* create tail */
list *tail = head;
In AddList:
void AddList(list **tail, int val)
{
list *node = malloc(sizeof(*node));
node->val = val;
node->next = NULL;
(*tail)->next = node;
*tail = node;
}
You can do:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
typedef struct node NODE;
struct llist {
unsigned int count;
NODE *head;
NODE *tail;
};
typedef struct llist LLIST;
LLIST *create_list() {
LLIST *llist = malloc(sizeof(LLIST));
if (llist == NULL)
exit(EXIT_FAILURE);
llist->head = NULL;
llist->tail = NULL;
llist->count = 0;
return llist;
}
NODE *create_ll_node(int data) {
NODE *node = malloc(sizeof(NODE));
if (node == NULL)
exit(EXIT_FAILURE);
node->data = data;
node->next = NULL;
return node;
}
void add_list_head(LLIST *llist, int data) {
NODE *node = create_ll_node(data);
if (llist->head == NULL) {
llist->head = node;
llist->tail = node;
} else {
node->next = llist->head;
llist->head = node;
}
llist->count++;
}
void add_list_tail(LLIST *llist, int data) {
NODE *node = create_ll_node(data);
if (llist->tail == NULL) {
llist->head = node;
llist->tail = node;
} else {
llist->tail->next = node;
llist->tail = node;
}
llist->count++;
}
void delete_list_elements(NODE *llist_head) {
NODE *node, *tmp;
if (llist_head == NULL) {
printf ("List is empty.\n");
return;
}
node = llist_head;
while(node) {
tmp = node->next;
free(node);
node = tmp;
}
}
void delete_list(LLIST *llist) {
if (llist == NULL) {
printf ("Invalid list.\n");
return;
}
delete_list_elements(llist->head);
free(llist);
}
void display_list(const LLIST *llist) {
if (llist == NULL) {
printf ("Invalid list.\n");
return;
}
if (llist->head == NULL) {
printf ("List is empty.\n");
return;
}
NODE *node = llist->head;
while(node) {
printf ("data: %d\n", node->data);
node = node->next;
}
}
unsigned int get_list_element_count(const LLIST *llist) {
if (llist == NULL)
return 0;
return llist->count;
}
int main() {
LLIST *llist = create_list();
add_list_head(llist, 5);
add_list_head(llist, 4);
add_list_head(llist, 3);
add_list_head(llist, 2);
add_list_head(llist, 1);
add_list_tail(llist, 6);
add_list_tail(llist, 7);
add_list_tail(llist, 8);
add_list_tail(llist, 9);
add_list_tail(llist, 10);
display_list(llist);
printf ("Total elements in list : %u\n", get_list_element_count(llist));
delete_list(llist);
return 0;
}

Code to insert node at last position in C programming language

I am writing a code on linklist in C programming language. When I am using an online compiler my code is working fine but when I am using Codeblock to run the code, the code is not working. I am posting the code kindly provide me solution. My code is to add node in a linklist on the last position.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node* Head;
void insert(int);
void print();
int main()
{
Head = NULL;
insert(2);
insert(3);
insert(4);
print();
return 0;
}
void insert(int a)
{
struct Node* temp1=(struct Node*)malloc(sizeof(struct Node));
temp1-> data = a;
temp1-> next = NULL;
if(Head == NULL)
{
Head = temp1;
return;
}
struct Node* temp = Head;
while(temp->next!= NULL)
{
temp = temp->next;
}
temp-> next = temp1;
}
void print()
{
struct Node* temp2=Head;
while(temp2 != NULL)
{
printf("%d \n", temp2->data);
temp2 = temp2->next;
}
return;
}
Here is how you can achieve what you want to do.
I suggest you to use a top-down approach when programming.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *next;
};
struct node *Insert(struct node *, int);
void Print_List(struct node *);
void Remove_List(struct node *);
int main(int argc, char **argv)
{
struct node *head;
head = NULL;
head = Insert(head, 10);
head = Insert(head, 20);
Print_List(head);
Remove_List(head);
head = NULL;
return 0;
}
struct node *Create_New_Node(int);
struct node *Head_Insert(struct node *, int);
struct node *Queue_Insert(struct node *, int);
struct node *Insert(struct node *, int);
void Print_List(struct node *);
void Remove_List(struct node *);
struct node *Insert(struct node *top, int elem)
{
if(top == NULL)
{
top = Head_Insert(top, elem);
}
else
{
top = Queue_Insert(top, elem);
}
return top;
}
struct node *Create_New_Node(int elem)
{
struct node *new_node;
new_node = (struct node *)malloc(sizeof(struct node));
if(new_node != NULL)
{
new_node -> info = elem;
new_node -> next = NULL;
}
return new_node;
}
struct node *Head_Insert(struct node *top, int elem)
{
struct node *new_node = Create_New_Node(elem);
if(new_node != NULL)
{
new_node -> next = top;
}
return new_node;
}
struct node *Queue_Insert(struct node *top, int elem)
{
if(top != NULL)
{
if(top -> next != NULL)
{
top -> next = Queue_Insert(top -> next, elem);
}
else
{
struct node *new_node = Create_New_Node(elem);
if(new_node != NULL)
{
top -> next = new_node;
}
}
}
return top;
}
void Print_List(struct node *top)
{
while(top != NULL)
{
printf("\nInfo : %d\tAddress : %u\tNext link address : %u\n", top -> info, top, top -> next);
top = top -> next;
}
return;
}
void Remove_List(struct node *top)
{
if(top != NULL)
{
Remove_List(top -> next);
top -> next = NULL;
free(top);
}
return;
}
Sample output :
Info : 10 Address : 39149584 Next link address : 39149616
Info : 20 Address : 39149616 Next link address : 0

Delete all items from my queue

I can't delete all my items from my queue. Here is what I am trying to do and here is all my code. Please I can't see what I am doing wrong and also I want to store the count of how many items where in my queue.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int count = 0;
struct Node
{
int Data;
struct Node* next;
}*rear, *front;
void delQueue()
{
struct Node *var=rear;
while(var!=NULL)
{
free(var);
var = var->next;
count = count + 1;
}
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=NULL;
}
}
void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements in queue are: ");
while(var!=NULL)
{
printf("\t%d",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty\n");
}
problem is simply here:
free(var);
var= var->next;
You can do like this:
struct Node* buf=var->next;
free(var);
var=buf;

Resources