- Computers & Software»
- Computer Science & Programming
C program code for queue data structure
Elements in a queue are served in the form of First-In-First-Out manner always. A queue data structure adds data to the rear end called enqueue and removes data from the front end called dequeue. In Computer Science, a queue performs the functions of a buffer. Queues are common in computer programs that are coupled with access routines.
C program for queue operations takes ‘data’as the data item in the structure ‘que’. The ‘front’ and ‘rear’ pointers to the structure point to the first and last elements in the queue respectively. They are initialized to point to the first element of the queue and after creating the queue, the push and pop operations are carried out with the help of the functions push() and pop().
1. Queue creation
Creating a queue function takes the pointer ‘ptr’ as the parameter. As elements are added to the rear end of a queue always, pointer ‘rear’ is changed each time. ’ ptr->data’ stores the values of the data input and each time data is being read, the next node of pointer ‘rear’ is linked to the new data. Then, the pointer ‘rear’ itself is made to point to the new data. The checking value ‘-999’ stops the creation of the queue.
C code for creating the queue should be written with care considering all the loopholes. A small mistake can cause runtime errors while executing the program.
Flowchart for the function createque(que *ptr)
2. Push Operation
Pushing elements into a queue is done by the function ‘void push()’. The pointer to member ‘ptr->data’ reads the value of the new element that gets pushed into the queue. It is added to the rear end by linking the next node of ‘rear’ pointer to ‘ptr’. Then ‘rear’ pointer itself is made to point to ‘ptr’ just like we did while creating the queue.
One thing to notice is ‘front’ pointer remains unchanged during the creation and during the push operation of a queue.
Flowchart for the function push()
3. Pop Operation
Pop operation removes one element from the front end of the queue. The element that gets served first should be removed first. Here ‘front’ pointer comes into action. Pointer ‘ptr’ is initialized to ‘front’ pointer first and then ‘front’ pointer is linked to its next node. Then the built-in function free(ptr) removes the first element of the queue. Pop operation of a queue is similar to that of a stack.
Flowchart for the function pop()
Output of pop operation
4. Displaying a queue
This function displays the elements of a queue in the same manner as in a linked list or stack. Pointer ‘cur’ is initialized to ‘front’ pointer and the value of ‘cur->data’ is displayed first. Then ‘cur’ is incremented and the elements of the queue are displayed one by one by iterating in a loop. When ‘cur’ reaches ‘NULL’ value, iteration stops and display function is exited.
Flowchart for display() function
A queue is an example of a linear data structure or more abstractly a sequential collection. Check-out lines, escalators and ATMs use queues. In each case, the customer at the front of the line is the first one to enter, while at the end of the line is the last to have entered (enqueue). Every time a customer finishes his job, he leaves the queue from the front. This represents the “dequeue” function.
C program code
struct queue *next;
typedef struct queue que;
que *front, *rear;
void createque(que *);
printf("\n\n\tCreate a queue(-999 to end):\n\n");
printf("\n\n\tGiven queue is");
printf("\n\n\tEnter your choice:");
printf("\n\tQueue after push operation is:\n\n");
case 2: pop();
printf("\n\tQueue after pop operation is:\n\n");
case 3: exit(1);
void createque(que *ptr)
printf("\n\n\tEnter the data:");
printf("\n\tEnter the data to push into the queue:");
Do you find this code easy to follow?
- C program code for stack data structure
Stacks are memory areas that store function attributes in a Last In First Out manner (LIFO). Whenever a function is called, its parameters and local variables get pushed to the top of the stack for faster execution.
- C program code for linked list manipulations
Linked lists are data structures that are user friendly. They not only save memory space, but also reduce the complexity in the manipulation of list elements such as insertion or deletion.
- About C Programming; An Overview
Why C has gained this much popularity? It is its portability that differentiates it from other programming languages. A program written in C in one computer system can be transferred to another with minimal changes. This is why the name ‘C’ is most h