Data Structure

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Data Structure

Linked List
ScudComp
Data Structure refers to a data collection with well defined
operations a and behaviour or properties. The term List refers
to a linear collection of data. The List can be of sequential
type or non sequential type.
0 N-1

Like arrays, Linked List is a linear data structure. Unlike arrays,


linked list elements are not stored at a contiguous location; the
elements are linked using pointers. An array is called static data
structure.
Why Linked List?
Arrays can be used to store linear data of similar types, but arrays have the
following limitations.
1) The size of the arrays is fixed: So we must know the upper limit on the
number of elements in advance. Also, generally, the allocated memory is
equal to the upper limit irrespective of the usage.
2) Inserting a new element in an array of elements is expensive because the
room has to be created for the new elements and to create room existing
elements have to be shifted
Linked List
Unlike an array the number of elements need not be
predetermined. Linked List is of two types viz., Singly
lined list and doubly linked list. A singly lined list contains
Node with a single pointer pointing to the next node
whereas a double linked list has two pointers one pointing
to the previous and the other to the next.

Memory allocation: Each element stored in the memory is


given some memory. The process of giving memory is
called memory allocation.
The memory can be of static or dynamic.
Static allocation means having fixed amount of memory before actual
processing takes place so the number of elements to be stores should be
predefined. Variables of primitive data type are allocated memory using
this technique
.
Dynamic memory location is allocating memory location as and when
required.
Memory allocation in java takes place via new operator. The new
operator instantiates a class by allocating memory for a new object from
the heap memory. Heap memory is a memory pool area managed by
java to create an object dynamically.
Anatomy of a singly linked list:
Start is a special reference with stores the very first address of a linked list
The link/next is a reference that stores the address of the next node.
The last node will contain a null link

Start
4 nulll

1200
start Null

8 null

900
Insertion in a sorted List- in between
start
30
a l

16 190 40 25 75 4400 90 null

30 190 90 4400

10 null s
25
Insertion in a sorted List- in the end
start
30

16 190 40 90 75 4400 90 null

30 190 90 4400

5 null 78

nptr
Algorithm to insert an element in a singly linked list
Step
1. START
2. ptr =start
3. nptr = new node
4. if start = null then start =nptr
5 else if val <start.data then 5.1
5.1 save =start
5.1 start =nptr
6. else
6.1 save=start
6.2 ptr=starts link
6.3 Repeat steps 6.4 to 6.4.6 until ptr is not equal to null
6.4 if val is greater than saves data and val is less than ptrs data then 6.4.1 else 6.4.5
6.4.1 saves link is nptr
6.4.2 nptrs link is ptr
6.4.3 make ins to be true
6.4.4 break
6.4.5 save should be ptr
6.4.6 ptr = ptr’s link

6 If ins is false then saves link as nptr


Important notes
start =null
When we want to make start take a node’s address then you write
start =nptr
If you have to check the value of the node start is pointing then you
write
start.data or if there is a function then start.getdata()
If you want the new node to take the address of say start then you have
to write nptr.setlink(start)
Or if you want another memory location to store the address of say
nptr then
save.setlink(nptr) You can’t give save=nptr because nptr has two parts.
If you want a to get the link or move the address of a node to the
pointer then
ptr=ptr.getlink();
import java.util.*;
class Node
{
int data;
Node link;
Node()
{
data=0;
link=null;
}
Node(int d, Node n)
{
data=d;
link=n;}
int getData() void setLink(Node n)
{ {
return data; link=n;
} }
Node getLink() }
{
return link;
}
void setData(int d)
{
data=d;
}
import java.util.*;
class LinkedList
{
public static Scanner obj=new
Scanner(System.in);

Node start;
LinkedList()
{
start=null;
}
void insert(int val)
{
Node ptr, save=null;
boolean ins=false;
Node nptr=new Node(val, null);
if(start==null)
start=nptr;
else if(val<=start.getData())
{
nptr.setLink(start);
start=nptr;
}//closure for else if
else
{
save=start;
ptr=start.getLink();

while(ptr!=null)
{
if(val>=save.getData() && val<=ptr.getData())
{
save.setLink(nptr);
nptr.setLink(ptr);
ins=true;
break;
}//closure for if
else
{
save=ptr;
ptr=ptr.getLink();
}
} //closure of while
if(ins==false)
save.setLink(nptr);
}// closure for else
}
Algorithm To Print all the nodes

// to print all nodes


1. START
2. ptr=start
3. Repeat steps 4 and 5 until ptr=null
4. Print ptr.data
.5. ptr=ptr.link
6. end
//Method to print
void prn( )
{
Node ptr=start;
System.out.print(start.getData( )+”- ->”
ptr=start.getLink( );
while(ptr.getLink( ) !=null)
{
System.out.print(ptr.getData( )+”- ->”);
ptr=ptr.getLink( );
}
System.out.print(ptr.getData( ));
}
230 Start

12 1130 230 2300 280 null


30 33

230 30 1130
2300

Ptr
Delete a node in a list
Algorithm
Step
1. Start
2. save =start, ptr =start boolean res=false
3. If ptr=val then step 4 else 5
4. start=start.link
5. res=true
6. Exiting if construct

7. Repeat steps 8 until 6 until ptr=null


8. If ptr.data ==val then 9 break else 13
9. save=ptr
10.ptr=ptr.link
11.res =true
12.return
13.save=ptr
14.ptr=ptr.link
15.stop
boolean delete(int val)
{
Node ptr, save;
boolean ins=false;
save=start;
ptr=start.getLink();

if(start.getData()==val)
{ start=start.getLink();
ins=true;
}
else
{
while(ptr!= null)
{
if(ptr.getData()==val)
{
save.setLink(ptr.getLink());
ins=true;
}
else
save=ptr;
ptr=ptr.getLink();
}
}
return ins;
}
public static void main(String args[])
{
LinkedList l =new LinkedList();

int i,no;
for(i=0;i<5;i++)
{
System.out.print("Enter a no");
no=obj.nextInt();
l .insert(no);
}
l.display();
boolean x=l.delete(3);
if(x==true)
System.out.print("deleted");
else
System.out.print("not deleted");
l.display();
System.out.println();
While()
Menu - insert, delete, print , quit
Switch case to branch

You might also like