0% found this document useful (0 votes)
8 views

Singly

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Singly

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

#include<stdio.

h>
#include<stdlib.h>

typedef struct nodetype


{
int info;
struct nodetype *next;
}node;

void insertleft(node**,node**);
void insertright(node**,node**);
void insertkey(node**,node**);
void insertpos(node**,node**);
void deletekey(node**,node**);
void deletepos(node**,node**);
void display(node*);
void reversedisplay(node*);
void odd_info(node*);
void even_info(node*);
void odd_pos(node*);
void even_pos(node*);
void max_element(node *);

int main()
{
int ch;
node *head=NULL,*tail=NULL;
do
{
printf("enter::\n1 to insert in right\n2 to insert in left\n3 to
insert by position\n4 to insert after a key\n5 to delete by key\n6 to
delete by position\n7 to display\n8 to reverse display\n9 to odd info
print\n10 to even position elements print\n11 to even info print\n12 to
odd position elements print\n13 to max_element\n:::");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertright(&head,&tail);
break;

case 2:
insertleft(&head,&tail);
break;

case 3:
insertpos(&head,&tail);
break;

case 4:
insertkey(&head,&tail);
break;

case 5:
deletekey(&head,&tail);
break;

case 6:
deletepos(&head,&tail);
break;

case 7:
display(head);
break;

case 8:
reversedisplay(head);
break;

case 9:
odd_info(head);
break;

case 10:
even_pos(head);
break;

case 11:
even_info(head);
break;

case 12:
odd_pos(head);
break;

case 13:
max_element(head);
break;
default:
printf("wrong choice\n");
}
printf("enter 1 to continue:");
scanf("%d",&ch);
}while(ch==1);

return 0;
}

void insertright(node **head,node **tail)


{
node *temp=NULL;
int x;
temp=(node*)malloc(sizeof(node));
if(temp==NULL)
{
printf("memory not allocated\n");
return;
}
else{
printf("enter the element=");
scanf("%d",&x);
temp->info=x;
if(*head==NULL)
{
temp->next=NULL;
*head=*tail=temp;
}
else{
(*tail)->next=temp;
temp->next=NULL;
*tail=temp;
}
}
}

void insertleft(node **head,node **tail)


{
node *temp=NULL;
int x;
temp=(node*)malloc(sizeof(node));
if(temp==NULL)
{
printf("memory not allocated\n");
return;
}
else
{
printf("enter the element=");
scanf("%d",&x);
temp->info=x;
if(*head==NULL)
{
temp->next=NULL;
*head=*tail=temp;
}
else
{
temp->next=*head;
*head=temp;
}
}
}

void insertkey(node **head,node **tail)


{
node *temp=NULL,*new=NULL;
int key,x;
if(*head==NULL)
{
printf("linked list is empty\n");
}
else{
printf("enter the key:");
scanf("%d",&key);
temp=*head;
while(temp->info!=key&&temp->next!=NULL)
{
temp=temp->next;
}
if(temp->next==NULL&&temp->info!=key)
{
printf("key not found\n");
return;
}
else{
printf("key is founded\n");
printf("enter the element to insert");
scanf("%d",&x);
new=(node*)malloc(sizeof(node));
if(new==NULL)
{
printf("memory not allocated\n");
return;
}
else{
new->info=x;
if(temp->info==key&&temp->next==NULL)
{
(*tail)->next=new;
*tail=new;
(*tail)->next=NULL;
}
else if(temp->info==key&&temp->next!=NULL)
{
new->next=temp->next;
temp->next=new;
}
}
}
}
}

void insertpos(node **head,node **tail)


