0% found this document useful (0 votes)
11 views4 pages

SLL Applications: Polynomial, Student, Sparse Matrix

The document outlines various applications of singly linked lists (SLL) in C programming, including polynomial representation, student record management, sparse matrix representation, large number arithmetic, and reversing a linked list. Each application includes code snippets, sample outputs, and time complexity analysis, all demonstrating the use of SLL for efficient data handling. The overall time complexity for each application is O(n), indicating linear performance relative to the number of elements.

Uploaded by

vgvms2058
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)
11 views4 pages

SLL Applications: Polynomial, Student, Sparse Matrix

The document outlines various applications of singly linked lists (SLL) in C programming, including polynomial representation, student record management, sparse matrix representation, large number arithmetic, and reversing a linked list. Each application includes code snippets, sample outputs, and time complexity analysis, all demonstrating the use of SLL for efficient data handling. The overall time complexity for each application is O(n), indicating linear performance relative to the number of elements.

Uploaded by

vgvms2058
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

DSA Assignment - SLL Applications

1. Polynomial Representation using SLL

#include <stdio.h>
#include <stdlib.h>

typedef struct Term {


int coeff, exp;
struct Term* next;
} Term;

void display(Term* head) {


while (head) {
printf("%dx^%d", head->coeff, head->exp);
head = head->next;
if (head) printf(" + ");
}
printf("\n");
}

int main() {
Term* head = (Term*)malloc(sizeof(Term));
head->coeff = 3; head->exp = 2;
head->next = (Term*)malloc(sizeof(Term));
head->next->coeff = 5; head->next->exp = 1;
head->next->next = (Term*)malloc(sizeof(Term));
head->next->next->coeff = -6; head->next->next->exp = 0;
head->next->next->next = NULL;

display(head);
return 0;
}

Sample Output: 3x^2 + 5x^1 + -6x^0

Time Complexity: O(n)

2. Student Record Management using SLL

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Student {


int id;
char name[20];
struct Student* next;
} Student;

void display(Student* head) {


while (head) {
DSA Assignment - SLL Applications

printf("ID: %d, Name: %s\n", head->id, head->name);


head = head->next;
}
}

int main() {
Student* s1 = (Student*)malloc(sizeof(Student));
s1->id = 101; strcpy(s1->name, "Alice");

Student* s2 = (Student*)malloc(sizeof(Student));
s2->id = 102; strcpy(s2->name, "Bob");
s1->next = s2;
s2->next = NULL;

display(s1);
return 0;
}

Sample Output: ID: 101, Name: Alice\nID: 102, Name: Bob

Time Complexity: O(n)

3. Sparse Matrix Representation using SLL

#include <stdio.h>
#include <stdlib.h>

typedef struct Element {


int row, col, val;
struct Element* next;
} Element;

void display(Element* head) {


while (head) {
printf("(%d, %d, %d)\n", head->row, head->col, head->val);
head = head->next;
}
}

int main() {
Element* e1 = (Element*)malloc(sizeof(Element));
e1->row = 0; e1->col = 1; e1->val = 3;

Element* e2 = (Element*)malloc(sizeof(Element));
e2->row = 1; e2->col = 2; e2->val = 5;
e1->next = e2;
e2->next = NULL;

display(e1);
return 0;
DSA Assignment - SLL Applications

Sample Output: (0, 1, 3)\n(1, 2, 5)

Time Complexity: O(n)

4. Large Number Arithmetic using SLL

#include <stdio.h>
#include <stdlib.h>

typedef struct Digit {


int num;
struct Digit* next;
} Digit;

void display(Digit* head) {


while (head) {
printf("%d", head->num);
head = head->next;
}
printf("\n");
}

int main() {
Digit* d1 = (Digit*)malloc(sizeof(Digit));
d1->num = 1;

Digit* d2 = (Digit*)malloc(sizeof(Digit));
d2->num = 2; d1->next = d2;

Digit* d3 = (Digit*)malloc(sizeof(Digit));
d3->num = 3; d2->next = d3; d3->next = NULL;

display(d1);
return 0;
}

Sample Output: 123

Time Complexity: O(n)

5. Reversing a Linked List using SLL

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node* next;
DSA Assignment - SLL Applications

} Node;

void reverse(Node** head) {


Node* prev = NULL;
Node* curr = *head;
Node* next = NULL;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
}

void display(Node* head) {


while (head) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}

int main() {
Node* n1 = (Node*)malloc(sizeof(Node));
n1->data = 1;

Node* n2 = (Node*)malloc(sizeof(Node));
n2->data = 2; n1->next = n2;

Node* n3 = (Node*)malloc(sizeof(Node));
n3->data = 3; n2->next = n3; n3->next = NULL;

display(n1);
reverse(&n1);
display(n1);
return 0;
}

Sample Output: 1 -> 2 -> 3 -> NULL\n3 -> 2 -> 1 -> NULL

Time Complexity: O(n)

You might also like