Delete all items from my queue - c

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;

Related

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

Why does the following code display a blank linked list?

Why does the following code display a blank linked list? I want to insert single characters to each node in the linked list. The characters are from the ch string. It works fine when I change the node member sc to integer and modify the associated code. But it seems there is an issue with using characters.
#include <stdio.h>
#include <string.h>
struct node {
char sc[1];
struct node *next;
};
char ch[30];
void insert(struct node **head_ref,int j) {
struct node *new_node = (struct node*)malloc(sizeof(struct node));
struct node *last = *head_ref;
new_node->sc[0] = ch[j];
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref=new_node;
return;
}
while (last->next != NULL) {
last = last->next;
}
last->next = new_node;
return;
}
void display(struct node *head) {
struct node *t = head;
while (t != NULL) {
printf("%s", t->sc);
t = t->next;
}
}
main() {
FILE *fp;
fp = fopen("C:\\Users\\Jefferson Warie\\Desktop\\input.txt", "r");
if (fp == NULL) {
printf("File could not be opened.");
}
struct node *num1h;
num1h = NULL;
int i = 0, j;
char arr[100][30];
char ch[30];
while (!feof(fp)) {
fgets(arr[i], 31, fp);
i++;
}
for (i = 0; i < 2; i++) {
strcpy(ch, arr[i]);
for (j = 0; ch[j] != '\0'; j++) {
if (i == 0) {
insert(&num1h, j);
}
}
}
printf("First linked list: ");
display(num1h);
}
You have declared character array ch twice — once as a global variable and once local to main.
Also, you need to add #include<stdlib.h> to use malloc
This is the final updated code.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
char sc[1];
struct node *next;
};
char ch[30];
void insert(struct node **head_ref,int j) {
struct node *new_node=(struct node*)malloc(sizeof(struct node));
struct node *last=*head_ref;
new_node->sc[0]=ch[j];
new_node->next=NULL;
if (*head_ref==NULL) {
*head_ref=new_node;
return;
}
while(last->next!=NULL) {
last=last->next;
}
last->next=new_node;
return;
}
void display(struct node *head) {
struct node *t=head;
while(t!=NULL) {
printf("%s",t->sc);
t=t->next;
}
}
void main() {
FILE *fp;
fp=fopen("C:\\Users\\Jefferson Warie\\Desktop\\input.txt","r");
if(fp==NULL) {
printf("File could not be opened.");
}
struct node *num1h;
num1h=NULL;
int i=0,j;
char arr[100][30];
while(!feof(fp)) {
fgets(arr[i],31,fp);
i++;
}
for(i=0; i<2; i++) {
strcpy(ch,arr[i]);
for(j=0; ch[j]!='\0'; j++) {
if(i==0) {
insert(&num1h,j);
}
}
}
printf("First linked list: ");
display(num1h);
}

Implement a Queue using two Stacks in 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

How to add to end in linked list C

