0% found this document useful (0 votes)
4 views5 pages

Portfolio Chapter 5

Uploaded by

duttapratim515
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
4 views5 pages

Portfolio Chapter 5

Uploaded by

duttapratim515
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 5

A linked list is a linear data structure, in which the elements are not stored at

contiguous memory locations. The elements in a linked list are linked using
pointers. In simple words, a linked list consists of nodes where each node contains
a data field and a reference(link) to the next node in the list.

Contiguous memory allocation is a classical memory allocation model. Here, a system


assigns consecutive memory blocks (that is, memory blocks having consecutive
addresses) to a process. Contiguous memory allocation is one of the oldest memory
allocation methods.

Types Of Linked List:


1. Singly Linked List
It is the simplest type of linked list in which every node contains some data and a
pointer to the next node of the same data type.

The node contains a pointer to the next node means that the node stores the address
of the next node in the sequence. A single linked list allows the traversal of data
only in one way.
--------------- ----------- ---------
head -> - a next 1002- -> - b 1003 - -> - c 1004 - -> NULL
- data 1001 - - 1002 - - 1003 - 1004
------------------ ------------ ----------
// C++ program to illustrate creation
// and traversal of Singly Linked List

#include <bits/stdc++.h>
using namespace std;

// Structure of Node
class Node {
public:
int data;
Node* next;
};

// Function to print the content of


// linked list starting from the
// given node
void printList(Node* n)
{

// Iterate till n reaches NULL


while (n != NULL) {

// Print the data


cout << n->data << " ";
n = n->next;
}
}

// Driver Code
int main()
{
Node* head = NULL;
Node* second = NULL;
Node* third = NULL;

// Allocate 3 nodes in the heap


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

// Assign data in first node


head->data = 1;

// Link first node with second


head->next = second;

// Assign data to second node


second->data = 2;
second->next = third;

// Assign data to third node


third->data = 3;
third->next = NULL;

printList(head);

return 0;
}

Time Complexity: O(N)

2. Doubly Linked List


A doubly linked list or a two-way linked list is a more complex type of linked list
that contains a pointer to the next as well as the previous node in sequence.

Therefore, it contains three parts of data, a pointer to the next node, and a
pointer to the previous node. This would enable us to traverse the list in the
backward direction as well.

// C++ program to illustrate creation


// and traversal of Doubly Linked List

#include <bits/stdc++.h>
using namespace std;

// Doubly linked list node


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

// Function to push a new element in


// 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;

// Make next of new node as


// head and previous as NULL
new_node->next = (*head_ref);
new_node->prev = NULL;

// Change prev of head node to


// the 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 traverse the Doubly LL


// in the forward & backward direction
void printList(Node* node)
{
Node* last;

cout << "\nTraversal in forward"


<< " direction \n";
while (node != NULL) {

// Print the data


cout << " " << node->data << " ";
last = node;
node = node->next;
}

cout << "\nTraversal in reverse"


<< " direction \n";
while (last != NULL) {

// Print the data


cout << " " << last->data << " ";
last = last->prev;
}
}

// Driver Code
int main()
{
// Start with the empty list
Node* head = NULL;

// Insert 6.
// So linked list becomes 6->NULL
push(&head, 6);

// Insert 7 at the beginning. So


// linked list becomes 7->6->NULL
push(&head, 7);

// Insert 1 at the beginning. So


// linked list becomes 1->7->6->NULL
push(&head, 1);

cout << "Created DLL is: ";


printList(head);
return 0;
}

The time complexity of the push() function is O(1) as it performs constant-time


operations to insert a new node at the beginning of the doubly linked list. The
time complexity of the printList() function is O(n) where n is the number of nodes
in the doubly linked list. This is because it traverses the entire list twice, once
in the forward direction and once in the backward direction. Therefore, the overall
time complexity of the program is O(n).

3. circular linked list

3. Circular Linked List


A circular linked list is that in which the last node contains the pointer to the
first node of the list.

While traversing a circular linked list, we can begin at any node and traverse the
list in any direction forward and backward until we reach the same node we started.
Thus, a circular linked list has no beginning and no end.

// C++ program to illustrate creation


// and traversal of Circular LL

#include <bits/stdc++.h>
using namespace std;

// Structure for a node


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

// Function to insert a node at the


// beginning of Circular LL
void push(Node** head_ref, int data)
{
Node* 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;
}

// For the first node


else
ptr1->next = ptr1;

*head_ref = ptr1;
}

// Function to print nodes in the


// Circular Linked List
void printList(Node* head)
{
Node* temp = head;
if (head != NULL) {
do {

// Print the data


cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
}
}

// Driver Code
int main()
{
// Initialize list 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\n ";

// Function call
printList(head);

return 0;
}

Insertion at the beginning of the circular linked list takes O(1) time complexity.
Traversing and printing all nodes in the circular linked list takes O(n) time
complexity where n is the number of nodes in the linked list.
Therefore, the overall time complexity of the program is O(n).

You might also like