Dear Students
It is advised to go through the Theory part in order to understand Lab Experiments, Here I am just giving your some revision exercise so that you will be ready for different code in DS Lab.
Go through the Structure Video discussed in C programming
Video-1
Link for Other brush up Videos
After this Study following post first one by one in sequence
Program-1
Write a C program that uses functions to perform the following:
1. Create a singly linked list of integers.
2. Delete a given integer from the above-linked list.
3. Display the contents of the above list after deletion.
Solution
/*
Code -Dr. Mukesh Mann @IIIT Sonepat
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*head;
void createnode(int n)
{
head=(struct node *)malloc(sizeof(struct node));
int data;
printf("Enter head data=");
scanf("%d",&data);
head->data=data;
head->link=NULL;
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp=head;
struct node *newnode;
for(int a=0;a<n-1;a++)
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("Enter node data=");
scanf("%d",&data);
newnode->data=data;
newnode->link=NULL;
temp->link=newnode;
temp=temp->link;
}
}
void traverse()
{
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp=head;
while(temp)
{
printf("Data you entered=%d\n",temp->data);
temp=temp->link;
}
}
void delete()
{
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp=head;
int del;
printf("Enter data of the which you want to delete=");
scanf("%d",&del);
while(temp->data)
{
if(head->data==del)
{
head=temp->link;
free(temp);
break;
}
else if(temp->link->data==del)
{
temp->link=temp->link->link;
break;
}
temp=temp->link;
}
}
void main()
{
int n;
printf("Enter number of nodes in list=");
scanf("%d",&n);
createnode(n);
traverse();
delete();
traverse();
}
great
ReplyDelete