Lab-Activity-8-Linked-List
Lab-Activity-8-Linked-List
College of Engineering
Computer Engineering Department
NCP 1203
Data Structure and Algorithm
Prepared for:
Prepared by:
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;
};
head->data = 1;
head->next = second;
second->data = 2; //assign data to second node
second->next = third;
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;
}
int main()
{
Node* head = NULL;
head = new Node();
head->data = 5;
append(&head, 6);
push(&head, 1);
append(&head, 4);
insertAfter(head->next, 8);
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;
};
if (temp == NULL)
return;
prev->next = temp->next;
free(temp);
}
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;
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();
// 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 << "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;
/* 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();
/* 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);
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;
/* 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;
}
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:
#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.
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.