0% found this document useful (0 votes)
32 views14 pages

WEEK-10 CP LAB Programs

The document describes how to create and manipulate a singly linked list using self-referential structures in C. It defines a node structure with a data and link field, and explains how to implement basic linked list operations like creation, traversal, insertion, deletion through algorithms using pointer variables to navigate between nodes.

Uploaded by

rishithamedabali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views14 pages

WEEK-10 CP LAB Programs

The document describes how to create and manipulate a singly linked list using self-referential structures in C. It defines a node structure with a data and link field, and explains how to implement basic linked list operations like creation, traversal, insertion, deletion through algorithms using pointer variables to navigate between nodes.

Uploaded by

rishithamedabali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

WEEK-10

Copy one structure variable to another structure of the same type.

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;

// Copying student1 to student2


student2 = student1;

In this example, student1 is copied to student2 using the assignment operator.


This operation copies each member of student 1 into the corresponding member of student2.
After this operation, student2 will have the same name ("RAMU") and age (25) as student 1.

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;

// Copying one structure variable to another


student2=student1;
// Displaying the contents of the second structure (after copying)
printf("Copied Structure:\nName: %s\nAge: %d\n", student2.name, student2.age);
}

OUTPUT:- Copied Structure:


Name: RAMU
Age: 25
Demonstrate the differences between structures and unions
AIM:- the C-program that Demonstrate the differences between structures and unions.
DESCRIPTION:-
structures and unions are both ways to define custom data types that can hold multiple variables of
different data types. The key difference lies in how they allocate memory for their members.
In this program ,
a structure Person with two members: name (an array of characters) and age (an integer).
Additionally, there's a union Info with two members: id (an integer) and salary (a float).

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.

Basic operations performed on the single linked list are:

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.

Step 1: Repeat WHILE TRUE


READ an element as x
IF x = -999 THEN
RETURN
ELSE
Allocate memory for a NEW node
DATA(NEW) ← x
LINK(NEW) ← NULL
IF START = NULL THEN
START ← END ← NEW
ELSE
LINK(END) ← NEW
END ← NEW
ENDIF
ENDIF
EndRepeat

Algorithm display(): This procedure is used to display the elements of the single linked
list from the first node to the last node.

Step 1: TEMP ← START


Step 2: IF START ≠ NULL THEN
Repeat WHILE TEMP ≠ NULL
WRITE DATA(TEMP)
TEMP ← LINK(TEMP)
EndRepeat
ELSE
WRITE „LIST EMPTY‟
ENDIF
Step 3: RETURN

Algorithm Finsertion(x): This procedure inserts an element x at front end of the list.

Step 1: Allocate memory for a NEW node


DATA(NEW) ← x
LINK(NEW) ← NULL
Step 2: IF START = NULL THEN
START ← END ← NEW
ELSE
LINK(NEW) ← START
START ← NEW
ENDIF
Step 3: RETURN
Algorithm Rinsertion(x): This procedure inserts an element x at rear end of the list.
Step 1: Allocate memory for a NEW node
DATA(NEW) ← x
LINK(NEW) ← NULL
Step 2: IF END = NULL THEN
START ← END ← NEW
ELSE
LINK(END) ← NEW
END ← NEW
ENDIF
Step 3: RETURN

Algorithm Anyinsertion(x, pos): This procedure inserts an element x at the specified


position pos of the list.

Step 1: Allocate memory for a NEW node


DATA(NEW) ← x
LINK(NEW) ← NULL
Step 2: k←1
PTR ← START
Step 3: Repeat WHILE k < pos
TEMP ← PTR
PTR ← LINK(PTR)
k ← k+1
EndRepeat
Step 4: LINK(TEMP) ← NEW
LINK(NEW) ← PTR
Step 5: RETURN

Algorithm Fdeletion( ): This function deletes the front element of the list.

Step 1: IF START = NULL THEN


RETURN -1
ELSE
k← DATA(START)
IF START = END THEN
START ← END ← NULL
ELSE
TEMP ←START
START ← LINK(START)
LINK(TEMP) ← NULL
Release memory of TEMP
ENDIF
RETURN k
ENDIF
Algorithm Rdeletion( ): This function deletes the rear element of the list.

Step 1: IF END = NULL THEN


RETURN -1
ELSE
k← DATA(END)
IF START = END THEN
START ← END ← NULL
ELSE
PTR ← START
TEMP ← END
Repeat WHILE LINK(PTR) ≠ END
PTR ← LINK(PTR)
EndRepeat
END ← PTR
LINK(END) ← NULL
Release memory of TEMP
ENDIF
RETURN k
ENDIF

Algorithm Anydeletion(pos): This function deletes the element of the list from the
specified position pos.

Step 1: IF START = NULL Then


RETURN -1
ELSE
PTR ← START
p←1
Repeat WHILE p < pos
TEMP ← PTR
PTR ← LINK(PTR)
p ← p+1
EndRepeat
k ← DATA(PTR)
LINK(TEMP) ← LINK(PTR)
LINK(PTR) ← NULL
Release memory of PTR
RETURN k
ENDIF

Program:

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

typedef struct list


{
int DATA;
struct list *LINK;
}node;

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;

case 3:printf("\nEnter An Element For Insertion=");


scanf("%d",&item);
rinsertion(item);
break;
case 4:printf("\nEnter An Element For Insertion=");
scanf("%d",&item);
printf("\nEnter A Position For Insertion=");
scanf("%d",&p);
anyinsertion(item,p);
break;
case 5:p=fdeletion();
if(p==-1)
printf("\nList Empty");
else
printf("\nFront Deleted Element=%d",p);
break;
case 6:p=rdeletion();
if(p==-1)
printf("\nList Empty");
else
printf("\nRear Deleted Element=%d",p);
break;
case 7:printf("\nEnter A Position For Deletion=");
scanf("%d",&p);
item=anydeletion(p);
if(item==-1)
printf("\nList Empty");
else
printf("\nSpecifed Deleted Element=%d",item);
break;
case 8:display();
break;
case 9:exit();
}
}
}

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 Your Choice = 1

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

Enter Your Choice = 8

List of Elements Are = 11 22 33 44

You might also like