1.
Write a program to print the given Linked List in Reverse Order
CODE:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
struct list{
int data;
struct list* link;
};
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;
}
void print_reverse(struct list* head){
if(head==NULL){
return;
}
print_reverse(head->link);
printf("%d ",head->data);
return ;
}
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(){
clock_t start=clock();
OUTPUT:
// creating list 1->2->3->4->5
struct list* head=NULL;
int n;
printf("Enter the size of the list : ");
scanf("%d",&n);
printf("Enter the elements of the list : ");
for(int i=0;i<n;i++){
int a;
scanf("%d",&a);
head=add_at_end(head,a);
}
printf("The List is : ");
print_list(head);
printf("The list in reverse oder : ");
print_reverse(head);
clock_t end=clock();
double executiontime=(double)(end-start)/CLOCKS_PER_SEC;
printf("\nexection time : %f",executiontime);
return 0;
}
2. Write a program to sort the given Linked List.
CODE:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
struct list{
int data;
struct list* link;
};
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;
}
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");
}
void sort_list(struct list* head){
struct list* ptr1=head;
while(ptr1){
struct list* ptr2=ptr1;
while(ptr2){
if(ptr2->data < ptr1->data){
int a=ptr2->data;
ptr2->data=ptr1->data;
ptr1->data=a;
}
ptr2=ptr2->link;
OUTPUT:
}
ptr1=ptr1->link;
}
}
int main(){
clock_t start=clock();
// creating list 1->2->3->4->5
struct list* head=NULL;
int n;
printf("Enter the size of the list : ");
scanf("%d",&n);
printf("Enter the elements of the list : ");
for(int i=0;i<n;i++){
int a;
scanf("%d",&a);
head=add_at_end(head,a);
}
printf("The List is : ");
print_list(head);
printf("The list after sorting : ");
sort_list(head);
print_list(head);
clock_t end=clock();
double executiontime=(double)(end-start)/CLOCKS_PER_SEC;
printf("\nexection time : %f",executiontime);
return 0;
}
3. Write a program to create a circularly linked list of integers entered by user and insert a new
node:
a. At the beginning
b. At the end.
CODE:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
struct node {
int data;
struct node* link;
};
struct node* add_first(struct node* tail,int data){
struct node* ptr=malloc(sizeof(struct node));
ptr->data=data;
ptr->link=ptr;
return ptr;
}
struct node* add_at_beg(struct node* tail,int data){
if(tail==NULL) {
return add_first(tail,data);
}
struct node* ptr=malloc(sizeof(struct node));
ptr->data=data;
ptr->link=tail->link;
tail->link=ptr;
return tail;
}
struct node* add_at_end(struct node* tail,int data){
if(tail==NULL) {
return add_first(tail,data);
}
struct node* ptr=malloc(sizeof(struct node));
ptr->data=data;
ptr->link=NULL;
ptr->link=tail->link;
tail->link=ptr;
tail=ptr;
return tail;
}
OUTPUT:
void print(struct node* tail){
struct node* ptr=tail;
do{
ptr=ptr->link;
printf("%d->",ptr->data);
}
while(ptr!=tail);
printf("repeats \n");
}
int main(){
clock_t start=clock();
struct node* tail=NULL;
printf("1.Add at end\n");
printf("2.Add at begining\n");
printf("3.Exit\n");
int a;
while(1){
int choice;
printf("Enter Your Choice : ");
scanf("%d",&choice);
switch(choice){
case 1 : printf("Enter the element to be added at end :");
scanf("%d",&a);
tail=add_at_end(tail,a);
print(tail);
break;
case 2 : printf("Enter the element to be added at begining : ");
scanf("%d",&a);
tail=add_at_beg(tail,a);
print(tail);
break;
default :
exit(1);
}
}
clock_t end=clock();
double executiontime=(double)(end-start)/CLOCKS_PER_SEC;
printf("exection time : %f",executiontime);
return 0;
}