Docsity

Link List ( Data Structure and ALgorithms ), Exams of Data Structures and Algorithms

Partial preview of the text

Download Link List ( Data Structure and ALgorithms ) and more Exams Data Structures and Algorithms in PDF only on Docsity!

Link List

What are Linked

Lists

 (^) A linked list is a linear data structure.  (^) Nodes make up linked lists.  (^) Nodes are structures made up of data and a pointer to another node.  (^) Usually the pointer is called next.

Types of lists There are two basic types of linked list

Singly Linked list

Doubly linked list

Singly Linked List  (^) Each node has only one link part  (^) Each link part contains the address of the next node in the list  (^) Link part of the last node contains NULL value which signifies the end of the node

Basic Operations on a list

  • (^) Creating a List
  • (^) Inserting an element in a

list

  • (^) Deleting an element from a

list

  • (^) Searching a list
  • (^) Reversing a list

Creating a node / / A simple node of a linked list struct node{ int data; nodenext; }start; start=NULL ; //start points at the first node initialised to NULL at beginning

To be called from main() as:- void main() { node* ptr; int data; cin>>data; ptr=create (data); }

Inserting the node in a SLL

There are 3 cases here:-

Insertion at the beginning

Insertion at the end

Insertion after a particular

node

//if the list is empty void insert_beg(node* p) { node* temp; if(start==NULL) { start=p; cout<<”\nNode inserted successfully at the beginning”; } else { } temp=start; start=p; p-

next=tem p; //making new node }

void insert_end(node* p) { node *q=start; if(start==NULL) { start= p; cout< <”
nNode insert ed succes sfully at the end…!

Inserting after an element

Here we again need to do 2 steps :-
 Make the next pointer of the node to be inserted
point to the next node of the node after which
you want to insert the node
 Make the next pointer of the node after which
the node is to be inserted, point to the node to
be inserted

void insert_after(int c,node* p) { node* q; q=start; for( int i=1; i<c; i+ +) { q= q-

li nk;

i

Deleting a node in SLL

Here also we have three cases:-
 Deleting the first node
Deleting the last node
Deleting the intermediate
node