Queue using linked list c program

#include
#include
struct node{
       int info;
       struct node *link;
       }*front=NULL,*rear=NULL;
       
       void insert(int num);
       int del();
       void display();
       
       main()
       {
             int choice, num;
             while(1)
             {
             printf("\n1 Insert\n");
             printf("2 Delete\n");
             printf("3 Display\n");
             printf("4 Exit\n");
             printf("Enter your choice\n");
             scanf("%d",&choice);
             switch(choice)
             {
                           case 1:
                                 printf("Enter an element to insert ");
                                 scanf("%d",&num);
                                 insert(num);
                                 break;
                           case 2:
                                del();
                                break;
                           case 3:
                                display();
                                break;
                           case 4:
                           	exit(1);
                           	
                           default:
                           	printf("Wrong Input\n");
                           	
             }
       }
}

void insert(int num)
{
     struct node *temp;
     temp=(struct node *)malloc(sizeof(struct node));
     if(temp==NULL)
     {
                  printf("Overflow\n");
                  return;
     }
     temp->info=num;
     temp->link=NULL;
     if(front==NULL)
      {
       front=temp;
      }
     else
      {
      rear->link=temp;
      }
       
	   rear=temp;
      }
     
     int del()
     {
          int num;
          struct node *temp;
          if(front==NULL)
          {
                         printf("Underflow\n");
                         return;
          }
          temp=front;
          num=temp->info;
          front=front->link;
          free(temp);
          return num;		
     }
     
     void display()
     {
                         struct node *p;
                         if(front==NULL)
                         {
                         printf("Underflow\n");
                         return;
                         }
                         p=front;
                         printf("\nQueue Elements:\n");
                         while(p!=NULL)
                         {
                                         printf("%d ",p->info);
                                         p=p->link;
                         }
                                         
                                  
                                  }         

Queue using linked list

A queue has two ends front and the rear end. In a queue elements are added from the rear end and they are deleted from the front end of the queue. Implementing queue using a linked list requires it to follow the same principle ie; deletion from front and insertion from the rear end.

Initially in the program Front and Rear are both initialized to NULL.
If the value of front is equal to NULL then it means queue is empty.
if(front == NULL)
{
/**empty queue**/
}
You may see in the above image the front pointer variable stores the address (1002) of the first node  and the rear pointer variable stores the address of the last node in the list.
If there exists only one node then that node is the first as well as the last node in the list.

Since we know the address of the first node which is stored in variable front we can delete nodes at the beginning of the list which would mean deletion from the front end of the queue.
Similarly we know the address of the last node which is held by pointer variable rear so we can add nodes at the end of the linked list which would mean insertion at the rear end of the queue.

 void insert(int num)
{
     struct node *temp;
     temp=(struct node *)malloc(sizeof(struct node));
     temp->info=num;
     temp->link=NULL;
     if(front==NULL)
      {
       front=temp;
      }
     else
      {
      rear->link=temp;
      }
       rear=temp;
   }

Above is the function for insertion.
Every time this function gets executed temp will hold the address of the newly created node.
temp->link=NULL;  set the link part of the newly created node to NULL because nodes will be added at the end of the linked list rear end means the end of the list and the link part of the last node is always null.

  
   if(front==NULL)
{
front=temp;
}

if the value of front is null and the insert function is called then front should be assigned the address of temp which is the newly created node because front should store the address of the first node in the list. Front == null means empty list and if we insert a node into an empty list then that node is the first as well as last node in the list.
     else
{
rear->link=temp;
}

If the above if statement is not true then it means we are inserting node into a list which already contains at least one node in that case the value of front doesn’t change, so the else condition will become true. rear->link=temp; the link part of the last node should be assigned the address of the newly created node ie; temp thus making the newly inserted node the last node in the list added from the rear end.
rear=temp; update the value of rear, assign rear the address of the new node which is the last node in the list. View one of the image above rear should store the address of  the last node in the list.

Deletion from the front end of the queue

int del()

     {

          int num;

          struct node *temp;

          if(front==NULL)

          {

                         printf("underflown");

                         return;

          }

          temp=front;
          num=temp->info;
          front=front->link;
          free(temp);
          return num;       

     }

