Data Structure (linked list, Queue, Stack)

Data Structure (Linked list, Queue,  Stack)






STUDY EVERTHING



Linked List
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
void insertbegin();
void display();
void insertend();
int length();
void insertatpos();
void deletebegin();
void deleteEnd();
void deletepos();
struct node *root=NULL;
int main()
{
int choice;
while(1)
{
  printf("\nPress 1. for Insert node at begaining\n");
  printf("Press 2. for Insert node at End\n");
  printf("Press 3. for Insert node at At given position\n");
  printf("Press 4. for Delete node from begaining\n");
  printf("Press 5. for Delete node from End\n");
  printf("Press 6. for Delete Node from given position\n");
  printf("Press 7. for Display Linked List\n");
  printf("Press 8. for Find Length of Linked List\n");
  printf("Press 9. for Quit\n");
  printf("Enter your Choice : ");
  scanf("%d",&choice); 

   switch(choice)
  {
   case 1 : insertbegin();
      break;    
    case 2 : insertend();
      break;   
    case 3 : insertatpos();
      break;    
    case 4 : deletebegin();
      break;    
    case 5 : deleteEnd();
      break;    
    case 6 : deletepos();
      break;    
    case 7 : display();
      break;    
    case 8 : printf("%d",length());
      break;    
    case 9 : exit(0);   
    default: printf("Invalide Input please check : \n");
  }
}
return 0;
}

void insertbegin()
{
int data;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter Data To insert : ");
scanf("%d",&data);
temp->data=data;
temp->link=NULL;
  if(root==NULL)
{
  root=temp;
  printf("Node is Inserted Success Fully \n");
}
else
{
  temp->link=root;
  root=temp; 
}
}

void display()
{
struct node *temp;
temp=root;

  while(temp!=NULL)
{
  printf("%d-->",temp->data);
  temp=temp->link;
}
printf("\n\n");
}

void insertend()
{
int data,i=0;
int len=length();
struct node *temp, *p;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter Data To insert : ");
scanf("%d",&data);
temp->data=data;
temp->link=NULL;
p=root;

  if(root==NULL)
{
  root=temp;
  printf("Node is Inserted Success Fully \n");
}
else
{
  while(i<len-1)
  {
   p=p->link;
   i++;
  }
  p->link=temp;
  printf("Node inserted successfully\n");
}
}

int length()
{
int length=0;
struct node *temp;
temp=root;
while(temp!=NULL)
{
  length++;
  temp=temp->link;
}
return length;
printf("\n");
}

void insertatpos()
{
int data,loc,i=1;
struct node *temp,*p;
printf("Enter position where you want to ensert data\n");\
scanf("%d",&loc);
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter Data To insert : ");
scanf("%d",&data);
temp->data=data;
temp->link=NULL;
p=root;
  if(loc>length())
{
  printf("Invalid Location\n");
}
else
{
  while(i<loc-1)
  {
   p=p->link;
   i++;
  }
  temp->link=p->link;
  p->link=temp;
}
}

void deletebegin()
{
struct node *temp;
temp=root;
if(root==NULL)
{
  printf("No Element present in Linked List\n");
}
else
{
  root=temp->link;
  temp->link=NULL;
  printf("First Node of Linked List has been deleted \n");
}
}

void deleteEnd(){
int len=1;
struct node *temp;
temp=root;
if(root==NULL)
{
  printf("No Element present in Linked List \n");
}
else if(length()==1)
{
  root=NULL;
  printf("Last Node has been deleted \n");
}
else
{
  while(len<length()-1)
  {
   temp=temp->link;
   len++;
  }
  temp->link=NULL;
  printf("Last Node has been deleted \n");
}
}

void deletepos()
{
struct node *temp,*p;
int pos,len=1;
temp=root;
printf("Enter the position of Node which do you want to delete \n");
scanf("%d",&pos);
if(pos>length())
{
  printf("Invlide Position \n");
}
else
{
  while(len<pos-1)
  {
   temp=temp->link;
   len++;
  }
  p=temp->link;
  temp->link=p->link;
  p->link=NULL;
  printf("Node of Location %d has been deleted \n",pos); 
}
}

