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

linkedlist

Uploaded by

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

linkedlist

Uploaded by

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

Assignment 2

Name Anushka Shivade


U no UEC2023156
Problem

Program:
#include<stdlib.h>
#include<stdio.h>

struct node{
int data;
struct node *next;
};
struct node *head, *curr ,*p;

void create(){

curr =(struct node*)malloc(sizeof(struct node));

char c;
head=NULL;

do{

printf("Enter the data: ");


scanf("%d", &curr->data);

if(head==NULL) head=curr;

printf("Do you want to enter more data :");


scanf(" %c" ,&c);

if(c=='y'||c=='Y'){

curr->next=(struct node*)malloc(sizeof(struct node));


curr=curr->next;
}

}while(c=='y'||c=='Y');
//printf("%p", head);

curr->next=NULL;
}
void display(struct node* mover){

if(mover==NULL) printf("Empty Linked list\n");


else{

while(mover!=NULL){

printf("%d\t" ,mover->data);
mover=mover->next;
}
printf("\n");
}
}
void search(int num , struct node *mover){

int found=0 ,poss=0,i=0;

while(found==0 && mover!=NULL){


if(mover->data==num) found=1;
else {
mover=mover->next;
poss++;
}

if(found==1) printf("Found at %d\n",poss);


else printf("NOt found\n");

}
void insert(struct node* mover,int c ){
int pos ,count=0 ,num;

do{
switch(c){

case 1:p->next=head;
head=p;
display(head);
break;
case 2:mover=head;
while(mover->next!=NULL){
mover=mover->next;
}
mover->next=p;
display(head);
break;
case 3:printf("Enter the index where to insert: ");
scanf("%d", &pos);
mover=head;
while(mover!=NULL){

count++;
mover=mover->next;
if(count==pos-1) {
p->next=mover->next;
mover->next=p;
break;
}

}
display(head);
break;

case 4: printf("Enter the number where to insert: ");


scanf("%d", &num);
mover=head;
while(mover->next->data==num){

mover=mover->next;

}
p->next=mover->next;
mover->next=p;

display(head);
break;

default : exit(0);

}while(c!=5);
free(p);

}
void delete(struct node* mover,int d){

}
int main(){

int c,num,choice;
create();

do{
printf("\n***********MENU************\n");
printf("1.Display\n2.search\n3.delete\n4.Insert\n5.Exit\n");
scanf("%d",&choice);

switch(choice){
case 1:display(head);
break;

case 2:
printf("Enter no to search: ");
scanf("%d", &num);

search(num ,head);
break;

case 3: p =(struct node*)malloc(sizeof(struct node));


printf("\nEnter the data to delete: ");
scanf("%d", &p->data);
p->next=NULL;
break;

case 4:
printf("INSERTION\n1.First\n2.last\n3.Anywhere\n4.Using number\n");
printf("Enter the choice : ");
scanf("%d",&c);
insert( head , c );
break;

case 5:exit(0);

default : printf("Enter valid choiice !!!!\n");

}
}while(c!=5);

return 0;
}

Output:
PS D:\Anushka\college\DSA> c

> cd "d:\Anushka\college\DSA\" ; if ($?) { gcc linkedlist.c -o linkedlist } ; if ($?) {


.\linkedlist }
Enter the data: 65
Do you want to enter more data :y
Enter the data: 55
Do you want to enter more data :y
Enter the data: 8
Do you want to enter more data :y
Enter the data: 56
Do you want to enter more data :y
Enter the data: 98
Do you want to enter more data :y
Enter the data: 21
Do you want to enter more data :y
Enter the data: 66
Do you want to enter more data :n
***********MENU************
1.Display
2.search
3.delete
4.Insert
5.Exit
1
65 55 8 56 98 21 66

***********MENU************
1.Display
2.search
3.delete
4.Insert
5.Exit
2
Enter no to search: 56
Found at 3

***********MENU************
1.Display
2.search
3.delete
4.Insert
5.Exit
3

Enter the data to delete: 8

***********MENU************
1.Display
2.search
3.delete
4.Insert
5.Exit
5

You might also like