0% found this document useful (0 votes)
109 views16 pages

C Programs for List Manipulation and Memory Management

This program adds two integers represented as linked lists. It takes in two numbers as input, creates linked lists for each number by storing each digit as a node, reverses the lists, and then performs addition by iterating through the lists node-by-node, calculating the sum of corresponding digits and carrying over values. The output is a new linked list representing the sum.

Uploaded by

Tiny G
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)
109 views16 pages

C Programs for List Manipulation and Memory Management

This program adds two integers represented as linked lists. It takes in two numbers as input, creates linked lists for each number by storing each digit as a node, reverses the lists, and then performs addition by iterating through the lists node-by-node, calculating the sum of corresponding digits and carrying over values. The output is a new linked list representing the sum.

Uploaded by

Tiny G
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

Tejada, Maxine Claire T.

BSCS 2-1N

1. Write a program that will initially create an UNSORTED list containing the following and then run
the program:

Mitch

Diane

Jack

Robbie

Katherine

Program:

#include <stdio.h>

#include<string.h>

int main(){

char charr[5][10] = {

"Mitch",

"Diane",

"Jack",

"Robbie",

"Katherine"

};

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

printf("%s \t at index =%d \t address = %u\n", charr + i,i, charr + i);

Output display:
Answer and explain the following questions below:

a) If we check the memory location of each element in the list, what would it be? What
index represent each element?

Memory location of each element in the list:


Mitch – 6487520

Diane – 6487530

Jack – 6487540

Robbie – 6487550

Katherine – 6487560

Index of each element in the list:


Mitch - 0

Diane – 1

Jack – 2

Robbie – 3

Katherine – 4

b) What happens if we add Morrie in the list? What will be its index value?

Answer:

If we add Morrie in the list, the list will be added to the array. And the index value of Morrie
will be 5.

c) What does the new list look like? Where do you think Morrie should be placed and why?

The original list:


Mitch

Diane

Jack

Robbie

Katherine

The updated list:


Mitch

Diane

Jack

Robbie

Katherine

Morrie

Answer:

I placed Morrie to the last element of the list since we’re just inserting a new element to the
unsorted list.

With the same list above (with Morrie added), delete/remove Jack. Answer and explain the
following questions below:

a) What is the new list? Identify the elements of the list and its index.

The new list:


Mitch

Diane

Robbie

Katherine

Morrie

b) What happened to the former location occupied by Jack?

Answer:

The location of Jack is occupied by Robbie and the list adjusted. From the last element up to
Robbie, they adjusted and move to left to occupy the memory location of Jack.

2. Write a program that will initially create a SORTED list containing the following and then run the
program:

Diane
Jack

Katherine

Mitch

Robbie

Program:

#include <stdio.h>

#include<string.h>

int main()

char charr[5][10] = {

"Diane",

"Jack",

"Katherine",

"Mitch",

"Robbie"

};

for(int i = 0; i < 5; i++)

printf("%s \t at index =%d \t address = %u\n", charr + i,i, charr + i);

}
Output display:

Answer and explain the following questions below:

a) If we check the memory location of each element in the list, what would it be? What index
represent each element?

Memory location of each element in the list:


Diane - 6487520

Jack – 6487530

Katherine – 6487540

Mich – 6487550

Robbie – 6487560

Index of each element in the list:


Diane - 0

Jack – 1

Katherine – 2

Mich – 3

Robbie – 4

b) What happens if we add Morrie in the list? What will be its index value?

Answer:

If we add Morrie in the list, the list will be added to the array. And the index value of Morrie will
be 4.
c) What does the new list look like? Where do you think Morrie should be placed and why?

The original list:


Diane

Jack

Katherine

Mitch

Robbie

The updated list:


Diane

Jack

Katherine

Mitch

Morrie

Robbie

Answer:

I placed Morrie between Mitch and Robbie since we’re dealing to a sorted list. In that way,
we’re sorting the list in alphabetical.

With the same list above (with Morrie added), delete/remove Jack. Answer and explain the
following questions below:

a) What is the new list? Identify the elements of the list and its index.

The new list:


Diane

Katherine

Mitch

Morrie

Robbie

b) What happened to the former location occupied by Jack?

Answer:

The location of Jack is occupied by Katherine and the list adjusted. From the last element up to
Katherine, they adjusted and move to left to occupy the memory location of Jack.
3. In most personal computers, the largest integer is 32,767 and the largest long integer is
2,147,483,647. Some applications, such as the national debt, may require an unbounded
integer. One way to store and manipulate integers of unlimited size is by using a linked list. Each
digit is stored in a node of the list. For example, the figure below shows how we could store a
five-digit number in a linked list.

While the linked list in the figure is represented as moving from right to left, remember that
there is no physical direction in a linked list. We represent it this way to assist us in
understanding the problem.

To add two numbers, we simply add the corresponding digit in the same location in their
respective linked lists with the carry from the previous addition. With each addition, if the sum
is greater than 10, then we need to subtract ten and set the carry to one. Otherwise, the carry is
set to zero.

Write a program to add two integer linked lists. Design your solution so that the same logic adds
the first numbers (units’ position) as well as the rest of the number. In other words, do not have
special one-time logic for adding the units’ position.

Program:
#include<stdio.h>

#include<stdlib.h>

struct node{

int data;

struct node *link;

};

struct node* addNode(struct node*, int);

struct node* create(struct node*, int);

void print(struct node*);

struct node* reverse(struct node*);

struct node* push(struct node*, int);

struct node* addition(struct node*, struct node*);

int main(){

int val1, val2;

printf("Enter two number: ");


scanf("%d %d", &val1, &val2);

//first number

printf("First number: \n");

struct node* head_1 = NULL;

head_1 = create(head_1, val1);

print(head_1);

//second number

printf("\nSecond number: \n");

struct node* head_2 = NULL;

head_2 = create(head_2, val2);

print(head_2);

//will reverse the link

head_1 = reverse(head_1);

head_2 = reverse(head_2);

//will output the sum of the two numbers

struct node* head_3 = NULL;

head_3 = addition(head_1, head_2);

printf("\nSum: \n");

print(head_3);

//will add new node at the begnning of the linked list

//will return the address of the first node of the list, head

struct node* addNode(struct node* head, int num){

struct node* new_node = (struct node*)malloc(sizeof(struct node)); //creating


new node

new_node->data = num; //updating the data part of the new node

new_node->link = NULL; //updating the link part fo the new node


new_node->link = head; //connects the nodes

head = new_node; //head must be updated to point to the new node since it is
the first node

return head; //return the address of the head back

//store each digit to the linked list

struct node* create(struct node* head, int num){

while(num != 0){

head = addNode(head, num%10); //will pass the address of head and gets
one digit at a time

num = num/10; //updates the number

return head; //return the address of the head back

//prints the linked representation of the two numbers

void print(struct node* head){

struct node* temp = head; //pointer that will traverse the list

if(head == NULL)

printf("The list is empty.");

else{

//prints each digit of the number

while(temp->link != NULL){

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

temp = temp->link; //moves to the next node

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

}
//will reverse the list

struct node* reverse(struct node* head){

if(head == NULL)

return head;

struct node* current = NULL;

struct node* next = head->link;

head->link = NULL;

//loop that will reverse the list

while(next != NULL){

current = next;

next = next->link;

current->link = head;

head = current;

return head;

//will push the new node in front of the resultant list

struct node* push(struct node* head, int num){

//creating new node

struct node* new_ptr = (struct node*)malloc(sizeof(struct node));

new_ptr->data = num;

new_ptr->link = head;

//assigning head to the address of new pointer

head = new_ptr;

return head;

}
//add the two nodes

struct node* addition(struct node* head_1, struct node* head_2){

struct node* ptr_1 = head_1;

struct node* ptr_2 = head_2;

struct node* head_3 = NULL;

int carry = 0, sum;

//loops until ptr_1 and ptr_2 equal to NULL

while(ptr_1 || ptr_2){

sum = 0;

if(ptr_1)

sum += ptr_1->data;

if(ptr_2)

sum += ptr_2->data;

sum += carry;

carry = sum/10; //obtain the carry

sum = sum%10; //sum must be updated to one digit after carry

head_3 = push(head_3, sum);

if(ptr_1)

ptr_1 = ptr_1->link;

if(ptr_2)

ptr_2 = ptr_2->link;

if(carry)

head_3 = push(head_3, carry);

return head_3;

}
Output display:

4. Write a program in C to create and display a circular linked list.

Test Data :

Input the number of nodes : 3

Input data for node 1 : 2

Input data for node 2 : 5

Input data for node 3 : 8

Expected Output :

Data entered in the list are :

Data 1 = 2

Data 2 = 5

Data 3 = 8
Program:
#include<stdio.h>

#include<stdlib.h>

struct node{

int data;

struct node *link; //pointer to the next address

}*head; //address of thew first node

void create(int);

void print();

int main(){

int num_nodes;

head = NULL;

printf("Input the number of nodes : ");

scanf("%d", &num_nodes);

create(num_nodes); //passing the number of nodes

print(); //calling the function print to print the list

//create a circular linked lists with the specified number of node by the user

void create(int num_nodes){

int node_count;

int data;

struct node *prev_ptr;

struct node *new_node;

if(num_nodes >= 1){


head = (struct node *)malloc(sizeof(struct node));

printf("Input data for node 1 : ");

scanf("%d", &data);

//First node

head->data = data;

head->link = NULL;

//set previous pointer

prev_ptr = head;

//loop that will create the specified number of nodes

for(node_count = 2; node_count <= num_nodes; node_count++){

//create new node

new_node = (struct node *)malloc(sizeof(struct node));

printf("Input data for node %d : ", node_count);

scanf("%d", &data);

//initialize new node

new_node->data = data;

new_node->link = NULL;

//connects the new node to the circular linked list

prev_ptr->link = new_node;

//update previous node

prev_ptr = new_node;

}
//connects last node to first node

prev_ptr->link = head;

//prints the contents of the circular linked lists

void print(){

struct node *current;

int node_count = 1;

//check if the list is empty

if(head == NULL)

printf("The list is empty.");

else{

//assign the first node to current

current = head;

printf("\nData entered in the list are : \n");

//traverse the whole linked list

do{

printf("Data %d = %d\n", node_count, current->data);

current = current->link;

node_count++;

}while(current != head);

Output display:

You might also like