I wrote a simple program that should add some numbers inside a linked-list, then remove a specific one and print the elements at the end but it doesn't add all the elements, it simply adds the first one and the last one. And also the delete function gives me problems because it doesn't delete anything but it keep's saying that the list's empty. Thanks for the help everybody
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int info;
struct node *next;
}node;
typedef node *list;
list L;
list del(list L, int elem)
{
node *current, *temp;
current=L;
if(L==NULL)
{
printf("there's nothign to delete\n");
}
if((L->next!=NULL)&&(L->info==elem))
{
current=L->next;
free(L);
L=current;
return(L);
}
else
{
temp=del(L->next, elem);
return(L);
}
}
list addEnd(list L, float elem)
{
node *punt, *curr, *new_node;
if(L==NULL)
{
punt=malloc(sizeof(node));
punt->info=elem;
punt->next=NULL;
L=punt;
return(L);
}
else
{
punt=L;
curr=L;
while(punt->next!=NULL)
{
curr=punt;
punt=punt->next;
}
new_node=malloc(sizeof(node));
new_node->info=elem;
new_node->next=NULL;
curr->next=new_node;
return(L);
}
}
void print(list L)
{
node *current = L;
if(current==NULL)
{
printf("list's empty");
}
while (current != NULL)
{
printf("%d\n", current->info);
current = current->next;
}
}
int main()
{
int n1,n2,n3,n4;
n1=1;
n2=10;
n3=23;
n4=45;
L=addEnd(L, n1);
L=addEnd(L, n2);
L=addEnd(L, n3);
L=addEnd(L, n4);
L=del(L,23);
print(L);
return(0);
}
Try this correction, you have two mistake one in delete and another one in add:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int info;
struct node *next;
}node;
typedef node *list;
list L;
list del(list L, int elem)
{
// no need to use recursive function
node *current, *temp, *prev;
temp = L;
prev = L;
if(temp==NULL)
{
printf("there's nothign to delete\n");
}
while((temp!=NULL))
{
if(temp->info==elem)
{
current=temp->next;
if(L == temp)
L = current;
else
prev->next=current;
free(temp);
return(L);
}
// you should keep the previous node
prev = temp;
temp = temp->next;
}
return(L);
}
list addEnd(list L, int elem)
{
node *punt/*, *curr*/, *new_node;
if(L==NULL)
{
punt=malloc(sizeof(node));
punt->info=elem;
punt->next=NULL;
L=punt;
return(L);
}
else
{
punt=L;
// curr=L;
while(punt->next!=NULL)
{
//curr=punt;
//no needed
punt=punt->next;
}
new_node=malloc(sizeof(node));
new_node->info=elem;
new_node->next=NULL;
// this instruction is wrong curr->next=new_node; it erase the last value
punt->next=new_node;
return(L);
}
}
void print(list L)
{
node *current = L;
if(current==NULL)
{
printf("list's empty");
}
while (current != NULL)
{
printf("%d\n", current->info);
current = current->next;
}
}
int main()
{
int n1,n2,n3,n4;
n1=1;
n2=10;
n3=23;
n4=45;
L=addEnd(L, n1);
L=addEnd(L, n2);
L=addEnd(L, n3);
L=addEnd(L, n4);
L=del(L,23);
print(L);
return(0);
}

Inorder traversal is printing in wrong order

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
struct node{
char *name;
struct node *lchild;
struct node *rchild;
};
void find(char *str,struct node **root,struct node **loc,struct node **par)
{
struct node *ptr,*ptrsave;
*loc=NULL;
*par=NULL;
if(*root==NULL)
{
return;
}
if(!strcmp((*root)->name,str))
{
*loc=*root;
return;
}
ptrsave=NULL;
ptr=*root;
while(ptr!=NULL)
{
if(!strcmp(ptr->name,str)) break;
ptrsave=ptr;
if(strcmp(ptr->name,str)>0)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}
*loc=ptr;
*par=ptrsave;
}
void insert(struct node **p,char *str)
{
struct node *location,*parent,*temp;
find(str,&(*p),&location,&parent);
if(location!=NULL)
{
printf("Element already exists\n");
return;
}
temp=(struct node *)malloc(sizeof(struct node));
temp->name=strdup(str);
temp->lchild=NULL;
temp->rchild=NULL;
if(parent==NULL)
{
*p=temp;
return;
}
else
{
if(strcmp(parent->name,str)>0)
parent->lchild=temp;
else
parent->rchild=temp;
}
}
void inorder(struct node *root)
{
if(root!=NULL)
{
preorder(root->lchild);
printf("[%30s]\n",root->name);
preorder(root->rchild);
}
}
int main()
{
struct node *root=NULL;
insert(&root,"Crocin");
insert(&root,"Acetyl");
insert(&root,"Colchichine");
insert(&root,"Diclofenac_50mg");
insert(&root,"Diclofenac_25mg");
insert(&root,"Morphine Sulphate");
insert(&root,"Fentanyl");
insert(&root,"Dolo");
insert(&root,"Ibuprofen");
insert(&root,"Tramadol");
insert(&root,"Paracetamol");
inorder(root);
getchar();
return 0;
}
This is the code i am using for creating and inserting nodes in a binary search tree. After that i created recursive inorder function. but this is not giving the right ouput. I am not able to find error that is creating discrepancy. Can someone suggest where i am going wrong?
Change
void inorder(struct node *root)
{
if(root!=NULL)
{
preorder(root->lchild);
printf("[%30s]\n",root->name);
preorder(root->rchild);
}
to
void inorder(struct node *root)
{
if(root!=NULL)
{
inorder(root->lchild);
printf("[%30s]\n",root->name);
inorder(root->rchild);
}
You are using another function (preorder) (not listed in your code)

Resources