0% found this document useful (0 votes)
3 views

Lab-Activity-8-Linked-List

The document is a laboratory report on C++ linked lists prepared for an engineering course. It includes multiple examples of linked list implementations, such as singly linked lists, doubly linked lists, and circular linked lists, along with observations and outputs for each example. The report demonstrates various operations like insertion, deletion, and traversal of linked lists.

Uploaded by

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

Lab-Activity-8-Linked-List

The document is a laboratory report on C++ linked lists prepared for an engineering course. It includes multiple examples of linked list implementations, such as singly linked lists, doubly linked lists, and circular linked lists, along with observations and outputs for each example. The report demonstrates various operations like insertion, deletion, and traversal of linked lists.

Uploaded by

jaxgaming43
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

UNVERSITY OF THE EAST

College of Engineering
Computer Engineering Department

NCP 1203
Data Structure and Algorithm

Laboratory Report for Experiment No. 8

C++ Linked List

Prepared for:

ENGR. JOHN VINCENT R. TRINIDAD


Faculty, CpE Department

Prepared by:

RANILO JOHN M. DELOS ANGELES


NCP 1203 – 1CPE-2A

MAY 7, 2024
EXAMPLE #1:
#include<iostream>
using namespace std;

class Node
{
public:
int data;
Node *next;
};

int main()
{
Node* head = NULL;
Node* second = NULL;
Node* third = NULL;
head = new Node();
second = new Node();
third = new Node();

head->data = 1;
head->next = second;

second->data = 2;

second->next = third;

third->data = 3;

return 0;
}

OUTPUT:

OBSERVATION:
My execution observation of Example #1 Is that first, there’s no output. But
if we will read the code, there are allocated and there are assigned data on
each nodes. And first to second nodes are linked
EXAMPLE #2:
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
};

void printList(Node *n)


{
while (n != NULL)
{
cout << n->data << endl;
n = n->next;
}
}
int main()
{
Node* head = NULL;
Node* second = NULL;
Node* third = NULL;

head = new Node();


second = new Node();
third = new Node();

head->data = 1;
head->next = second;
second->data = 2; //assign data to second node
second->next = third;

third->data = 3; //assign data to third node


third->next = NULL;

printList(head);
system("pause");
return 0;
}

OUTPUT:
OBSERVATION:
Now same observation from Example #1 but there are now assign numbers on each data nodes,
head is = 1, second is = 2, third = 3. then each data assigns to each other and links. Head links to
Second then links ot Third, Then it prints head.

EXAMPLE #3:

#include <iostream>
#include <cmath>
using namespace std;

class Node
{
public:
int data;
Node *next;
};

