Stack and Queue Implementation in C

Stack Implementation in C

Introduction

This code implements a stack data structure using a linked list in C. A stack follows the Last-In, First-Out (LIFO) principle. The program provides options to push (insert), pop (remove), display, and free the stack.

Code Explanation

The code defines a structure NODE with an integer element ele and a pointer hur to the next node. The main function presents a menu with options to perform stack operations. The sartu function pushes an element onto the stack. The kendu function pops an element from the stack. The display function displays the elements of the stack. The free function deallocates the memory used by the stack.

Key Functions

  • sartu(NODE **top): Adds a new node to the top of the stack.
  • kendu(NODE **top): Removes the node at the top of the stack.
  • display(NODE *top): Prints the elements of the stack.
  • free(NODE **top): Frees the memory allocated for the stack.

Queue Implementation in C

Introduction

This code implements a queue data structure using a linked list in C. A queue follows the First-In, First-Out (FIFO) principle. The program provides options to enqueue (insert), dequeue (remove), display, and free the queue.

Code Explanation

Similar to the stack implementation, the queue code uses the same NODE structure. The main function provides a menu for queue operations. The sartu function enqueues an element to the rear of the queue. The kendu function dequeues an element from the front of the queue. The display function displays the elements of the queue. The free function deallocates the memory used by the queue.

Key Functions

  • sartu(NODE **top): Adds a new node to the rear of the queue.
  • kendu(NODE **top): Removes the node at the front of the queue.
  • display(NODE *top): Prints the elements of the queue.
  • free(NODE **top): Frees the memory allocated for the queue.