The del() function is very simple everytime this function is called and if the list is not empty then the first node is going to get deleted.
temp=front; assign temp the value of front which is the address of the node which would get deleted.
front=front->link; front->link means the link part of the first node which has the address of the second node or null if there is only one node in the list. The second node becomes the first node when a node from the front or beginning of the list is deleted and front should store the address of the new first node.
free(temp); temp stores the address of the node which is to be deleted and free function frees the memory and deletes that node completely from the memory.
return num; del function is of type int and an integer value should be returned.

void display()
      {
                     struct node *p;
                     if(front==NULL)
                     {
                      printf("underflown");
                      return;
                      }
                     p=front;
                     printf("nQueue:n");
                     while(p!=NULL)
                     {
                      printf("%d ",p->info);
                      p=p->link;
                     }
                                         
                                  
        }

C program implementing Stack using a Linked List

Previous method: https://marxtudor.com/stack-using-linked-list-c-program/
In this method pointer variable top is not declared in the global section its declared in the main() function. So we need to return the value of top each time the value of top is changed when the node is inserted or deleted so that the value of top gets updated.

#include
#include
struct node{
       int info;
       struct node *link;
       };
       
       void display(struct node *top);
       struct node *push(struct node *top, int num);
       struct node *pop(struct node *top);

       main()
       {
             struct node *top=NULL;
             int choice, num;
             
             
             while(1)
             {
                     
                     printf("n1 Pushn");
                     printf("2 Popn");
                     printf("3 Displayn");
                     printf("4 Exitnn");
                     
                     scanf("%d",&choice);
                     switch(choice)
                     {
                                 
                                   case 1:
                                        printf("Enter the number to be insertedn");
                                        scanf("%d",&num);
                                       top = push(top, num); 
                                        break;
                                    case 2:
                                         top = pop(top);
                                         break;
                                    case 3:
                                      display(top);
                                      break;
                                    case 4:
                                         exit(1);

                                    default:
                                    printf("Invalid Input");
                     }                  
                     
         }
       
                    
}


 struct node *push(struct node *top, int num)
 {
        struct node *temp;
        temp=(struct node *)malloc(sizeof(struct node));
        temp->info=num;
        temp->link = top;
        top = temp;
        return top;
          
 }

struct node *pop(struct node *top)
{
     struct node *temp;
    int num;
    if (top==NULL)
    {
         printf("underflown");
         return;
    }
    temp=top;
    num = top->info;
    top = top->link;
    free(temp);
    printf("Deleted number is %d",num);
    return top;   
}

void display(struct node *top)
{
     struct node *p;
     if(top==NULL)
     {
                    printf("nList is emptyn");
                    return;
     }
     p=top;
     printf("n Stack is: n");
     while(p != NULL)
     {
                   printf("%dn", p->info);
                   p=p->link;
     }
}

Stack using Linked List C program

Another approach to create the same program
https://marxtudor.com/c-program-implementing-stack-using-a-linked-list/

Implementing stack using a linked list

#include
#include
struct node
{
int info;
struct node *link;
}*top=NULL;
   
void push(int num);
int pop();
void display();

main()
{
    int choice, num;
    while(1)
    {
        printf("n1 Pushn");
        printf("2 Popn");
        printf("3 Displayn");
        printf("4 Exitnn");
        
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                printf("Enter a number to be insertedn");
                scanf("%d",&num);
                push(num);
                break;
            case 2:
                num = pop();
                break;
            case 3:
                 display();
                 break;
           case 4:
                 exit(1);
          default:
                 printf("Invalid Input");
        }
    }
}
void push(int num)
{
    struct node *temp;
    temp = (struct node *)malloc(sizeof(struct node));
     if (temp==NULL)
 {
               printf("stack overflown");
               return;
}
 temp->info = num;
 temp->link = top;
 top = temp;   
}

int pop()
{
    struct node *temp;
    int num;
    if (top==NULL)
    {
         printf("underflown");
         return;
    }
    temp=top;
    num = top->info;
    top = top->link;
    free(temp);
    return num;
}