/
void push(Node** head_ref, int new_data)
{
Node* new_node = NULL;
new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

/* Given a node prev_node, insert a new node after the given prev_node */
void insertAfter(Node* prev_node, int new_data)
{
if (prev_node == NULL)
{
cout << "the given previous node cannot be NULL" << endl;
return;
}

Node* new_node = NULL;


new_node = new Node();
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}

void append(Node** head_ref, int new_data)


{
Node* new_node = NULL;
new_node = new Node();
Node *last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return;
}

void printList(Node *node)


{
while (node != NULL)
{
cout << node->data << "->";
node = node->next;
}
cout << "NULL" << endl;
}

int main()
{
Node* head = NULL;
head = new Node();

head->data = 5;

append(&head, 6);

push(&head, 1);
append(&head, 4);

insertAfter(head->next, 8);

cout << "Created Linked list is:";


printList(head);
system("pause");

return 0;
}

OUTPUT:
OBSERVATION
This program is very different from the first two, first if else logic statement is
applied where linked lists would be arranged 1>5>6>4 and then NULL

EXAMPLE #4:
#include<iostream>
using namespace std;

class Node
{
public:
int data;
Node *next;
};

void push(Node** head_ref, int new_data)


{
Node* new_node = NULL;
new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

void deleteNode(Node **head_ref, int key)


{
Node* temp = *head_ref, *prev;

if (temp != NULL && temp->data == key)


{
*head_ref = temp->next;
free(temp);
return;
}

while (temp != NULL && temp->data != key)


{
prev = temp;
temp = temp->next;
}

if (temp == NULL)
return;
prev->next = temp->next;
free(temp);
}

void printList(Node *node)


{
while (node != NULL)
{
cout << node->data << "->";
node = node->next;
}
cout << "NULL" << endl;
}

/* Driver program to test above functions*/


int main()
{
/* Start with the empty list */
Node* head = NULL;
head = new Node();
head->data = 7;
push(&head, 1);
push(&head, 3);
push(&head, 2);

cout << "Created Linked List: ";


printList(head);
deleteNode(&head, 1);
cout << "Linked List after Deletion of Node with value of 1: ";
printList(head);
system("pause");
return 0;
}
OUTPUT:

OBSERVATION:
In my observation, this is the same as last example but the difference is there is a deletion
option on the single linked list.

EXAMPLE #5:
// A complete working C++ program to demonstrate all
// insertion before a given node
#include <iostream>
using namespace std;

// A linked list node


class Node {
public:
int data;
Node* next;
Node* prev;
};

/* Given a reference (pointer to pointer) to the head of a list


and an int, inserts a new node on the front of the list. */
void push(Node** head_ref, int new_data) {
Node* new_node = NULL;
new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
new_node->prev = NULL;

if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}

/* Given a node as next_node, insert a new node before the given node */
void insertBefore(Node** head_ref, Node* next_node, int new_data) {
/*1. check if the given next_node is NULL */
if (next_node == NULL) {
printf("the given next node cannot be NULL");
return;
}
/* 2. allocate new node */
Node* new_node = NULL;
new_node = new Node();

/* 3. put in the data */


new_node->data = new_data;

/* 4. Make prev of new node as prev of next_node */


new_node->prev = next_node->prev;

/* 5. Make the prev of next_node as new_node */


next_node->prev = new_node;

/* 6. Make next_node as next of new_node */


new_node->next = next_node;

/* 7. Change next of new_node's previous node */


if (new_node->prev != NULL)
new_node->prev->next = new_node;
/* 8. If the prev of new_node is NULL, it will be the new head node */
else
(*head_ref) = new_node;

// This function prints contents of linked list starting from the given node
void printList(Node* node) {
Node* last;
last = new Node();
cout << "Traversal in forward direction: ";
while (node != NULL) {
cout << node->data << "->" ;
last = node;
node = node->next;
}
cout << "NULL" << endl;

cout << "Traversal in reverse direction: ";


while (last != NULL) {
cout << last->data << "->" ;
last = last->prev;
}
cout << "NULL" << endl;
}

/* Driver program to test above functions*/


int main()
{
/* Start with the empty list */
Node* head = NULL;
head = new Node();
head->data = 7;
push(&head, 1);
push(&head, 4);
// Insert 8, before 1. So linked list becomes 4->8->1->7->NULL
insertBefore(&head, head->next, 8);

cout << "Created Doubly Linked List is: " << endl;
printList(head);
system("pause") ;

OUTPUT:

OBSERVATION:
This program is different as well, since it is a double linked list where there’s insertion and
involves adding a new node before a given node.
EXAMPLE #6:
// C++ program to delete a node from
// Doubly Linked List
#include <iostream>
using namespace std;

/* a node of the doubly linked list */


class Node
{
public:
int data;
Node* next;
Node* prev;
};

/* Function to delete a node in a Doubly Linked List.


head_ref --> pointer to head node pointer.
del --> pointer to node to be deleted. */
void deleteNode(Node** head_ref, Node* del)
{
/* base case */
if (*head_ref == NULL || del == NULL)
return;

/* If node to be deleted is head node */


if (*head_ref == del)
*head_ref = del->next;

/* Change next only if node to be


deleted is NOT the last node */
if (del->next != NULL)
del->next->prev = del->prev;

/* Change prev only if node to be


deleted is NOT the first node */
if (del->prev != NULL)
del->prev->next = del->next;

/* Finally, free the memory occupied by del*/


free(del);
return;
}

/* UTILITY FUNCTIONS */
/* Function to insert a node at the
beginning of the Doubly Linked List */
void push(Node** head_ref, int new_data)
{
/* allocate node */
Node* new_node = new Node();

/* put in the data */


new_node->data = new_data;

/* since we are adding at the begining,


prev is always NULL */
new_node->prev = NULL;

/* link the old list off the new node */


new_node->next = (*head_ref);

/* change prev of head node to new node */


if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;

/* move the head to point to the new node */


(*head_ref) = new_node;
}

/* Function to print nodes in a given doubly linked list


This function is same as printList() of singly linked lsit */
void printList(Node* node)
{
while (node != NULL)
{
cout << node->data << "->";
node = node->next;
}
cout << "NULL" << endl;
}

/* Driver code*/
int main()
{
/* Start with the empty list */
Node* head = NULL;
/* Let us create the doubly linked list 10<->8<->4<->2 */
push(&head, 2);
push(&head, 4);
push(&head, 8);
push(&head, 10);

cout << "Original Linked list ";


printList(head);

/* delete nodes from the doubly linked list */


deleteNode(&head, head); /*delete first node*/
deleteNode(&head, head->next); /*delete middle node*/
deleteNode(&head, head->next); /*delete last node*/

/* Modified linked list will be NULL<-8->NULL */


cout << "\nModified Linked list ";
printList(head);
system("pause");
return 0;
}

OUTPUT:
OBSERVATION:
Now this really is the same since it’s still a double deletion linked lists but there’s
deleteNode from first, middle and last mode

EXAMPLE #7:
// C++ program to implement
// the above approach
#include <iostream>
using namespace std;

/* structure for a node */


class Node {
public:
int data;
Node *next;
};

/* Function to insert a node at the begining of a Circular


linked list */
void push(Node **head_ref, int data) {
Node *ptr1 = NULL;
ptr1 = new Node();
Node *temp = *head_ref;
ptr1->data = data;
ptr1->next = *head_ref;

/* If linked list is not NULL then set the next of last node */
if (*head_ref != NULL)
{
while (temp->next != *head_ref)
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1; /*For the first node */
*head_ref = ptr1;
}

/* Function to print nodes in a given Circular linked list */


void printList(Node *head) {
Node *temp = head;
if (head != NULL)
{
do
{
cout << temp->data << "->";
temp = temp->next;
}
while (temp != head);
}
cout << "\b\b " << endl;
}

/* Driver program to test above functions */


int main() {
/* Initialize lists as empty */
Node *head = NULL;

/* Created linked list will be 11->2->56->12 */


push(&head, 12);
push(&head, 56);
push(&head, 2);
push(&head, 11);

cout << "Contents of Circular Linked List: " ;


printList(head);
system("pause");

return 0;
}

OUTPUT:

OBSERVATION:
In this observation, this is the observation where the last node points back to the
first node forming a circular structure useful in certain applications, such as
creating a circular buffer or implementing a circular queue.

MORE EXAMPLES:

EXAMPLE #8: SINGLY LINKED LIST EXAMPLE

#include <iostream>
#include <stdlib.h>
using namespace std;

struct node
{int data;
node* next;
};
int x,sum;
node* n;
node* t;
node* h;
main()
{ n= new node;
t=n;
h=n;
for (x=1;x<=5;x++)
{
cout<<"enter a data: ";
cin>>n->data;
sum= n->data + sum;
n= new node;
t->next=n;
t=n;

}
for (x=1;x<=5;x++)
{
cout<<h->data<<endl;
h=h->next;

cout<<endl<<"sum="<<sum;
}
OUTPUT:

OBSERVATION:

This is a single linked list where logic applied every numbers inserted or input
will be into total of sum.

EXAMPLE #9: DOUBLY LINKED LIST EXAMPLE


#include <iostream>
#include <stdlib.h>
using namespace std;

struct node
{int data;
node* next;
node* prev;
};
int x,sum;
node* n;
node* t;
node* h;
main()
{ n= new node;
t=n;
h=n;
for (x=1;x<=5;x++)
{
cout<<"enter a data: ";
cin>>n->data;
sum= n->data + sum;
n= new node;
t->next=n;
n->prev=t;
t=n;

}
for (x=1;x<=5;x++)
{
cout<<h->data<<endl;
h=h->next;

for (x=1;x<=5;x++)
{ n=n->prev;
cout<<n->data<<endl;

}
cout<<endl<<"sum="<<sum;
}

OUTPUT:

OBSERVATION:
Same as last number, where node is easily applied as well and you must input
the numbers and it will be total of the sum.

You might also like