Lab 4
Lab 4
1. Write a program to create a linked list of integers entered by user and perform the following:
a. Traverse the linked list and print all the elements from first to last.
b. Insert a new node at the start of the linked list. Print the new linked list.
c. Insert a new node at the end of the linked list. Print the new linked list.
d. Insert a new node after a node having a key value. Print the new linked list.
CODE:
#include<stdio.h>
#include<stdlib.h>
struct list{
int data;
struct list* link;
};
//b. add node at the beginning
struct list* add_at_beg(struct list** head,int data){
struct list* ptr=malloc(sizeof(struct list));
ptr->data=data;
ptr->link=*head;
return ptr;
}
//c. add node at the end
struct list* add_at_end(struct list* ptr,int data){
struct list* temp=malloc(sizeof(struct list));
temp->data=data;
temp->link=NULL;
if(ptr==NULL) {
return temp;
}
struct list* ptr2=ptr;
while(ptr2->link){
ptr2=ptr2->link;
}
ptr2->link=temp;
return ptr;
}
OUTPUT:
//d. add node at a position
void add_at_pos(struct list* head,int data, int pos){
struct list* ptr2=malloc(sizeof(struct list));
ptr2->data=data;
ptr2->link=NULL;
struct list* ptr=head;
while(pos>2){
ptr=ptr->link;
pos--;
}
ptr2->link=ptr->link;
ptr->link=ptr2;
}
//a. traverse the list from start to end
void print_list(struct list* ptr){
struct list* ptr2=ptr;
if(ptr2==NULL) printf("list is empty\n");
while(ptr2!=NULL){
printf("%d -> ",ptr2->data);
ptr2=ptr2->link;
}
printf("NULL\n");
}
int main(){
struct list* head;
head=malloc(sizeof(struct list));
head->data=0;
head->link=NULL;
printf("1.Print the linkedlist\n");
printf("2.Add element at the begining of the list\n");
printf("3.Add element at the end of the list\n");
printf("4.Add the element at a position read from the user\n");
printf("5.Exit from the program\n");
while(1){
printf("Enter your choice : ");
int choice;
scanf("%d",&choice);
int a,pos;
switch(choice){
case 1 :
printf("The Elements in the list are : ");
print_list(head);
break;
OUTPUT
case 2 :
printf("enter the element to be added at the beginning : ");
scanf("%d",&a);
head=add_at_beg(&head,a);
printf("List after addtion of %d at the begining is: ",a);
print_list(head);
break;
case 3 :
printf("enter the element to be added at the end : ");
scanf("%d",&a);
head=add_at_end(head,a);
printf("List after addtion of %d at the end is: ",a);
print_list(head);
break;
case 4 :
printf("enter the element to be added and the position also : ");
scanf("%d %d",&a,&pos);
add_at_pos(head,a,pos);
printf("List after addtion of %d at the %d is: ",a,pos);
print_list(head);
break;
case 5 : exit(0);
}
}
return 0;
}
2. Write a program to create a linked list of integers entered by user and delete a node:
a. At a particular position entered by the user
b. After a node having a key value
c. Before a node having a key value
d. Having a key value
After each deletion print the resultant Linked List.
CODE:
#include<stdio.h>
#include<stdlib.h>
struct list{
int data;
struct list* link;
};
if(ptr==NULL) {
return temp;
}
struct list* ptr2=ptr;
while(ptr2->link){
ptr2=ptr2->link;
}
ptr2->link=temp;
return ptr;
}
}
OUTPUT:
//b. Delete a node after key value
struct list* del_after_key(struct list* head,int key){
struct list* ptr=head;
while(ptr && ptr->data!=key){
ptr=ptr->link;
}
if(ptr==NULL){
printf("There is no node with data %d \n",key);
return head;
}
if(ptr->link==NULL) {
printf("There is no node after %d \n",key);
return head;
}
if(ptr->link!=NULL){
struct list* del=ptr->link;
ptr->link=del->link;
free(del);
}
return head;
}
int main(){
//making a list
struct list* head;
head=malloc(sizeof(struct list));
head->data=55;
head->link=NULL;
head=add_at_end(head,57);
head=add_at_end(head,59);
head=add_at_end(head,61);
head=add_at_end(head,63);
head=add_at_end(head,65);
printf("Initial list : ");
print_list(head);
while(1){
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice){
case 1 : printf("Enter the position of the node to delte : ");
scanf("%d",&pos);
del_pos(head,pos);
printf("List After deletion at positon %d: ",pos);
print_list(head);
break;
case 2 : printf("Enter the key of the node after which the node has to be
deleted : ");
scanf("%d",&key);
head=del_after_key(head,key);
printf("List After deletion of node after node with %d: ",key);
print_list(head);
break;
case 3 : printf("Enter the key of the node before which the node hase to be
deleted : ");
scanf("%d",&key);
head=del_before_key(head,key);
printf("List After deletion of node before node with %d: ",key);
print_list(head);
break;
case 4 : printf("Enter the key of the node to delete : ");
scanf("%d",&key);
head=del_key(head,key);
printf("List After deletion of node with %d: ",key);
print_list(head);
break;
case 5 : printf("\nEnd of the program\n");
exit(0);
break;
}
}
return 0;
}