Complete Doubly linked list using C

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
  struct node
  {
      int data;
       struct node *next, *prev;
  };
  struct node *head, *tail, *newnode, *temp;

  int count =0;
  void create()
  {
      int choice=1;
      while(choice==1){
      newnode = (struct node*)malloc(sizeof(struct node));
      count++;
      newnode->next=0;
      newnode->prev=0;
      printf("\nenter data: ");
      scanf("%d",&newnode->data);
      if(head==0)
      {
          head=newnode;
          tail=newnode;
      }
      else
      {
          newnode->prev=tail;
          tail->next=newnode;
          tail=newnode;
      }
      printf("Want another element to be inserted(0,1): ");
      scanf("%d",&choice);
    }
  }
  void insertbeg()
  {
      int choice = 1;
      while(choice==1){
      newnode=(struct node*)malloc(sizeof(struct node));
      count++;
      newnode->next=0;
      newnode->prev=0;
      printf("enter data: ");
      scanf("%d",&newnode->data);
      //first make the link to the previously existing first node;
      head->prev=newnode;
      newnode->next=head;
      head=newnode;
      printf("Want another element to be inserted in beginning(0,1): ");
      scanf("%d",&choice);
      }
  }
  void insertend()
  {   int choice =1;
      while(choice==1){
      newnode=(struct node*)malloc(sizeof(struct node));
      count++;
      newnode->next=0;
      newnode->prev=0;
      printf("enter data: ");
      scanf("%d",&newnode->data);
      tail->next=newnode;
      newnode->prev=tail;
      tail=newnode;
      printf("Want another element to be inserted at end(0,1): ");
      scanf("%d",&choice);
      }
  }
  void insertpos()
  {
      int choice = 1,pos,i;
      while(choice==1)
      {
         printf("enter the position of insertion greater than 1 and less than the length of list(0,1): ");
         scanf("%d",&pos);
         if(pos>1 && pos<count)
         {
            count++;
            newnode=(struct node*)malloc(sizeof(struct node));
            newnode->next=0;
            newnode->prev=0;
            printf("enter data: ");
            scanf("%d",&newnode->data);
            // Forming link;
            temp=head; i=1;
            while(i<pos-1)
            {
                temp=temp->next;
                i++;
            }
            newnode->prev=temp;
            newnode->next=temp->next;
            temp->next->prev=newnode;
            temp->next=newnode;
            printf("Do you want another element to be inserted in middle 0,1: ");
            scanf("%d",&choice);
         }
         else
         {
             printf("\nPosition cannot be <=1 or >=size of list");
             printf("\n");
             printf("Do you want to insert again(0,1): ");
             scanf("%d",&choice);
         }
      }
  }

  void del()
  {
      int choice=1,pos,i;
      while(choice==1)
      {
          printf("enter the position to delete");
          scanf("%d",&pos);
          if(pos==1)
          {
              temp = head;
              head=temp->next;
              free(temp);
              count--;
          }
          else if(pos==count)
          {
              temp=tail;
              tail=temp->prev;
              free(temp);
              tail->next=0;
              count--;
          }
          else
          {
              i=1; count--;
              temp=head;
              while(i<pos)
                {temp=temp->next;i++;}
              temp->prev->next=temp->next;
              temp->next->prev=temp->prev;
              free(temp);
          }
          printf("Do you want another node to be deleted(0,1): ");
          scanf("%d",&choice);
      }
  }

  void listprint()
  {
      temp=head;
      printf("size is: %d\n",count);
      while(temp!=0)
      {
          printf("%d ",temp->data);
          temp=temp->next;
      }
  }
  void main()
  {
      int f=1, conti=1, choice;
      while(conti==1)
      {
      printf("\npress 1. to create list");
      printf("\npress 2. to enter node at beginning");
      printf("\npress 3. to enter node at end");
      printf("\npress 4. to enter node at certain position");
      printf("\npress 5. to print list");
      printf("\npress 6. to delete node");
      printf("\nenter your choice from given list: ");
      scanf("%d",&choice);
      if(choice==1)
      {   if(f==1)
          {create();
          f=0;}
          else
          printf("list already created");
      }
      else if(choice==2)
      insertbeg();
      else if(choice==3)
      insertend();
      else if(choice==4)
      insertpos();
      else if(choice==5)
      listprint();
      else if(choice==6)
      del();
      else
      printf("invalid choice");
      printf("\nDo you want to continue(0,1): ");
      scanf("%d",&conti);
      }
  }

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree