Array LL

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

/* Data structure Program for implementation and operation on linked list */

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int info;
}
void create(int data)
{
struct node *q,*temp;
temp=malloc(sizeof(struct node));
temp->info=data;
temp->link=NULL;
if(start==NULL)
start=temp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=temp;
}
}
void display()
{
struct node *temp;
temp=start;
if(start==NULL)
printf("\nSorry..List Is Empty");
else
{
while(temp!=NULL)
{
printf("\n%d\t",temp->info);
temp=temp->link;
}
}
}
void insertafter(int data,int pos, int n)
{
int i;
struct node *q,*temp;
q=start;
if((q->link!=NULL)&&(pos<n))
{
temp=malloc(sizeof(struct node));
temp->info=data;
for(i=0;i<pos;i++)
q=q->link;
temp->link=q->link;
q->link=temp;
}
else
{
printf("\n Sorry..The Position Is Invalid....");
}
}
void insertbefore(int data,int pos, int n)
{
int i;
struct node *q,*temp;
q=start;
if((q->link!=NULL)&&(pos<n))
{
temp=malloc(sizeof(struct node));
temp->info=data;
for(i=1;i<pos;i++)
q=q->link;
temp->link=q->link;
q->link=temp;
}
else
{
printf("\n Sorry..The Position Is Invalid....");
}
}
void delele(int num)
{
int i;
struct node *temp,*q;
q=start;
while(q->link!=NULL);
if(q->info==num)
{
temp=q->link;
q->link=temp;
q=q->link;
free(temp);
printf("\n%d Is Deleted..",num);
}
else
printf("\n %d Is Not Present In The List !!! /n",num);
}
void delpos(int pos)
{
int i;
struct node *q,*temp;
q=start;
for(i=1;i<pos-1;i++)
q=q->link;
temp=q->link;
q->link=temp->link;
free(temp);
printf("\n %d Is Deleted...",pos);
}
void search(int data)
{
int i=0,flag=1;
struct node *temp;
temp=start;
while(temp!=NULL)
{
i++;
if(temp->info==data)
{
printf("\n%d Is Found At %d Position",data,i);
flag=0;
break;
}
temp=temp->link;
}
if(flag==1)
printf("\n%d Is Not Found",data);
}
void searchpos(int pos,int n)
{
int i;
struct node *temp;
temp=start;
if(pos>n)
printf("\nYou Enter Wrong Position");
else
{
for(i=1;i<pos;i++)
temp=temp->link;
printf("\n%d Is Found At %d Position",temp->info,i);
}
}
void menu()
{
int ch,n,num,i,pos;
printf("\n\n\n MAIN MENU\n");
printf("\n *********");
printf("\n\n1:Create List");
printf("\n2:Display");
printf("\n3:Insertion Element By After The Position");
printf("\n4:Insertion Element By Before The Position");
printf("\n5:Delete Element");
printf("\n6:Delete Element By Position");
printf("\n7:Searching The Element");
printf("\n8:Searching The Element By Position");
printf("\n9:Exit");
printf("\nEnter Your Wish:- \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nHow Many Node You Want To Enter:- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter The %d Element:- ",i+1);
scanf("%d",&num);
create(num);
}
menu();
break;
case 2:
display();
menu();
break;
case 3:
printf("\nEnter The Element You Want To Insert:- ");
scanf("%d",&num);
printf("\nEnter The Position After You Want To Insert:- ");
scanf("%d",&pos);
insertafter(num,pos,n);
menu();
break;
case 4:
printf("\nEnter The Element You Want To Insert:- ");
scanf("%d",&num);
printf("\nEnter The Position Before You Want To Insert:- ");
scanf("%d",&pos);
insertbefore(num,pos,n);
menu();
break;
case 5:
printf("\n Enter The Element:");
scanf("%d",&num);
delele(num);
menu();
break;
case 6:
printf("\nEnter The Position:- ");
scanf("%d",&pos);
delpos(pos);
menu();
break;
case 7:
printf("\nEnter The Element You Want To Search:- ");
scanf("%d",&num);
search(num);
menu();
break;
case 8:
printf("\nEnter The Position For Searching The Element:- ");
scanf("%d",&pos);
searchpos(pos,n);
menu();
break;
case 9:
exit(0);
default:
printf("\nOops..!Your Option Is Wrong ..Please Try Again.");
menu();
}
}
void main()
{
clrscr();
start=NULL;
menu();
getch();
}

You might also like