Professional Documents
Culture Documents
Lab - 4 ( 24-08-2021 )
Q1) WAP to create a singly linked list and display the values of each node (atleast 5
nodes) using user defined create and display function (using iteration).
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data_788;
struct Node *next_788;
}*first_788=NULL;
void create(int A_788[], int n_788){
int i;
struct Node *temp_788, *last_788;
first_788=(struct Node *)malloc(sizeof(struct Node));
first_788->data_788=A_788[0];
first_788->next_788=NULL;
last_788=first_788;
for(i=1;i<n_788;i++){
temp_788=(struct Node *)malloc(sizeof(struct Node));
temp_788->data_788=A_788[i];
temp_788->next_788=NULL;
last_788->next_788=temp_788;
last_788=temp_788;
}
}
void Display(struct Node *temp_788){
printf("Displaying linked list:\n");
while(temp_788){
printf("%d ",temp_788->data_788);
temp_788=temp_788->next_788;
}
}
int main(){
struct Node *f_788;
int n_788,i,*A_788;
printf("Enter number of elements: ");
scanf("%d",&n_788);
A_788=(int*)malloc(n_788*sizeof(int));
printf("Enter elements of array:\n");
for(i=0;i<n_788;++i)
{
scanf("%d",A_788+i);
}
create(A_788,n_788);
Display(first_788);
return 0;
}
Output:
Q2) WAP to create a singly linked list and display the values of each node (atleast 5
nodes) using user defined create and display function (using recursion).
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data_788;
struct Node *next_788;
}*first_788=NULL;
void create(int A_788[], int n_788){
int i;
struct Node *temp_788, *last_788;
first_788=(struct Node *)malloc(sizeof(struct Node));
first_788->data_788=A_788[0];
first_788->next_788=NULL;
last_788=first_788;
for(i=1;i<n_788;i++){
temp_788=(struct Node *)malloc(sizeof(struct Node));
temp_788->data_788=A_788[i];
temp_788->next_788=NULL;
last_788->next_788=temp_788;
last_788=temp_788;
}
}
void RecursiveDisplay(struct Node *temp_788){
if(temp_788!=NULL){
printf("%d ",temp_788->data_788);
RecursiveDisplay(temp_788->next_788);
}
}
int main(){
struct Node *f_788;
int n_788,i,*A_788;
printf("Enter number of elements: ");
scanf("%d",&n_788);
A_788 = (int*)malloc(n_788*sizeof(int));
printf("Enter elements of array:\n");
for(i=0;i<n_788;++i)
{
scanf("%d",A_788+i);
}
create(A_788,n_788);
printf("Displaying linked list recursively:\n");
RecursiveDisplay(first_788);
return 0;
}
Output:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data_788;
struct node* next_788;
}*first_788=NULL,*last_788=NULL;
void create(int A_788[],int n_788){
for(int i=0;i<n_788;i++){
struct node *temp_788;
temp_788 = (struct node*)malloc(n_788*sizeof(struct node));
temp_788->data_788 = A_788[i];
temp_788->next_788 = NULL;
if(first_788 == NULL){
first_788 = temp_788;
last_788 = temp_788;
}
else{
last_788->next_788 = temp_788;
last_788 = temp_788;
}
}
}
void insert(int data_788,int pos_788){
struct node * temp_788;
temp_788 = (struct node *)malloc(sizeof(struct node));
if(pos_788 == 0){
temp_788->data_788 = first_788->data_788;
temp_788->next_788 = first_788->next_788;
first_788->next_788 = temp_788;
first_788->data_788 = data_788;
}
else{
struct node *x_788;
x_788 = (struct node *)malloc(sizeof(struct node));
x_788 = first_788;
for(int i=0;i<pos_788-1;i++){
x_788 = x_788->next_788;
}
temp_788->data_788 = data_788;
temp_788->next_788 = x_788->next_788;
x_788->next_788 = temp_788;
}
}
void insert_end(int data_788){
struct node *temp_788;
temp_788 = (struct node *)malloc(sizeof(struct node));
temp_788->data_788 = data_788;
last_788->next_788 = temp_788;
temp_788->next_788 = NULL;
last_788 = temp_788;
void display(){
struct node *show_788;
show_788 = first_788;
printf("\nThe elements in the linked list are: \n");
while(show_788){
printf("%d ",show_788->data_788);
show_788 = show_788->next_788;
}
}
void delete(){
struct node *temp_788;
while(first_788){
temp_788 = first_788;
first_788 = first_788->next_788;
free(temp_788);
}
}
void main()
{
int A_788[]={5,4,3,2,1};
int n_788=5,choice_788,data_788;
create(A_788,n_788);
while(1){
printf("\nEnter your choice: ");
printf("\n1. To enter element in the begining ");
printf("\n2. To enter the element at the end ");
printf("\n3. To enter at any position other than begining and end ");
printf("\n4. To quit\n");
scanf("%d",&choice_788);
switch(choice_788){
case 1:
printf("Enter the value: ");
scanf("%d",&data_788);
insert(data_788,0);
display();
break;
case 2:
printf("Enter the value: ");
scanf("%d",&data_788);
insert_end(data_788);
display();
break;
case 3:
printf("Enter the value: ");
scanf("%d",&data_788);
int pos_788;
printf("Enter positon to be entered: ");
scanf("%d",&pos_788);
insert(data_788,pos_788);
display();
break;
case 4:
exit(0);
default:
exit(0);
}
}
delete();
}
Output: