CPWeek 10
CPWeek 10
CPWeek 10
Aim: To write a C program to create and display singly linked list using self-referential structure
Algorithm:
struct Node {
int data;
};
int main( ) {
1. Declare a pointer to the head of the linked list (initially set to NULL).
a. Create a new node using `createNode` and assign it to the head pointer.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node
int data;
};
{
printf("Memory allocation failed\n");
exit(1);
return newNode;
printf("NULL\n");
int main( ) {
head = createNode(1);
displayList(head);
return 0;
Result:
ii) Demonstrate the differences between structures and unions using a C program.
Aim: To write a C program to Demonstrate the differences between structures and unions using a C
program.
Algorithm:
struct StructExample {
int intValue;
char charValue;
float floatValue;
};
union UnionExample {
int intValue;
char charValue;
float floatValue;
};
int main( ) {
3. Display the values of structure members and the size of the structure.
Program:
#include <stdio.h>
// Define a structure
struct StructExample
int intValue;
char charValue;
float floatValue;
};
// Define a union
union UnionExample
int intValue;
char charValue;
float floatValue;
};
int main ( ) {
structVar.intValue = 10;
structVar.charValue = 'A';
structVar.floatValue = 3.14;
// Display structure values
printf("Structure Values:\n");
unionVar.intValue = 20;
printf("Union Values:\n");
return 0;
Result:
Structure Values:
Integer Value: 10
Char Value: A
Integer Value: 20
Char Value:
struct BitfieldExample {
};
1. Iterate through the bits of the number from the most significant bit to the least significant bit.
int main() {
Program:
#include <stdio.h>
struct BitfieldExample {
};
int i;
if (i % 4 == 0)
printf("\n");
int main( ) {
displayBinary(bitfieldVar.data);
displayBinary(bitfieldVar.data);
displayBinary(bitfieldVar.data);
displayBinary(bitfieldVar.data);
return 0;
Result:
Algorithm:
struct Student {
char name[50];
int age;
float gpa;
};
int main ( ) {
Program:
#include <stdio.h>
#include <string.h>
// Define a structure
struct Student {
char name[50];
int age;
float gpa;
};
printf("\n");
int main( ) {
// Declare and initialize a structure variable
copyStruct(&student2, &student1);
printf("Original Structure:\n");
displayStruct(&student1);
printf("Copied Structure:\n");
displayStruct(&student2);
return 0;
Result:
Original Structure:
Age: 20
GPA: 3.75
Copied Structure:
Age: 20
GPA: 3.75