Doubly linked list

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
void insertBegin();
void insertEnd();
void insertLoc();
void deleting();
void display();
int findLength();

struct node *root=NULL;
int main()
{
int choice;
while(true)
{
  printf("Enter your choice to perform opreation on Double Linked List\n");
  printf("Press 1. for Insert Element at begining \n");
  printf("Press 2. for Insert Element at End \n");
  printf("Press 3. for Insert Element at given Location \n");
  printf("Press 4. for Delete element at given position \n");
  printf("Press 5. for Displaying Doubnle linked list \n");
  printf("Press 6. for find lenth \n");
  printf("Press 7. for Exit \n");
  printf("Enter your choice\n");
  scanf("%d",&choice);
 
  switch(choice)
  {
   case 1 : insertBegin();
      break;
   case 2 : insertEnd();
      break;
   case 3 : insertLoc();
      break;  
   case 4 : deleting();
      break;
   case 5 : display();
      break;
   case 6 : printf("Length of Double linked list is : %d\n\n",findLength());
      break;
   case 7 : exit(0);
   default:  printf("Please Enter a valid choice\n");
  }
}
}

void insertBegin()
{
int nodedata;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("Please Enter Data which you want to Insert \n");
scanf("%d",&nodedata);
temp->data=nodedata;
temp->left=NULL;
temp->right=NULL;
if(root==NULL)
{
  root=temp;
}
else
{
  temp->right=root;
  root->left=temp;
  root=temp;
}
  printf("%d inserted into double linked list \n and Linked list is shown as\n",nodedata);
  display();
}

void insertEnd()
{
int nodedata,len=1;
struct node *temp,*p;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter data which you want to insert \n");
scanf("%d",&nodedata);
temp->data=nodedata;
temp->left=NULL;
temp->right=NULL;
p=root;
if(p==NULL)
{
  root=temp;
}
else
{
  while(len<findLength())
  {
   p=p->right;
   len++;
  }
  p->right=temp;
  temp->left=p;
}
  printf("%d inserted into double linked list \n and Linked list is shown as\n",nodedata);
  display();
}

void insertLoc()
{
int loc,len=1,nodedata;
struct node *temp,*p;
temp=root;
printf("Enter Location where do you want to insert node \n");
scanf("%d",&loc);
if(loc>findLength())
{
  printf("Invalid location \n");
}
else
{
  while(len<loc-1)
  {
   temp=temp->right;
   len++;
  }
  p=(struct node *)malloc(sizeof(struct node));
  printf("Enter data which you want to insert int doubly linked list\n");
  scanf("%d",&nodedata);
  p->data=nodedata;
  p->right=temp->right;
  temp->right->left=p;
  temp->right=p;
  p->left=temp;
  printf("%d inserted into double linked list \n and Linked list is shown as\n",nodedata);
  display();
}
printf("\n");
}

void deleting()
{
int loc,len=1;
struct node *temp,*q;
printf("Enter location frome where do you want to delete node \n");
scanf("%d",&loc);
if(loc==1)
{
  printf("%d deleted from list \n",root->data);
  temp=root;
  root=root->right;
  root->left=NULL;
  free(temp);
}
else if(loc>findLength())
{
  printf("Invalide Location \n");
}
else
{
  temp=root;
  while(len<loc-1)
  {
   temp=temp->right;
   len++;
  }
  q=temp->right;
  temp->right=q->right;
  q->right->left=q->left;
  q->left=NULL;
  q->right=NULL;
  free(q);
}
}

void display()
{
struct node *temp;
temp=root;
if(temp==NULL)
{
  printf("List is Empty \n");
}
while(temp!=NULL)
{
  printf("<--%d-->",temp->data);
  temp=temp->right;
}
printf("\n\n");
}

