0% found this document useful (0 votes)
58 views11 pages

Lab 5

The document contains three C programs that demonstrate different types of linked lists: a singly linked list with insertion, deletion, counting, and traversal functions; a circular linked list with insertion and display functions; and a sparse matrix representation using a linked list with creation and display functions. Each program includes a main function that interacts with the user to perform operations on the linked lists. The code is structured with appropriate functions for managing nodes and handling user input.

Uploaded by

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

Lab 5

The document contains three C programs that demonstrate different types of linked lists: a singly linked list with insertion, deletion, counting, and traversal functions; a circular linked list with insertion and display functions; and a sparse matrix representation using a linked list with creation and display functions. Each program includes a main function that interacts with the user to perform operations on the linked lists. The code is structured with appropriate functions for managing nodes and handling user input.

Uploaded by

abhishek.sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd

1.#include <stdio.

h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

void insert(struct Node** head, int data, int position) {

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

newNode->data = data;

newNode->next = NULL;

if (position == 1) {

newNode->next = *head;

*head = newNode;

return;

struct Node* current = *head;

for (int i = 1; i < position - 1 && current != NULL; i++) {

current = current->next;

if (current == NULL) {
printf("Invalid position.\n");

free(newNode);

return;

newNode->next = current->next;

current->next = newNode;

void deleteNode(struct Node** head, int position) {

if (*head == NULL) {

printf("List is empty. Deletion not possible.\n");

return;

if (position == 1) {

struct Node* temp = *head;

*head = (*head)->next;

free(temp);

return;

struct Node* current = *head;

struct Node* previous = NULL;

for (int i = 1; i < position && current != NULL; i++) {


previous = current;

current = current->next;

if (current == NULL) {

printf("Invalid position.\n");

return;

previous->next = current->next;

free(current);

int countNodes(struct Node* head) {

int count = 0;

struct Node* current = head;

while (current != NULL) {

count++;

current = current->next;

return count;

void traverse(struct Node* head) {

struct Node* current = head;


while (current != NULL) {

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

current = current->next;

printf("NULL\n");

int main() {

struct Node* head = NULL;

int n, choice, element, position;

printf("Enter number of nodes: ");

scanf("%d", &n);

for (int i = 0; i < n; i++) {

printf("Enter element %d: ", i + 1);

scanf("%d", &element);

insert(&head, element, i + 1);

while (1) {

printf("\nMENU:\n");

printf("1. Insert the node at a position\n");

printf("2. Delete a node from specific position\n");

printf("3. Count\n");
printf("4. Traversal\n");

printf("5. Exit\n");

printf("Enter choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter element: ");

scanf("%d", &element);

printf("Enter position: ");

scanf("%d", &position);

insert(&head, element, position);

printf("Node inserted\n");

break;

case 2:

printf("Enter position to delete: ");

scanf("%d", &position);

deleteNode(&head, position);

printf("Node deleted\n");

break;

case 3:

printf("The total number of nodes: %d\n", countNodes(head));

break;

case 4:

printf("The list is: ");


traverse(head);

break;

case 5:

exit(0);

default:

printf("Invalid choice\n");

return 0;

2.#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* insertEnd(struct Node* head, int data) {

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

newNode->data = data;

if (head == NULL) {

newNode->next = newNode;
return newNode;

struct Node* temp = head;

while (temp->next != head) {

temp = temp->next;

temp->next = newNode;

newNode->next = head;

return head;

void display(struct Node* head) {

if (head == NULL) {

printf("Circular Linked List is empty.\n");

return;

struct Node* temp = head;

do {

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

temp = temp->next;

} while (temp != head);


printf("...\n");

int main() {

struct Node* head = NULL;

int n, data;

printf("Enter the number of elements in the circular linked list: ");

scanf("%d", &n);

for (int i = 0; i < n; i++) {

printf("Enter element %d: ", i + 1);

scanf("%d", &data);

head = insertEnd(head, data);

printf("Circular Linked List elements: ");

display(head);

return 0;

3.#include <stdio.h>

#include <stdlib.h>

struct Node {
int row, col, value;

struct Node* next;

};

struct Node* createNode(int row, int col, int value) {

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

newNode->row = row;

newNode->col = col;

newNode->value = value;

newNode->next = NULL;

return newNode;

void displaySparseMatrix(struct Node* head, int rows, int cols) {

struct Node* current = head;

for (int i = 0; i < rows; ++i) {

for (int j = 0; j < cols; ++j) {

if (current != NULL && current->row == i && current->col == j) {

printf("%d ", current->value);

current = current->next;

} else {

printf("0 ");

printf("\n");
}

struct Node* createSparseMatrix(int matrix[][3], int size) {

struct Node* head = NULL;

struct Node* tail = NULL;

for (int i = 0; i < size; ++i) {

struct Node* newNode = createNode(matrix[i][0], matrix[i][1], matrix[i][2]);

if (head == NULL) {

head = tail = newNode;

} else {

tail->next = newNode;

tail = newNode;

return head;

int main() {

int matrix[][3] = {

{0, 1, 3},

{1, 0, 4},
{2, 2, 5}

};

int rows = 3;

int cols = 3;

struct Node* sparseMatrix = createSparseMatrix(matrix, sizeof(matrix) / sizeof(matrix[0]));

printf("Sparse Matrix:\n");

displaySparseMatrix(sparseMatrix, rows, cols);

return 0;

You might also like