NAME: MBAROUK SALUM MBAROUK
REG NO: BCS/13/18/010/TZ
QUESTION 4: Write a program to implement single linear
linked list with the following operations
a.
Creation
b.
Deletion at beginning
c.
Deletion at Middle
d.
Deletion at end
e.
Display(rear to head)
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node
*head=NULL,*tail=NULL;
void
createNode(){
struct node *ptr;
ptr=(struct node *) malloc(sizeof(struct
node));
printf("Enter the data: ");
scanf("%d",&ptr->data);
ptr->next=NULL;
if(head==NULL){
head=ptr;
tail=ptr;
}else{
tail->next=ptr;
tail=ptr;
}
}
void
deleteBegin(){
struct node *tpr;
tpr=head;
head=tpr->next;
free(tpr);
}
void
deleteMiddle(){
struct node *ptr,*tpr;
int pos;
printf("Enter the position of node to be
deleted: ");
scanf("%d",&pos);
int i;
tpr=head;
for(i=1;i<pos;i++){
ptr=tpr;
tpr=tpr->next;
if(tpr==NULL){
printf("Position out of range\n");
return;
}
}
ptr->next=tpr->next;
free(tpr);
}
void
deleteEnd(){
struct node *ptr,*tpr;
ptr=head;
while(ptr->next!=NULL){
tpr=ptr;
ptr=ptr->next;
}
free(ptr);
tail=tpr;
tail->next=NULL;
}
void
display(){
struct node *ptr;
ptr=head;
while(ptr!=NULL)
{
printf("%d \t",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
int main(){
int choice;
do{
printf("\n1.Create the node\n2.Delete
node at the begining.\n3.Delete node at the middle.\n4.Delete node at the
end\n5.Display\n6.Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1:
createNode();
break;
case 2:
deleteBegin();
break;
case 3:
deleteMiddle();
break;
case 4:
deleteEnd();
break;
case 5:
display();
break;
case 6:
exit(0);
break;
default:
printf("wrong choice\n");
break;
}
}while(choice!=6);
return 0;
}
OUTPUT
NAME: MBAROUK SALUM MBAROUK
REG NO: BCS/13/18/010/TZ
QUESTION 4: Write a program to implement single linear
linked list with the following operations
a.
Creation
b.
Deletion at beginning
c.
Deletion at Middle
d.
Deletion at end
e.
Display(rear to head)
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node
*head=NULL,*tail=NULL;
void
createNode(){
struct node *ptr;
ptr=(struct node *) malloc(sizeof(struct
node));
printf("Enter the data: ");
scanf("%d",&ptr->data);
ptr->next=NULL;
if(head==NULL){
head=ptr;
tail=ptr;
}else{
tail->next=ptr;
tail=ptr;
}
}
void
deleteBegin(){
struct node *tpr;
tpr=head;
head=tpr->next;
free(tpr);
}
void
deleteMiddle(){
struct node *ptr,*tpr;
int pos;
printf("Enter the position of node to be
deleted: ");
scanf("%d",&pos);
int i;
tpr=head;
for(i=1;i<pos;i++){
ptr=tpr;
tpr=tpr->next;
if(tpr==NULL){
printf("Position out of range\n");
return;
}
}
ptr->next=tpr->next;
free(tpr);
}
void
deleteEnd(){
struct node *ptr,*tpr;
ptr=head;
while(ptr->next!=NULL){
tpr=ptr;
ptr=ptr->next;
}
free(ptr);
tail=tpr;
tail->next=NULL;
}
void
display(){
struct node *ptr;
ptr=head;
while(ptr!=NULL)
{
printf("%d \t",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
int main(){
int choice;
do{
printf("\n1.Create the node\n2.Delete
node at the begining.\n3.Delete node at the middle.\n4.Delete node at the
end\n5.Display\n6.Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1:
createNode();
break;
case 2:
deleteBegin();
break;
case 3:
deleteMiddle();
break;
case 4:
deleteEnd();
break;
case 5:
display();
break;
case 6:
exit(0);
break;
default:
printf("wrong choice\n");
break;
}
}while(choice!=6);
return 0;
}
OUTPUT
Hakuna maoni:
Chapisha Maoni