int findLength()
{
int count=0;
struct node *temp;
temp=root;
if(temp==NULL)
{
  printf("List is Empty \n");
}
while(temp!=NULL)
{
  count++;
  temp=temp->right;
}
return count;
}

[  ] Reverse a Linked List
#include<stdio.h>
#include<stdlib.h>
struct Node
{
    int data;
    struct Node* next;
};

struct Node *reverse (struct Node *head, int k)
{
    if (!head)
        return NULL;
    struct Node* current = head;
    struct Node* next = NULL;
    struct Node* prev = NULL;
    int count = 0;

    /*reverse first k nodes of the linked list */
    while (current != NULL && count < k)
    {
        next  = current->next;
        current->next = prev;
        prev = current;
        current = next;
        count++;
    }

    /* next is now a pointer to (k+1)th node
       Recursively call for the list starting from current.
       And make rest of the list as next of first node */
    if (next !=  NULL)
       head->next = reverse(next, k);

    /* prev is new head of the input list */
    return prev;
}

void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node =
            (struct Node*) malloc(sizeof(struct Node));

    /* put in the data  */
    new_node->data  = new_data;

    /* link the old list off the new node */
    new_node->next = (*head_ref);   

    /* move the head to point to the new node */
    (*head_ref)    = new_node;
}

void printList(struct Node *node)
{
    while (node != NULL)
    {     printf("%d  ", node->data);
node = node->next;
    }
}   

/* Driver code*/
int main(void)
{
    struct Node* head = NULL;
     push(&head, 9);
     push(&head, 8);
     push(&head, 7);
     push(&head, 6);
     push(&head, 5);
     push(&head, 4);
     push(&head, 3);
     push(&head, 2);
     push(&head, 1);          

     printf("\nGiven linked list \n");
     printList(head);
     head = reverse(head, 3);
     printf("\nReversed Linked list \n");
     printList(head);
     return(0);
}

[  ] Stack using array

#include<stdio.h>
#include<stdlib.h>
#define SIZE 5

int stack[SIZE],top=-1;
int isFull(void);
void push(int);
void traverse();
void topElement();
int pop();
int isEmpty();
int main()
{
int choice,itm;
while(1)
{
printf("1. Push \n");
printf("2. Pop \n");
printf("3. Traverse \n");
printf("4. Top Element \n");
printf("5. Exit \n");
printf("Enter Your Choice which Operation do you want to perform on stack \n");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("Enter The element which you want to push \n");
scanf("%d",&itm);
push(itm);
break;
case 2 : printf("Deleted item is : %d \n",pop());
break;
case 3 : traverse();
break;
case 4 : topElement();
break;
case 5 : exit(0);
default: printf("Invalid Input \n");
}
}
return 0;
}

void push(int item)
{
if(isFull())
{
printf("Stack is full we can't push Elemet \n");
}
else
{
top++;
stack[top]=item;
printf("%d is inserted(Pushed) into stack \n");
}
}

int isFull()
{
if(top == SIZE-1)
{
return 1;
}
else
{
return 0;
}
}

void traverse()
{
int i;
printf("Element of stack are \n");
for(i=0;i<=top;i++)
{
printf("%d\n",stack[i]);
}
}

void topElement()
{
printf("Top Element of Stack is : %d \n",stack[top]);
}
int pop()
{
if(isEmpty())
{
printf("Stack is Empty \n");
return 0;
}
else
{
return stack[top];
}
}

int isEmpty()
{
if(top==-1)
{
return 1;
}
else
{
return 0;
}
}

[  ] Stack using Linked List

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
void push();
int pop();
void display();
struct node *top=NULL;
int main()
{
while(true)
{
int choice;
printf("Press 1. for PUSH Element into stack \n");
printf("Press 2. for POP Element from stack \n");
printf("Press 3. for Display Element of stack \n");
printf("Press 4. for Exit \n");
printf("Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1 : push();
printf("\n");
break;
case 2 : pop();
printf("\n");
break;
case 3 : display();
printf("\n");
break;
case 4 : exit(0);

default : printf("Invalide Input \n");
}
}
return 0;
}

