Doubly linked list, with Separate functions for each operation 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(int a)
  {
      static int count ;
      count +=a;
      return count;
  }
  void create()
  {
      int choice=1;
      while(choice==1){
      newnode = (struct node*)malloc(sizeof(struct node));
      count(1);
      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(1);
      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(1);
      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,x;
      while(choice==1)
      {
          x=count(0);
         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<x)
         {
            count(1);
            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 listprint()
  {
      temp=head;
      printf("size is: %d\n",count(0));

      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("\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
      printf("invalid choice");


      printf("\nDo you want to continue(0,1): ");
      scanf("%d",&conti);
      }
  }

Comments

  1. You can directly use count variable by declaring it globally, and it can be accessed directly with out using scope resolution(::) operator.
    you can directly perform count++ operations.
    note this is possible, if no other variable with same name is declared.

    ReplyDelete

Post a Comment

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