{
int pos,x,c=0;
node *tp=NULL,*tc=NULL,*new=NULL;
printf("enter the posotion:");
scanf("%d",&pos);
tc=*head;
while(tc!=NULL)
{
c++;
tc=tc->next;
}
int t=c;
if(pos<=0||pos>c+1)
{
printf("position is invalid\n");
return;
}
else{
printf("enter the element:");
scanf("%d",&x);
new=(node*)malloc(sizeof(node*));
if(new==NULL)
{
printf("memory not allocated\n");
return;
}
else
{
new->info=x;
if(pos==1&&*head!=NULL)
{
new->next=*head;
*head=new;
}
else if(pos==1&&*head==NULL)
{
new->next=NULL;
*head=*tail=new;
}
else if(pos==t)
{
*tail=new;
(*tail)->next=NULL;
}
else{
c=1;
tp=*head;
tc=(*head)->next;
while(tc!=NULL)
{
c++;
if(c==pos)
{
break;
}
tp=tc;
tc=tc->next;
}
new->next=tc;
tp->next=new;

}
}
}
}

void deletekey(node **head,node **tail)


{
int key;
node *tc=NULL,*tp=NULL;
if(*head==NULL)
{
printf("linked list is empty\n");
return;
}
printf("enter the key:");
scanf("%d",&key);
tc=*head;
while(tc->info!=key&&tc->next!=NULL)
{
tp=tc;
tc=tc->next;
}
if(tc->info!=key&&tc->next==NULL)
{
printf("key not founded\n");
return;
}
else{
printf("key is founded\n");
if(tc==*head)
{
*head=(*head)->next;
free(tc);
}

else if(tc==*tail)
{
*tail=tp;
tp->next=NULL;
free(tc);
}

else
{
tp->next=tc->next;
free(tc);
}
}
}

void deletepos(node **head,node **tail)


{
int pos,c=0;
node *tc=NULL,*tp=NULL;
printf("enter the position");
scanf("%d",&pos);
tc=*head;
if(*head==NULL)
{
printf("linked list is empty\n");
return;
}
while(tc!=NULL)
{
c++;
tc=tc->next;
}
if(pos<=0||pos>c)
{
printf("invalid poistion\n");
return;
}
else{
if(pos==1)
{
node *temp=*head;
*head=(*head)->next;
free(temp);

}
else if(pos==c)
{
tc=(*head)->next;
tp=tc;
while(tc->next!=NULL)
{
tp=tc;
tc=tc->next;
}
*tail=tp;
(*tail)->next=NULL;
free(tc);
}
else if(pos>1&&pos<c){
int h=1;
tc=(*head)->next;
tp=tc;
while(tc!=NULL)
{
h++;
if(pos==h)
{
break;
}
tp=tc;
tc=tc->next;
}
tp->next=tc->next;
free(tc);
}
}
}

void display(node *head)


{
while(head!=NULL)
{
printf("%d",head->info);
head=head->next;
}
}

void reversedisplay(node *head)


{
if(head!=NULL)
{
reversedisplay(head->next);
printf("%d",head->info);
}
}

void odd_info(node *he)


{
int t;
while(he!=NULL)
{
t=he->info;
if(t%2!=0)
{
printf("%d\t",he->info);
}
he=he->next;
}
}

void even_info(node *he)


{
int t;
while(he!=NULL)
{
t=he->info;
if(t%2==0)
{
printf("%d\t",he->info);
}
he=he->next;
}
}

void odd_pos(node *he)


{
int i=0;
while(he!=NULL)
{
i++;
if(i%2!=0)
{
printf("%d\t",he->info);
}
he=he->next;
}
}

void even_pos(node *he)


{
int i=0;
while(he!=NULL)
{
i++;
if(i%2==0)
{
printf("%d\t",he->info);
}
he=he->next;
}
}

void max_element(node *he)


{
node *temp=he;
int l;
l=he->info;
if(he==NULL)
{
printf("empty linked list\n");
}
else{
while(temp!=NULL)
{
if(l<(temp->info))
{
l=temp->info;
}
temp=temp->next;
}

printf("%d is the largest element\n",l);


}
}

You might also like