void push()
{
int item;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter data to push\n");
scanf("%d",&item);
temp->data=item;
temp->link=NULL;

if(top==NULL)
{
top=temp;
printf("Data inserted sucessfully \n");
}
else
{
temp->link=top;
top=temp;
printf("Data inserted sucessfully \n");
}
}

int pop()
{
if(top==NULL)
{
printf("Stack is Empty \n");
return 0;
}
int item;
struct node *temp;
temp=top;
item=temp->data;
top=temp->link;
temp->link=NULL;
printf("%d poped from stack \n",item);
return item;
}

void display()
{
struct node *temp;
temp=top;

if(top==NULL)
{
printf("Stack is Empty \n");
}
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->link;
}
printf("\n");
}

[  ] Queue (array)

#include<stdio.h>
#include<stdlib.h>
#define SIZE 5

void inserting();
void deleting();
void display();

int frant=0;
int rear=0;
int queue[SIZE];

int main()
{
int choice;
while(true)
{
printf("Press 1. for Inserting Element \n");
printf("Press 2. for Deleting Element \n");
printf("Press 3. for Displaying Element \n");
printf("Press 4. for Exit \n");
printf("Enter your choice \n");
scanf("%d",&choice);

switch(choice)
{
case 1 : inserting();
printf("\n");
break;
case 2 : deleting();
printf("\n");
break;
case 3 : display();
printf("\n");
break;
case 4 : exit(0);

default : printf("Invalide Input \n");
}
}
return 0;
}

void inserting()
{
int item;
if(rear==SIZE)
{
printf("Queue is full we can't insert data into queue \n");
}
else
{
printf("Enter data which do you want to insert into queue \n");
scanf("%d",&item);
queue[rear]=item;
printf("%d inserted successfully \n",item);
rear++;
}
}

void deleting()
{
int temp;
if(frant==rear)
{
printf("Queue is Empty \n");
}
else
{
printf("%d deleted from Queue \n",queue[frant]);
temp=frant;
while(temp<rear)
{
queue[temp]=queue[temp+1];
temp++;
}
rear--;
}
}

void display()
{
int temp;
temp=frant;
printf("Data present in queue is : \n");
while(temp!=rear)
{
printf("%d \t",queue[temp]);
temp++;
}
}


[  ] Queue (linked list)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};

struct node *front=NULL,*reer=NULL;
void insertion();
void deletion();
void display();
int main()
{
int choice;
while(1)
{
  printf("Press 1. for Insertion \n");
  printf("Press 2. for Deletion \n");
  printf("Press 3. for Display \n");
  printf("Press 4. for Exit \n");
 
  printf("Enter your choice \n");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1 : insertion();
      printf("\n");
      break;
   case 2 : deletion();
      printf("\n");
      break;
   case 3 : display();
      printf("\n");
      break;
   case 4 : exit(0);
 
   default : printf("Invalide Input \n");
  }
}
return 0;
}

void insertion()
{
int value;
printf("Enter value which you want to Insert \n");
scanf("%d",&value);
struct node *temp;
temp=(struct node *)malloc(sizeof(node));
temp->data = value;
temp->link = NULL;

if(front==NULL)
{
  front = temp;
  reer = temp;
}
else
{
  reer->link = temp;
  reer = reer->link;
}
printf("Value inserted successfully \n");
}

void deletion()
{
if(front==NULL)
{
  printf("Queue is Empty \n");
}
else
{
  printf("Deleted item is : %d \n",front->data );
  struct node *temp = front;
  front=front->link;
  temp->link = NULL;
  free(temp);
}
}


void display()
{
if(front==NULL)
{
  printf("Queue is Empty \n");
}
else
{
  struct node *temp;
  temp=front;
  printf("The Queue is : ");
  while(temp!=NULL)
  {
   printf("%d \t",temp->data );
   temp=temp->link;
  }
}
printf("\n");
}












Click on the above button. 


Post a Comment

If you have furthur QUERIES please let us know

Previous Post Next Post