WEEK-10 CP LAB Programs
WEEK-10 CP LAB Programs
AIM:- To Write a C program to copy one structure variable to another structure of the same type.
DESCRIPTION:-
Structures in programming are a way to group different data types under a single name. They allow you to
create a custom data type that contains multiple fields or members, each with its own data type.
For instance, in C, a structure is declared using the struct keyword, followed by the structure name and
its members within curly braces. Here's an example:
struct stud {
char name[50];
int age;
};
In this case, stud is a structure containing two members: name (an array of
characters) and age (an integer)
To copy one structure variable to another structure of the same type, you can use the assignment (=)
operator.
Here's an example using the previously defined Person structure:
struct stud student1 = {"RAMU", 25};
struct stud student2;
ALGORITHAM:
STEP-1 START
STEP-2. Define a structure 'stud' with members 'name' and 'age'.
STEP-3. Declare variables:
- student1 (struct stud)
- student2 (struct stud)
STEP-4. Initialize student1 with values "RAMU" for name and 25 for age.
STEP-5. Copy the contents of student1 to student2.
STEP-6. Display the contents of student2 after copying:
a. write "Copied Structure:"
b. write the name and age from student2.
STEP-7 END.
C-PROGRAM
#include<stdio.h>
// Define a structure
struct stud {
char name[50];
int age;
};
int main() {
// Create structure variables
struct stud student1 = {"RAMU", 25};
struct stud student2;
The sizeof operator is used to determine the memory size occupied by each.
Structures allocate memory separately for each member, so the size of the structure is the sum of
the sizes of its members plus any padding required for alignment.
Unions, on the other hand, allocate memory that is just enough to hold the largest member.
This means that the size of a union is equal to the size of its largest member.
When you run this program, we will notice that the size of the structure is the sum of the sizes of its
members, including any padding for alignment, while the size of the union is equal to the size of its
largest member.
This showcases a fundamental difference in memory allocation between structures and unions in C.
C-Program:
#include <stdio.h>
// Define a structure
struct Person {
char name[20];
int age;
};
// Define a union
union Info {
int id;
float salary;
};
int main() {
// Structure example
printf("Size of Structure: %lu\n", sizeof(struct Person));
// Union example
printf("Size of Union: %lu\n", sizeof(union Info));
return 0;
}
OUTPUT:
Size of Structure : 24
Size of Union: 4
Create and display a singly linked list using self-referential structure.
AIM:- To Write a C program to implement single linked list operations.
Description
In single linked list, each node is divided into two parts as INFO/DATA part and LINK part.
DATA LINK
NODE
Where, the first part DATA field consist information part of the element and the second
part LINK field consist address of the next node in the list.
Example:
1 2 3
59 2 38 3 64
START END
Here, START and END are two pointer variables that points to beginning and ending
nods of the list. The LINK field of the last node filled with a special pointer called NULL
pointer.
i) Creating a list
ii) Traversing the list
iii) Insertion of a node into the list
iv) Deletion of a node from the list
v) Counting number of elements
vi) Searching an element etc.,
Algorithm creation (): This procedure creates a single liked list with the specified
number of elements.
Algorithm display(): This procedure is used to display the elements of the single linked
list from the first node to the last node.
Algorithm Finsertion(x): This procedure inserts an element x at front end of the list.
Algorithm Fdeletion( ): This function deletes the front element of the list.
Algorithm Anydeletion(pos): This function deletes the element of the list from the
specified position pos.
Program:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
node *start=NULL,*end=NULL;
void create();
void display();
void finsertion(int);
void rinsertion(int);
void anyinsertion(int,int);
int fdeletion();
int rdeletion();
int anydeletion(int);
main()
{
int ch,item,p;
clrscr();
while(1)
{
printf("\n\n1:CREATION\n2:FRONT INSERTION\n3:REAR INSERTION\n");
printf("4:ANY POSITION INSERTION\n5:FRONT DELETION\n");
printf("6:REAR DELETION\n 7:ANY POSITION DELETION\n”);
printf(“\n8:DISPLAY\n9:EXIT");
printf("\n\nEnter Your Choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:printf("\nEnter An Element For Insertion=");
scanf("%d",&item);
finsertion(item);
break;
void create()
{
node *new;
int x;
while(1)
{
printf("\nEnter An Element(Stop:-999)=");
scanf("%d",&x);
if(x==-999)
return;
else
{
new=(node*)malloc(sizeof(node));
new->DATA=x;
new->LINK=NULL;
if(start==NULL)
start=end=new;
else
{
end->LINK=new;
end=new;
}
}
}
}
void finsertion(int x)
{
node *new;
new=(node*)malloc(sizeof(node));
new->DATA=x;
new->LINK=NULL;
if(start==NULL)
start=end=new;
else
{
new->LINK=start;
start=new;
}
}
void rinsertion(int x)
{
node *new;
new=(node*)malloc(sizeof(node));
new->DATA=x;
new->LINK=NULL;
if(end==NULL)
start=end=new;
else
{
end->LINK=new;
end=new;
}
}
void anyinsertion(int x,int pos)
{
node *new,*ptr,*temp;
int k;
new=(node*)malloc(sizeof(node));
new->DATA=x;
new->LINK=NULL;
k=1;
ptr=start;
while(k<pos)
{
temp=ptr;
ptr=ptr->LINK;
k=k+1;
}
temp->LINK=new;
new->LINK=ptr;
return;
}
int fdeletion()
{
node *temp;
int k;
if(start==NULL)
return -1;
else
{
k=start->DATA;
if(start==end)
{
start=end=NULL;
return k;
}
else
{
temp=start;
start=start->LINK;
temp->LINK=NULL;
free(temp);
return k;
}
}
}
int rdeletion()
{
node *temp,*ptr;
int k;
if(end==NULL)
return -1;
else
{
k=end->DATA;
if(start==end)
{
start=end=NULL;
return k;
}
else
{
ptr=start;
temp=end;
while(ptr->LINK!=end)
{
ptr=ptr->LINK;
}
end=ptr;
end->LINK=NULL;
free(temp);
return k;
}
}
}
int anydeletion(int pos)
{
node *ptr,*temp;
int k,s;
if(start==NULL)
return -1;
else
{
ptr=start;
k=1;
while(k<pos)
{
temp=ptr;
ptr=ptr->LINK;
k=k+1;
}
s=ptr->DATA;
temp->LINK=ptr->LINK;
ptr->LINK=NULL;
free(ptr);
return s;
}
}
void display()
{
node *temp=start;
if(start!=NULL)
{
printf("\nList of Elements Are=\n");
while(temp!=NULL)
{
printf("%d\t",temp->DATA);
temp=temp->LINK;
}
}
else
printf("\nList Empty");
}
Result
1:CREATION
2:FRONT INSERTION
3:REAR INSERTION
4:ANY POSITION INSERTION
5:FRONT DELETION
6:REAR DELETION
7:ANY POSITION DELETION
8:DISPLAY
9:EXIT
Enter An Element(Stop:-999) = 11
Enter An Element(Stop:-999) = 22
Enter An Element(Stop:-999) = 33
Enter An Element(Stop:-999) = 44
Enter An Element(Stop:-999) = -999
1:CREATION
2:FRONT INSERTION
3:REAR INSERTION
4:ANY POSITION INSERTION
5:FRONT DELETION
6:REAR DELETION
7:ANY POSITION DELETION
8:DISPLAY
9:EXIT