Saturday, October 1, 2016

Operations On Sorted Linklist :)

Tags

//Program To Perform Operations On A Sorted Linklist

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>  //for malloc function
struct node
{
    int data;
    struct node *next;

}*head;
void insert(int data) //for insertion
{ struct node *temp=(struct node *)malloc(sizeof(struct node));
struct node *save=(struct node *)malloc(sizeof(struct node));
struct node *ptr=(struct node *)malloc(sizeof(struct node));
temp->data=data;
if(head==NULL) //if it is first element
{
    head=temp;
    temp->next=NULL;
}
if(temp->data<head->data)//if value less than first element
{
    temp->next=head;
    head=temp;
 }
else{

ptr=head;
while(ptr!=NULL)
{
    if(temp->data<ptr->data)
    {
        break;
    }
    else
    {   save=ptr;
        ptr=ptr->next;
    }
}
save->next=temp;
temp->next=ptr;
}
}
void print() //for display
{
struct node *temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
printf("\n");
}
}
 void delete(int data)
 {
  struct node *ptr=(struct node*)malloc(sizeof(struct node));
struct node *save=(struct node*)malloc(sizeof(struct node));
save=head;
ptr=head->next;
if(save->data==data)
{
 head=head->next;
 return;
 } 
 else{
  while(ptr!=NULL)
  {
   if(ptr->data==data)
   {
    save->next=ptr->next;
    return;
   }
   else
   {
    save=ptr;
    ptr=ptr->next;
   }}
   printf("ELEMENT NOT FOUND");
  
 }
 }
main()
{
int choice,num;
    head=NULL; //initially head is null
    while(1)  //for user interface
    { printf("\nOperations On Linklist\n");
         printf("Enter your choice\n");
              printf("1. INSERT \n");
              printf("2. DELETE \n");
              printf("3. DISPLAY \n");
              printf("4. EXIT \n");
              scanf("%d",&choice);
              switch(choice)
              {
                   case 1: //If user wishes to insert element in the queue
                   printf("Enter number to be inserted in the queue: ");
                   scanf("%d",&num); //getting the element to be inserted from the user
                   insert(num); //calling insert function
                   break; //jumping out of switch statement
                   case 2:
                   printf("Enter Element To Delete");
                   scanf("%d",&num);//If user wishes to delete element from the queue
                        delete(num); //calling delete function and saving the result in an int variable(num)
                        break;
                        case 3://If user wishes to display the queue
                             print(); //calling display function
                             break;
                             case 4: //If user wishes to exit(stop continuing the program)
                                  exit(1); //end of while loop
                                  default ://If user enters a number other than 1 to 4
                                          printf("INVALID CHOICE \n");
                                          }
                                          }
                                          }

Output: