Algorithm CC0007: College of Computer Studies

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

College of Computer Studies

Algorithm
CC0007

Machine Problem # 2C

Submitted by:

ESPERO, JUNELLE BAUTISTA, ISHMAEL

ALTARES, CYRIL VELASCO, ELIJAH

Submitted to:

DR. HADJI J. TEJUCO


Professor

May 12, 2021

1
Table of Contents
Introduction............................................................................................................................3
Algorithm................................................................................................................................3
Screen Output and User’s manual...........................................................................................4
Test Cases...............................................................................................................................5
Source Code...........................................................................................................................6
Conclusion...............................................................................................................................6
References............................................................................................................................12

2
Introduction
This program implements Bubble Sort Algorithm, Functions, and File Handling on a
Doubly Linked List using C++. The program should be able to accept a number of inputted data,
and using those data, create a Doubly Linked list, display the unsorted list and then perform
Bubble Sort on the linked list, and then display again.

Algorithm

3
Screen Output and User’s manual

FIGURE 1: User input prompt

This is the part where the program asked for the limit and the integer data to be stored in the
linked list.

Figure 2: Table of unsorted Linked List

This displays the Address, Previous Address, data, and the Next Address details of each node.
This follows the sequence as the linked list was created, before Bubble Sort was performed.

4
Figure 3. Table of Bubble Sorted Linked List

This displays the Address, Previous Address, data, and the Next Address details of each node.
This follows the sequence after Bubble Sort was performed.

Test Cases
Positive Test

Test Case Test Test Steps Test Data Expected Actual Result Pass/Fail
ID Scenario Result
1 The user is 1. Run the Input data The user The program Pass
inputting application should see shows the
value to using the output values
the codio,
program (command Unsorted: Unsorted:
use) 239710 239710 239710
2. From the
menu BubbleSorted BubbleSorted
select : :
choice. 012379 012379
Negative Test

Test Case Test Test Steps Test Data Expected Actual Result Pass/Fail
ID Scenario Result
1 The user is 1. Run the Input The user The program Fail
inputting application should see shows the
value to using the output value
the codio,
program (command Unsorted: Unsorted:
use) 239710 239710 239710
2. From the
menu BubbleSorted BubbleSorted
select : :
choice. 012379 239710

5
Source Code
#include <iostream>

using namespace std;

6
struct Node {

int data;

struct Node* next;

struct Node* prev;

};

void List(struct Node** head, int new_data);

void bubbleSort(struct Node* node, int size);

void displayList(struct Node* node);

//insert a new node at the end of the list

void List(struct Node** head, int new_data)

//allocate memory for node

struct Node* newNode = new Node;

struct Node* last = *head; //set last node value to head

//set data for new node

7
newNode->data = new_data;

//new node is the last node , so set next of new node to null

newNode->next = NULL;

//check if list is empty, if yes make new node the head of list

if (*head == NULL) {

newNode->prev = NULL;

*head = newNode;

return;

//otherwise traverse the list to go to last node

while (last->next != NULL)

last = last->next;

//set next of last to new node

last->next = newNode;

//set last to prev of new node

newNode->prev = last;

8
return;

void bubbleSort(struct Node* node, int size)

//Node current will point to head

struct Node *current = node, *index = NULL;

int temp;

if(node == NULL) {

return;

else {

while(current != NULL) {

//Node index will point to node next to current

index = current->next;

while(index != NULL) {

//If current node's data is greater than index's node data, swap the data between
them

if(current->data > index->data) {

temp = current->data;

9
current->data = index->data;

index->data = temp;

index = index->next;

current = current->next;

// This function prints contents of linked list starting from the given node

void displayList(struct Node* node) {

Node* temp = node;

cout << "\n\n ADDRESS PREVIOUS DATA NEXT" << endl;

while(temp!= NULL){

if (temp -> prev == 0){

cout << &temp-> data << " ";

cout << temp -> prev << " ";

cout << temp-> data << " ";

cout << temp -> next << " ";

10
temp = temp-> next;

else{

cout << &temp-> data << " ";

cout << temp -> prev << " ";

cout << temp-> data << " ";

cout << temp -> next << " ";

temp = temp-> next;

cout << endl;

cout << endl;

//main program

int main() {

11
struct Node *head = NULL;

int size;

int num[size] = {};

cout << "Enter the number of elements of the array: ";

cin >> size;

cout << "Enter " << size << " elements: ";

for (int i = 0; i < size; ++i) { // store input from user to array

cin >> num[i];

for (int i = 0; i < size; ++i) { // store input from user to array

List(&head, num[i]);

displayList(head);

cout << endl << endl;

bubbleSort(head, size);

12
displayList(head);

return 0;

Conclusion
Doubly Linked List is one of the types of a linked list and for this program, the data
saved in each node was assigned with the use of a for loop and an array. This just means that
even though the double linked list is the main data structure that the program requires, making
use of other data structures and different types of loops can help implement the linked list.
Bubble Sort and other sorting algorithms also make use of combinations of different
programming concepts and techniques to produce more efficient and accurate programs.

References

GeeksForGeeks(www.geeksforgeeks.org)

W3schools(www.w3schools.com)

Tutorialspoint(www.tutorialspoint.com)

13

You might also like