You are on page 1of 12

Week 10

Week 10 : Bitfields, Self-Referential Structures, Linked lists

i) Create and display a singly linked list using self-referential structure.

Aim: To write a C program to create and display singly linked list using self-referential structure

Algorithm:

Step 1: Define the structure for a node in the linked list

struct Node {

int data;

struct Node * next;

};

Step 2: Function to create a new node

struct Node * createNode(int data) {

1. Allocate memory for a new node using `malloc`.

2. Check if memory allocation is successful.

3. Set the data of the new node to the provided value.

4. Set the `next` pointer of the new node to NULL.

5. Return the pointer to the new node.

Step 3: Function to display the linked list

void displayList(struct Node * head) {

1. Traverse the linked list:

a. While the current node is not NULL:

i. Print the data of the current node.

ii. Move to the next node.


2. Print "NULL" to indicate the end of the list.

Step 4: Main function:

int main( ) {

1. Declare a pointer to the head of the linked list (initially set to NULL).

2. Add nodes to the linked list:

a. Create a new node using `createNode` and assign it to the head pointer.

b. Connect additional nodes by updating the `next` pointers.

3. Display the linked list using `displayList`.

4. Return 0 to indicate successful execution.

Program:

#include <stdio.h>

#include <stdlib.h>

// Define the structure for a node in the linked list

struct Node

int data;

struct Node * next;

};

// Function to create a new node

struct Node * createNode(int data)

struct Node * newNode = (struct Node *)malloc(sizeof(struct Node));

if (new Node == NULL)

{
printf("Memory allocation failed\n");

exit(1);

newNode -> data = data;

newNode -> next = NULL;

return newNode;

// Function to display the linked list

void displayList(struct Node * head) {

printf("Linked List: ");

while (head != NULL) {

printf("%d -> ", head -> data);

head = head -> next;

printf("NULL\n");

int main( ) {

// Initialize an empty linked list

struct Node * head = NULL;

// Add nodes to the linked list

head = createNode(1);

head -> next = createNode(2);

head -> next->next = createNode(3);

head -> next -> next -> next = createNode(4);

// Display the linked list

displayList(head);
return 0;

Result:

Linked List: 1 -> 2 -> 3 -> 4 -> NULL

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:

Step 1: Define a structure

struct StructExample {

int intValue;

char charValue;

float floatValue;

};

Step 2: Define a union:

union UnionExample {

int intValue;

char charValue;

float floatValue;

};

Step 3: Main function:

int main( ) {

1. Declare an instance of the structure `StructExample`.

2. Set values for the structure members.

3. Display the values of structure members and the size of the structure.

4. Declare an instance of the union `UnionExample`.

5. Set values for the union members.


6. Display the values of union members and the size of the union.

7. Return 0 to indicate successful execution.

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 ( ) {

// Declare an instance of the structure StructExample

struct StructExample structVar;

// Set values for structure members

structVar.intValue = 10;

structVar.charValue = 'A';

structVar.floatValue = 3.14;
// Display structure values

printf("Structure Values:\n");

printf("Integer Value: %d\n", structVar.intValue);

printf("Char Value: %c\n", structVar.charValue);

printf("Float Value: %f\n", structVar.floatValue);

printf("Size of Structure: %lu bytes\n\n", sizeof(struct StructExample));

// Declare an instance of the union UnionExample

union UnionExample unionVar;

// Set values for union members

unionVar.intValue = 20;

// Display union values

printf("Union Values:\n");

printf("Integer Value: %d\n", unionVar.intValue);

printf("Char Value: %c\n", unionVar.charValue);

printf("Float Value: %f\n", unionVar.floatValue);

printf("Size of Union: %lu bytes\n", sizeof(union UnionExample));

return 0;

Result:

Structure Values:

Integer Value: 10

Char Value: A

Float Value: 3.140000

Size of Structure: 12 bytes


Union Values:

Integer Value: 20

Char Value:

Float Value: 0.000000

Size of Union: 4 bytes

iii) Write a C program to shift/rotate using bitfields.

Aim: To write a C program to shift/rotate using bitfields.

Step 1: Define a bitfield structure:

struct BitfieldExample {

unsigned int data : n; // n-bit field (adjust 'n' as needed)

};

Step 2: Function to display the binary representation of an unsigned integer:

void displayBinary(unsigned int num) {

1. Iterate through the bits of the number from the most significant bit to the least significant bit.

2. Print each bit.

3. Add spaces for better readability.

Step 3: Main function:

int main() {

1. Declare an instance of the bitfield structure.

2. Initialize the bitfield data.

3. Display the original binary representation.

4. Perform bitwise left shift.

5. Display the binary representation after left shift.

6. Perform bitwise right shift.

7. Display the binary representation after right shift.


8. Perform bitwise rotation to the left.

9. Display the binary representation after left rotation.

10. Return 0 to indicate successful execution.

Program:

#include <stdio.h>

// Define a bitfield structure

struct BitfieldExample {

unsigned int data : 4; // 4-bit field

};

// Function to display the binary representation of an unsigned integer

void displayBinary(unsigned int num)

int i;

for ( i = sizeof(unsigned int) * 8 - 1; i >= 0; i--)

printf("%d", (num >> i) & 1);

if (i % 4 == 0)

printf(" "); // Add a space for better readability

printf("\n");

int main( ) {

// Declare an instance of the bitfield structure

struct BitfieldExample bitfieldVar;

// Initialize the bitfield data


bitfieldVar.data = 5; // Binary representation: 0101

// Display the original binary representation

printf("Original Binary Representation: ");

displayBinary(bitfieldVar.data);

// Perform bitwise left shift

bitfieldVar.data = bitfieldVar.data << 2;

// Display the binary representation after left shift

printf("After Left Shift (<< 2): ");

displayBinary(bitfieldVar.data);

// Perform bitwise right shift

bitfieldVar.data = bitfieldVar.data >> 1;

// Display the binary representation after right shift

printf("After Right Shift (>> 1): ");

displayBinary(bitfieldVar.data);

// Perform bitwise rotation to the left

bitfieldVar.data = (bitfieldVar.data << 1) | (bitfieldVar.data >> (4 - 1));

// Display the binary representation after left rotation

printf("After Left Rotation: ");

displayBinary(bitfieldVar.data);

return 0;

Result:

Original Binary Representation: 0101

After Left Shift (<< 2): 010100

After Right Shift (>> 1): 001010

After Left Rotation: 010001


iv) Write a C program to 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.

Algorithm:

Step 1: Define a structure:

struct Student {

char name[50];

int age;

float gpa;

};

Step 2: Function to copy one structure variable to another:

void copyStruct(struct Student* dest, const struct Student* source) {

1. Copy the `name` member from `source` to `dest`.

2. Copy the `age` member from `source` to `dest`.

3. Copy the `gpa` member from `source` to `dest`.

Step 3: Function to display the content of a structure:

void displayStruct(const struct Student* student) {

1. Display the `name` member.

2. Display the `age` member.

3. Display the `gpa` member.

Step 4: Main function:

int main ( ) {

1. Declare and initialize a structure variable, e.g., `student1`.

2. Declare another structure variable, e.g., `student2`.

3. Copy the content of `student1` to `student2` using `copyStruct`.

4. Display the content of both structures using `displayStruct`.


5. Return 0 to indicate successful execution.

Program:

#include <stdio.h>

#include <string.h>

// Define a structure

struct Student {

char name[50];

int age;

float gpa;

};

// Function to copy one structure variable to another

void copyStruct(struct Student * dest, const struct Student * source) {

// Copy individual members from source to destination

strcpy(dest -> name, source -> name);

dest -> age = source -> age;

dest -> gpa = source -> gpa;

// Function to display the content of a structure

void displayStruct(const struct Student * student) {

printf("Name: %s\n", student -> name);

printf("Age: %d\n", student -> age);

printf("GPA: %.2f\n", student -> gpa);

printf("\n");

int main( ) {
// Declare and initialize a structure variable

struct Student student1 = {"John Doe", 20, 3.75};

// Declare another structure variable

struct Student student2;

// Copy the content of the first structure to the second

copyStruct(&student2, &student1);

// Display the content of both structures

printf("Original Structure:\n");

displayStruct(&student1);

printf("Copied Structure:\n");

displayStruct(&student2);

return 0;

Result:

Original Structure:

Name: John Doe

Age: 20

GPA: 3.75

Copied Structure:

Name: John Doe

Age: 20

GPA: 3.75

You might also like