void display()
{
    struct node *p;
        if (top==NULL)
    {
         printf("underflown");
         return;
    }
    printf("n Stack is: n");
    p=top;
    while (p!=NULL)
         {
                   printf("%dn",p->info);
                   p=p->link;
     }
    
}

Stack using linked list

When implementing stack using an array we make an array behave like a stack in the same way here we need to make a linked list behave like a stack to implement stack using a linked list. We do this by following a property of stack which is, an element can only be inserted and deleted from one end called the top end of the stack.

In the image above there is a linked list containing 4 elements  imagine it as a stack. The first node is the top position. Since in a stack elements can only be inserted and deleted from the top end so in linked list implementation of stack we should only allow nodes to be added at the beginning of the linked list and also deletion should be allowed at the beginning of the linked list only.

Nodes store data which can be integer value, float etc. and allowing nodes to be inserted and deleted from one end of the linked list ie; the top end will make a linked list behave like a stack. This is called implementing stackc using a linked list.

What functions do we need in our program?
We need a function to insert nodes at the beginning of the linked list
A function to delete nodes at the beginning of the list
and a display function to display the elements.
If you know linked list or you understood the previous tutorial on linked lists then you very must know how to make those functions listed above.
The only difference is that instead of naming the pointer variable start which holds the address of the first node, here we named it as top as it sounds more appropriate in the case of stack.

Structure of the nodes
struct node
{
int info;
struct node *link;
}*top=NULL;

Pointer top is of type struct node and it is assigned the value of NULL. Remember here we have declared and initialized top in global section of the program so its not local to any function all the functions will have access to the top variable and its value will be updated without returning the value of top in the functions.

void push(int num)
{
	struct node *temp;
	temp = (struct node *)malloc(sizeof(struct node));
	 if (temp==NULL)
 {
               printf("stack overflown");
               return;
}
 temp->info = num;
 temp->link = top;
 top = temp;	
}

You should know that in case of a linked list memory is dynamically allocated during run time with the help of malloc() function but if there is no memory left to be allocated or the system runs out of memory then malloc function returns NULL value and it is assigned to temp so if temp is equal to null it means there is no memory left and stack is full.
If it is not full then malloc() returns the address of the allocated memory location and assigns it to temp.
 temp->info = num;
temp->link = top;
top = temp;

temp is the newly created node and num is the number entered by the user which is going to get stored in the node which the help of  the statement temp->info = num

 Top holds the address of the first node or NULL in case of an empty list.
 temp->link = top; it assigns link part of the newly created node the value of top. In case of an empty list null is assigned and in case of a non-empty list the address of the first node is assigned to the link part of the new node making the node the first node in the list and the previously the node which was the first node now becomes the second node in the list.
top = temp; pointer variable temp has the address of the new node which is assigned to top because top should always store the address of the first node.

Deletion from the top or from the beginning of the list.

int pop()
{
	struct node *temp;
	int num;
	if (top==NULL)
	{
		 printf("underflown");
		 return;
	}
	temp=top;
	num = top->info;
	top = top->link;
	free(temp);
	return num;
}

If top is equal to null then list is empty and its an underflow condition for a stack.

 

temp=top; assigning temp the value of top which is the address of the node to be deleted ie; the first node.
num = top->info; assigning num top->info which is the data or number stored in the info part of the first node to be returned later because the function pop() is of type int not void.
top = top->link; top should store the address of the first node, when the first node is deleted the second node in the list should become the first node and top->link which means the link part of the first node contains the address of the second node which is assigned to top.
free(temp); temp has the address of the first node which is to be deleted and free function completely deletes it by freeing that memory location or address. Dynamically allocated memory has to be freed using free function.

Display function:

void display()
{
	struct node *p;
		if (top==NULL)
	{
		 printf("underflown");
		 return;
	}
	printf("n Stack is: n");
	p=top;
	while (p!=NULL)
	     {
                   printf("%dn",p->info);
                   p=p->link;
     }
     
}
alert('dsf'); console.log("dsdsdsd");