Linked list creation, deletion & insertion.

At Beginning, End and middle.

#include<bits/stdc++.h>
 using namespace std; 
 struct node
 {
     int data;
     struct node * next;
 };
 struct node * head, * newnode, *temp;
  int main()
  {
      int choice;
      head = 0;
      cout<<"do you want to enter data in list(0,1): ";
      cin>>choice;
      while(choice==1)
      {
      newnode=(struct node*)malloc(sizeof(struct node));
      newnode->next=0;
      cout<<" enter data in list";
      cin>>newnode->data;
      if(head==0)
      {
          head = newnode;
          temp =head;
      }
      else
      {
          temp->next=newnode;
          temp=newnode;
      }
      cout<<"want another element to be inserted(0,1): ";
      cin>>choice;
      }
      
      temp=head;
      cout<<"printing the list\n";
      while(temp!=0)
      {
          cout<<temp->data<<" ";
          temp=temp->next;
      }
      

// INSERTION AT BEGINNING

      cout<<"\nDo you want to insert an element in begining(0,1): ";
      cin>>choice;
//You can also use if statement if you want to insert only one node, and don't ask user again whether to insert node again or not.
      while(choice==1)
      {
          newnode=(struct node*)malloc(sizeof(struct node));
          cout<<"enter data: ";
          cin>>newnode->data;
          // forming links between newnode, head and previous first node;
          newnode->next=head;
          head = newnode;
          
        //Printing the NEW list;
          temp=head;
          cout<<"printing the list\n";
          while(temp!=0)
          {
          cout<<temp->data<<" ";
          temp=temp->next;
          }
          cout<<"\nWant another element to be inserted at beginning (0,1): ";
          cin>>choice;
      }
    

  // INSERTION AT END:

      cout<<"\nDo you want to insert an element at end(0,1): ";
      cin>>choice;
      while(choice==1)
      {
          newnode=(struct node*)malloc(sizeof(struct node));
          newnode->next=0;
          cout<<"enter data: ";
          cin>>newnode->data;
          // forming links between newnode, and last node;
          temp = head;
           while(temp->next!=0) // with this temp will point to last node
           {
               temp=temp->next;
           }
          temp->next=newnode;  // stored address of newnode in last node
          
          //Printing new list
          temp=head;
          cout<<"\nprinting the list\n";
          while(temp!=0)
          {
          cout<<temp->data<<" ";
          temp=temp->next;
          } 
          cout<<"\nWant another element to be inserted at end (0,1): ";
          cin>>choice;
      }

// INSERTION IN MIDDLE

      int i,pos;
      cout<<"\nDo you want to insert element in middle(0,1): ";
      cin>>choice;
      while(choice==1)
      {
          cout<<"\nenter the position of insertion graeter than 1 (0,1): ";
          cin>>pos;
          newnode=(struct node*)malloc(sizeof(struct node));
          cout<<"enter data";
          cin>>newnode->data;
          
          temp=head; i=1;
          while(i<pos-1)
          {
              temp=temp->next;
              i++;
          }
          newnode->next=temp->next;
          temp->next=newnode;
          
          //printing new list
          temp=head;
          cout<<"\nprinting the list\n";
          while(temp!=0)
          {
          cout<<temp->data<<" ";
          temp=temp->next;
          }
          cout<<"\nDo you again want to insert element in middle(0,1): ";
          cin>>choice;
      }

}








Complete linked list, creation, insertion, deletion.

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};

int count=0,f=1;
struct node * head, *temp, *newnode, *stemp;

void create()
{   head=0;
    int choice =1;
    while(choice==1)
    {
        count++;
       newnode=(struct node*)malloc(sizeof(struct node));
       newnode->next=0;
       printf("enter data");
       scanf("%d",&newnode->data);
       if(head==0)
       {
           head=newnode;
           temp=newnode;
       }
       else
        {
            temp->next=newnode;
            temp=newnode;
        }
        printf("Want another node: ");
        scanf("%d",&choice);
    }
}
void insertpos()
{
    int choice=1,pos,i;
    while(choice==1)
    {
        printf("enter the position of insertion");
        scanf("%d",&pos);
        if(pos==1)
        {
            count++;
            newnode=(struct node*)malloc(sizeof(struct node));
            printf("enter data");
            scanf("%d",&newnode->data);
            //forming link
            newnode->next=head;
            head=newnode;
        }
        else if(pos==count-1)
        {
            count++;
            newnode=(struct node*)malloc(sizeof(struct node));
            printf("enter data");
            scanf("%d",&newnode->data);
            newnode->next=0;
            //forming link
            temp=head;
            while(temp->next!=0)
                temp=temp->next;
            temp->next=newnode;
        }
        else
        {
            count++;
            newnode=(struct node*)malloc(sizeof(struct node));
            printf("enter data");
            scanf("%d",&newnode->data);
            //forming link
            temp=head;
            i=1;
            while(i<pos-1)
            {
                temp=temp->next;
                i++;
            }
            newnode->next=temp->next;
            temp->next=newnode;
        }
        printf("\nDo you want another insertion (0,1): ");
        scanf("%d",&choice);
    }
}
void delpos()
{
    int choice,pos,i;
    printf("enter the position of node you want to delete: ");
    scanf("%d",&pos);
    if(pos==1)
    {
        count--;
        if(head!=0){
        temp = head;
        head=temp->next;
        free(temp);}
        if(head==0)
            f=1; //indicates that the list is empty;
        //so first create list;
    }
    else if(pos==count)
    {
        count--;
        temp = head;
        while(temp->next!=0)
        {
            stemp=temp;
            temp=temp->next;
        }
        //temp is pointing to the last node;
        //stemp is pointing to the last second node;
        {stemp->next=0;
        free(temp);}
    }
    else
    {
        count--;
        i=1;
        while(i<pos-1)
        {
            temp=temp->next;
            i++;
        }
        stemp=temp->next;
        //stemp is pointing to the node which is being deleted;
        //temp is pointing to that node, which is just before the node being deleted;
        temp->next=stemp->next;
        free(stemp);
    }
}
void listprint()
{
    temp = head;
    printf("size is: %d\n",count);
           while(temp!=0)
            {
              printf("%d ",temp->data);
              temp=temp->next;
            }
}
void main()
{
    int choice,conti=1;
   
    while(conti==1)
    {
        printf("Press 1. to create list");
        printf("\nPress 2. to insert in list");
        printf("\nPress 3. to delete in list");
        printf("\nPress 4. to print list");
        printf("\nEnter your choice:  ");
        scanf("%d",&choice);
        if(choice==1)
        {
            if(f==1)
            {create();f=0;}
            else
            printf("list already created");
        }
        else if(f==1)
            printf("list is empty, create list first");
        else
        {
            if(choice==2)
                insertpos();
            else if(choice==3)
                delpos();
            else if(choice==4)
                listprint();
            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