Programmer

Saturday 14 April 2018

Linked List In C Programming




#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
}*first=NULL,*curr,*prev,*new1;

void insert()
{
int val;
clrscr();
printf("Enter Value :");
scanf("%d",&val);
new1=(struct node*)malloc(sizeof(struct node));
new1->data=val;
new1->next=NULL;

if(first==NULL)
{
first=new1;
return;
}
curr=first;
while(curr->next!=NULL)
{
curr=curr->next;
}
curr->next=new1;
}

void display()
{
if(first==NULL)
printf("Linked list is empty");
curr=first;
while(curr!=NULL)
{
printf("%d-->",curr->data);
curr=curr->next;
}
}

void del()
{
int val;
clrscr();
printf("Which Value U want to Delete ?:");
scanf("%d",&val);

if(first==NULL)
{
printf("Linked list is empty");
}
curr=first;
if(first->data==val)
{
first=first->next;
free(curr);
return;
}

while(curr->data!=val && curr!=NULL)
{
prev=curr;
curr=curr->next;
}
if(curr==NULL)
printf("%d value is not in Linked List",val);

prev->next=curr->next;
free(curr);
}

void after()
{
int val,key;
clrscr();
printf("Enter key :");
scanf("%d",&key);
printf("Enter Value :");
scanf("%d",&val);
new1=(struct node *)malloc(sizeof(struct node));
new1->data=val;
new1->next=NULL;
curr=first;
while(curr->data!=key && curr!=NULL)
{
curr=curr->next;
}
if(curr==NULL)
{
printf("%d key is not in linked list",key);
}
new1->next=curr->next;
curr->next=new1;
}

void before()
{
int val,key;
printf("Enter key :");
scanf("%d",&key);
printf("Enter Val :");
scanf("%d",&val);
new1=(struct node *)malloc(sizeof(struct node));
new1->data=val;
new1->next=NULL;

curr=first;
if(first->data==key)
{
new1->next=first;
first=new1;
return;
}

curr=first;
while(curr->data!=key && curr!=NULL)
{
prev=curr;
curr=curr->next;
}
prev->next=new1;
new1->next=curr;
}

void sort()
{
struct node *i,*j;
int tmp;

if(first==NULL || first->data==NULL)
{
return;
}
i=first;
while(i->next!=NULL)
{
j=i->next;
while(j!=NULL)
{
if(j->data < i->data)
{
tmp=i->data;
i->data=j->data;
j->data=tmp;
}
j=j->next;
}
i=i->next;
}
getch();
}
struct node *rev(struct node *t)
{
struct node *tmp;
if(t->next==NULL)
{
first=t;
return t;
}
tmp=rev(t->next);
tmp->next=t;
return t;
}
void main()
{
int ch;
do
{
clrscr();
printf("***** MENU *****\n");
printf("0. Exit \n");
printf("1. Insert \n");
printf("2. Display \n");
printf("3. Delete \n");
printf("4. Insert After \n");
printf("5. Insert Before\n");
printf("6. Sort \n");
printf("7. Reverse \n");
printf("****************\n");
printf("Enter Your choice :");
scanf("%d",&ch);

switch(ch)
{
case 0: exit(0); break;
case 1: insert(); break;
case 2: display(); break;
case 3: del(); break;
case 4: after(); break;
case 5: before(); break;
case 6: sort(); break;
case 7:
struct node *x;
x=rev(first);
x->next=NULL;
break;
default :
printf("Wrong Input ");
}
getch();
}while(ch>0);
}

No comments:

Post a Comment

Program To Print Particular Line From File.

#include<fstream.h> #include<conio.h> #include<stdlib.h> void main() { ifstream fin; int count=0,num,l=1; char ...