Linked several following steps- (we need to insert data

  Linked List   A linked
list is a way to store a collection of elements. Like an array these
can be character or integers. Each element in a linked list is stored in the
form of a node.   
 A
node is a collection of two sub-elements or parts. A data part
that stores the element and a next part that stores the link
to the next node. There is 2 part of the node of linked list- Data – Each node has a data element (value stored
in the node).Next- This part of the node contains
the address of the next node.So, a linked list is the
connection of the nodes, in which each node store the address of the next node.Example: In a system if we maintain a sorted list of IDs in an array
id.   Representation of
Linked List        Question-1: linked
lists implementation: Basic operations of
Linked list-Insertion – insert any new
node in the linked list.Deletion – Deletes the
node from the linked list.Print? It prints the whole
linked list.Search ? Searches an
element using the given key.  In C++, we create a node by using the structure
called Node. It contains two field one is the data to store the value of the
node and the other part of the node contains the pointer of the node type so it
stores the address of the next node.    //Create
a node with 2 field , 1- data and 2- pointer to store the address of the next
node of the Linked liststruct Node { 
  int
data; 
  struct Node * next;}; //Declare
head as a global pointer to keep the address of first node in the Linked Liststruct Node *
head = NULL;  Insertion OperationThis operations performs in several following
steps- (we need to insert data between leftNode and rightNode in the linked
list) Step-1:
Create a new node called temp node, and assign data
to be inserted as the data part of the temp node-                                     Temp->data = newData;And assign the next pointer with NULL.                                    Temp->next = NULL; Step-2: Now
assign the next pointer of the temp node with the address of the right node as:                                    Temp->next = rightNode;It should look like this ?Step-3:
Now, assign the next pointer of the leftNode by the
address of the temp node as:leftNode -> next =
temp;Now finally the linked list looks like this – void insert(int
x){  
  
  struct Node * temp = new Node; 
  temp -> data =
x; 
  temp -> next =
head; 
  head =
temp;}  Deletion OperationDeletion operation also performed in
several steps-(target node to be deleted) Step-1:  Assign the left node (previous) next pointer
with the address of the rightNode of the target node, as:                                    leftNode
-> next = targetNode -> next; This will remove the link that was
pointing to the target node. Step-2:
Now remove the link between target node and the right node as:                                   targetNode -> next = NULL  We need to use the deleted node. We can keep that
in memory otherwise we can simply deallocate memory and wipe off the target
node completely.  //position
= nvoid deleteNode(int
n){  
  
  //Create a node a leftNode of the
target node 
  struct Node * temp1 = head;  
  
  //goto n-1 th node 
  for(int i=0; i next;  
      
  //create a new node as the target
node 
    struct Node * temp2 = temp1 -> next;  
      
  //Fix the link(left node to the right
node of the target node(temp2)) 
  //and make the target node next
pointer as NULL 
      temp1 -> next = temp2 -> next;  
      
      delete
temp2; 
  }}  Print Operation-Step-1: Create a temp node and initialize with the head pointer i.e. the address
of the first node.Step-2: Now traverse the temp node and print data of the current node.  void printLL(){ 
  struct Node * temp = head; 
  //Traverse 
  while(temp
!= NULL){ 
      coutdata; 
      temp = temp -> next; 
  }} Note- Search operation is similar
as print operation , In search instead of the print just compare the data with
the key provided to search. What are the
possible variations of linked lists? Provide brief description of at least 2 of
them.Types of Linked List-Singly Linked List –
Can be traversed in one direction only.Doubly Linked List –
can be navigated in both direction i.e forwad and backward.Circular
Linked List – Last node of the linked list
has the address of the first element so than we can go from last node to first
node(in circular manner).Singly Linked List:
Each
node has a data part and a pointer to keep the address of the next node. It can
be navigated in one direction-   Head pointer contains the address of the first node
and basic operation performed on singly linked list discussed above (with
implementation).Doubly Linked List: In this type of linked list
each node contains tree part,                         Part-1(prev pointer):
contains the address of the previous node.                        Part-2(data): contains
the data(value).                        Part-3(next pointer):
contains the address of the next node.     Node is implemented as: struct Node { 
  int
data; 
  struct Node * next; 
  struct Node * prev;}; //Three node of doubly linked list created as:/*
Initialize nodes */struct node
*head;struct node
*one = NULL;struct node
*two = NULL;struct node
*three = NULL; /*
Allocate memory */one =
malloc(sizeof(struct node));two =
malloc(sizeof(struct node));three =
malloc(sizeof(struct node)); /*
Assign data values */one->data
= 1;two->data
= 2;three->data
= 3; /*
Connect nodes */one->next
= two;one->prev
= NULL; two->next
= three;two->prev
= one; three->next
= NULL;three->prev
= two; /* Save
address of first node in head */head =
one;   What is the
difference between an array and a linked list? Discuss 1 example from your work
or previous course work experience where linked lists can be useful.

 

Array

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

Linked
List

Definition
 

Collection of data elements having the same data
type.

Ordered
collection of elements dissimilar data type.

Access
of elements
 

Random
access, we can access the element of the array randomly by index number of
that element.

We
can’t access the elements randomy we need to traverse to that element since
we have address of first element only.

Memory
Structure
 

All
the elements are stored in memory in contiguous manner.

Elements
of linked list can be stored anywhere in memory, as each node has the address
of the next node.

Insertion
& Deletion
 

It
takes more time in array as the elements stored in contiguous manner in
memory.

Easy
and efficient as we only need to add new element and modify the address of 2 nodes.

Memory
Allocation
 

Static
memory allocation.

Memory
allocated dynamically in linked list.

 ExampleProblem 1: Suppose
you need to program an application that has a pre-defined number of categories,
but the exact items in each category is unknown. Solution:
Pre-defined number of categories implies that we can use a simple static
structure like array to represent the categories. Since we do not know the
number of items in each category, we can represent items in each category using
a linked list. So what we need is an array of linked lists