-
-
Save mycodeschool/7510222 to your computer and use it in GitHub Desktop.
/*Queue - Linked List implementation*/ | |
#include<stdio.h> | |
#include<stdlib.h> | |
struct Node { | |
int data; | |
struct Node* next; | |
}; | |
// Two glboal variables to store address of front and rear nodes. | |
struct Node* front = NULL; | |
struct Node* rear = NULL; | |
// To Enqueue an integer | |
void Enqueue(int x) { | |
struct Node* temp = | |
(struct Node*)malloc(sizeof(struct Node)); | |
temp->data =x; | |
temp->next = NULL; | |
if(front == NULL && rear == NULL){ | |
front = rear = temp; | |
return; | |
} | |
rear->next = temp; | |
rear = temp; | |
} | |
// To Dequeue an integer. | |
void Dequeue() { | |
struct Node* temp = front; | |
if(front == NULL) { | |
printf("Queue is Empty\n"); | |
return; | |
} | |
if(front == rear) { | |
front = rear = NULL; | |
} | |
else { | |
front = front->next; | |
} | |
free(temp); | |
} | |
int Front() { | |
if(front == NULL) { | |
printf("Queue is empty\n"); | |
return; | |
} | |
return front->data; | |
} | |
void Print() { | |
struct Node* temp = front; | |
while(temp != NULL) { | |
printf("%d ",temp->data); | |
temp = temp->next; | |
} | |
printf("\n"); | |
} | |
int main(){ | |
/* Drive code to test the implementation. */ | |
// Printing elements in Queue after each Enqueue or Dequeue | |
Enqueue(2); Print(); | |
Enqueue(4); Print(); | |
Enqueue(6); Print(); | |
Dequeue(); Print(); | |
Enqueue(8); Print(); | |
} | |
There is a mistake in dequeue method ..you need to do temp = temp->next;
then free(first); first = temp;
You are freeing the second element each time and if free->next is null then it will crash.
Shouldn't you return something with the Dequeue function?
Why have you used the Front function? It's never really called. Please explain the purpose of this function definition.
@MRSharff - The purpose of dequeue function is to delete a node from the linked list. It has nothing to do with returning something.
Thanks a lot for this sample code.
Hey, There is a typo in Front function, when front==NULL - > return -1 or something instead of nothing. 👍
Your code is very helpful here is another example hope it adds to your concept.
C++ queue implementation using linked list
There is a simpler one here
Hi you have only 2 free from 5 allocs
==9712== HEAP SUMMARY:
==9712== in use at exit: 48 bytes in 3 blocks
==9712== total heap usage: 5 allocs, 2 frees, 1,088 bytes allocated
==9712==
==9712== LEAK SUMMARY:
==9712== definitely lost: 0 bytes in 0 blocks
==9712== indirectly lost: 0 bytes in 0 blocks
==9712== possibly lost: 0 bytes in 0 blocks
==9712== still reachable: 48 bytes in 3 blocks
==9712== suppressed: 0 bytes in 0 blocks
what we must do if the Data in our struct was int and string (number and name of student)
can anyone tell me what is the use of the function int Front in this code?I am confused.
Thanks in advance!
@labeelola This is when you want to view, the latest value in front of the queue. This is not called, but that's not a problem, you can call it anywhere. It is just to view.
Hey guys, Please help me to complete my Assignment, I dont know much about C Programming
-
Create a Queue.h file. This file contains:
Node definition - int number, and a node pointer.
Queue definition - head, tail, nodeCount
Set of functions -
a. void CreateQueue(Queue *q)
Precondition: none. Postcondition: Queue q has been created and initialized to be empty.
b. Boolean CheckQueueEmpty(Queue *q)
Precondition: The queue q exists and has been initialized. Postcondition: Function returns ture or false accordingly as q is empty or not.
c. void enqueue(int number, Queue *q)
Precondition: The queue q exists and has been initialized. Postcondition: The argument item has been stored at the end of the queue.
d. int dequeue(Queue *q)
Precondition: The queue q exists and has been initialized. Postcondition: The first entry of the queue has been removed and returned the node number. If q is an emtpy queue, then returns -999.
-
Create a Queue.c file. This file contains all the functions implementation listed in Queue.h.
Remark: ADT Queue is "First in First Out".
enqueue - always add a new node at the back of the queue.
dequeue - delete the head of the queue.
Thank you so much
sir make videos on other topics also,i observed that u r not uploading videos from past 2-3 years ,why sir?
THANKS ....IT'S SIMPLE AND COOL ....
Why have you used the Front function? It's never really called. Please explain the purpose of this function definition.
Its to get the first element of the Queue(He didn't call it though)
If anyone tried with local front and rear variables in main method !
in the Dequeue function when the second if(front==rear) runs we have to free the space of node pointed by front previously i.e first node is the one when front and rear are equal if we modify the pointer front and rear to NULL we are wasting the memory of first node that was created
please reply
ASAP
There is a mistake in dequeue method ..you need to do temp = temp->next;
then free(first); first = temp;
You are freeing the second element each time and if free->next is null then it will crash.
i understand your problem , but see , the case that you're mentioning is when we have only one node in the queue , and here we've explicitly considered that case which is when both front and rear are equal . In that we're deleting simply that one node , not performing
any operation involving , and after the deletion of that single node both front and rear would be pointing to NULL .
I hope i've explained it well .
Mycodeschool will always be remembered
Best course on youtube no doubt
same code in java pls provid it
Thnx 😄 ^_^