Differences Between Arrays and Linked Lists
Differences Between Arrays and Linked Lists
Tech IT sem-1
DATA STRUCTURES DATA STRUCTURES
Another drawback of arrays is that the elements in an array are stored a fixed distance apart, data next
and the insertion and deletion of elements in between require a lot of data movement.
Node
The linked list is the solution to overcome all these problems. A linked list using dynamic
memory management follows this principle allocate and use memory when you need it and linked lists do not necessarily contain consecutive memory locations. These data items can be
release it (free or deallocate) stored anywhere in the memory in a scattered manner.
To maintain the specific sequence of these data items, we need to maintain link(s) with a
A linked list is a very effective and efficient dynamic data structure for linear lists. Items may successor (and/or a predecessor). It is called as a linked list as each node is linked with its
be added or deleted from it at any position more easily as compared to arrays. A programmer successor (and/or predecessor).
does not need to worry about how many data items a program will have to store. This enables A linked list is an ordered collection of data in which each element (node)
the programmer to make effective use of the memory. contains a minimum of two values, data and link(s) to its successor (and/or predecessor). A
list with one link field using which every element is associated to its successor is known as a
Differences between arrays and linked lists singlylinked list (SLL). A link is made from each item to the next item in the list
Basis for
Array Linked list data 1 next data 2 next data3 NULL
Comparison
It is a consistent set of a fixed It is an ordered set comprising a HEAD Node1 Node2 Node3/TAIL
Basic memory for a node is dynamically allocated. Therefore, the number of elements that may be
number of data items. variable number of data items.
added to a list is limited only by the amount of memory available.
No need to specify; grow and shrink Declaration of the node is,
Size Specified during declaration.
during execution. class Node
Element location is allocated Element position is assigned during {
Storage Allocation Element_type data;
during compile time. run time.
Node next;
Order of the }
Stored consecutively Stored randomly
elements
Direct or randomly accessed, Sequentially accessed, i.e., Traverse Some of the basic operations that can be performed on a list are
Accessing the 1. Create a list
i.e., Specify the array index or starting from the first node in the list
element
subscript. by the pointer. 2. Search for an element in a list
3. Delete an element from a list ( at beg, at end, at given location)
Insertion and Slow relatively as shifting is
Easier, fast and efficient. 4. Add an element at a specified location of a list ( at beg, at end, at given location).
deletion of element required.
5. Sort a list
Searching Binary search and linear search linear search 6. Print a list
7. Determine the size or no of elements in a list
Memory required less More
Memory
Utilization
Ineffective Efficient Implementation of linked list
import java.util.*;
class MyNode
{
Scanner s=new Scanner(System.in); A node of the linked list will represent each term. A node will have 3 fields, which represent the
MyLinkedList l=new MyLinkedList(); coefficient and exponent of a term and a pointer to the next term
System.out.println("enter the size of the list");
l.create(s.nextInt());
For instance, the polynomial, say A 6x7 3x5 4x3 12 would be stored as
while(true)
{
System.out.println("enter your choice");
The polynomial B 8x5 9x4 2x2 10 would be stored as
ch=s.nextInt();
switch(ch)
{ Program for polynomial representation
case 1: l.insertB();
break; import java.util.*;
case 2: l.insertE(); class MyNode
break; {
case 3:System.out.println("enter the position"); int c,e;
l.insertP(s.nextInt()); MyNode next;
break; }
case 4:l.deleteB(); class MyLinkedList
break; {
case 5:l.deleteE(); Scanner s=new Scanner(System.in);
break; MyNode head,last;
case 6: System.out.println("emter the position"); MyLinkedList()
l.deleteP(s.nextInt()); {
break; head=last=null;
case 7:l.display(); }
break; void create(int n)
{
default:return; MyNode temp;
} for(int i=0;i<n;i++)
} {
} temp=new MyNode();
} System.out.println("enter coefficient and exponent");
temp.c=s.nextInt();
Advantages of linked lists: temp.e=s.nextInt();
temp.next=null;
Efficient implementation of insertion and deletion operations(they are
if(head==null)
performed at the complexity of O(1) where as in arrays O(n)). head=last=temp;
Efficient use of memory. else
Can dispose nodes after its use. {
Can expand the list in constant time without memory overhead last.next=temp;
last=temp;
Disadvantages: }
}
}
void add(MyLinkedList p1, MyLinkedList p2)
Applications of linked lists: {
MyNode temp1=p1.head;
Polynomial Representation
MyNode temp2=p2.head;
polynomial we want to represent using a linked list be A(x). It is expanded as,
MyNode temp3;
A(x) = k1xm kn-1 x2 + knx1
while(temp1!=null && temp2!=null)
where ki is a non-zero coefficient with exponent m such that m m 1 ... 2 1 0.
{ temp2=temp2.next;
temp3=new MyNode(); }
if(temp1.e==temp2.e) }
{ void mul(MyLinkedList p1, MyLinkedList p2)
temp3.c=temp1.c+temp2.c; {
temp3.e=temp1.e; MyNode temp1=p1.head;
temp3.next=null; MyNode temp2;
temp1=temp1.next;
temp2=temp2.next; MyNode temp3;
}
else if(temp1.e>temp2.e) while(temp1!=null)
{ {
temp3.c=temp1.c; temp2=p2.head;
temp3.e=temp1.e; while(temp2!=null)
temp3.next=null; {
temp1=temp1.next; temp3=new MyNode();
} temp3.c=temp1.c*temp2.c;
else temp3.e=temp1.e+temp2.e;
{ temp3.next=null;
temp3.c=temp2.c; if(head==null)
temp3.e=temp2.e; head=last=temp3;
temp3.next=null; else
temp2=temp2.next; {
} last.next=temp3;
if(head==null) last=temp3;
head=last=temp3; }
else temp2=temp2.next;
{ }
last.next=temp3; temp1=temp1.next;
last=temp3; }
} temp1=head;
} while(temp1!=null)
while(temp1!=null) {
{ temp2=temp1;
temp3=new MyNode(); temp3=temp1.next;
temp3.c=temp1.c; while(temp3!=null)
temp3.e=temp1.e; {
temp3.next=null; if(temp1.e==temp3.e)
last.next=temp3; {
last=temp3; temp1.c=temp1.c+temp3.c;
temp1=temp1.next; temp2.next=temp3.next;
} temp3=temp2.next;
while(temp2!=null) }
{ else
temp3=new MyNode(); {
temp3.c=temp2.c; temp2=temp3;
temp3.e=temp2.e; temp3=temp3.next;
temp3.next=null; }
last.next=temp3; }
last=temp3; temp1=temp1.next;
}
}
void display()
{
MyNode temp;
for(temp=head;temp!=null;temp=temp.next)
{
if(temp.next!=null)
System.out.print(temp.c+"x^"+temp.e+"+"); Implementation o sparse matrix representation:
else class MyNode
System.out.println(temp.c+"x^"+temp.e); {
} int r,c,e;
} MyNode next;
} }
class Poly class SparseList
{ {
public static void main(String[] args) public static void main(String[] args)
{ {
Scanner s=new Scanner(System.in); int s[][]={{0,0,2},{0,0,0},{3,0,0}};
MyLinkedList p1=new MyLinkedList(); MyNode head=null;
MyLinkedList p2=new MyLinkedList(); MyNode last=null;
MyLinkedList p3=new MyLinkedList(); MyNode temp;
MyLinkedList p4=new MyLinkedList(); for(int i=0;i<3;i++)
System.out.println("enter no.of terms in the polynomial equation1"); {
p1.create(s.nextInt()); for(int j=0;j<3;j++)
System.out.println("enter no.of terms in the polynomial equation2"); {
p2.create(s.nextInt()); if(s[i][j]!=0)
p1.display(); {
p2.display(); temp=new MyNode();
p3.add(p1,p2); temp.r=i;
p3.display(); temp.c=j;
p4.mul(p1,p2); temp.e=s[i][j];
p4.display(); temp.next=null;
} if(head==null)
} head=last=temp;
else
Sparse Matrix Representation {
last.next=temp;
Representation of sparse matrix using linked list: last=temp;
Sparse matrix is a matrix in which most of the elements are zeros. A matrix with a maximum }
of 1/3 non-zero elements. We can efficiently perform the operations with matrix }
representation. But this representation uses more space for storing zeros. }
We have studied the sparse matrix representation using arrays, which is a sequential }
allocation scheme. Representing a sparse matrix sequentially allows faster execution of for(temp=head;temp!=null;temp=temp.next)
matrix operations, and it is more storage efficient than linked allocation schemes. However, it System.out.print(temp.r+"|"+temp.c+"|"+temp.e+"->");
has many shortcomings. The insertion and deletion of elements need the movement of many System.out.println("null");
other elements. In applications with frequent insertions and deletions, a linked representation }
can be adopted. }
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
CircularLinkedList c=new CircularLinkedList();
Implementation of circular linked list: c.create(s.nextInt());
c.display();
import java.util.Scanner; }
class MyNode }
{
int data;
MyNode next; Linked Stacks:
} we have implemented stacks using arrays. However, an array implementation has certain limitations. One
class CircularLinkedList of the limitations is that such a stack cannot grow or shrink dynamically. This drawback can be overcome
{ by using linked implementation.
Scanner s=new Scanner(System.in); A stack implemented using a linked list is also called linked stack. Each element of the stack
MyNode head; will be represented as a node of the list. The addition and deletion of a node will be only at
CircularLinkedList() one end. The last element given is at the top of the stack, and it will be pointed to by a
{ pointer called top. The first element is at the bottom of the stack, and its link field is set to
head=null; Null. An empty stack will have Top = Null.
}
void create(int n)
{
MyNode temp,last=null;
for(int i=0;i<n;i++)
{
temp=new MyNode();
temp.data=s.nextInt();
if(head==null) Implementation of Stack using linked lists:
{ import java.util.Scanner;
head=last=temp; class MyNode
head.next=head; {
} int data;
else MyNode next;
{ }
last.next=temp; class MyStack
temp.next=head; {
last=temp; MyNode top;
} MyStack()
} {
} top=null;
void display() }
{ void push(int e)
MyNode temp; {
for(temp=head;temp.next!=head;temp=temp.next) MyNode temp=new MyNode();
System.out.print(temp.data+"->"); temp.data=e;
System.out.println(temp.data); if(top==null)
} {
} top=temp;
class CircularLinkedListDemo top.next=null;
} case 2:stk.pop();
else break;
{ case 3:System.out.println(stk.peek());
temp.next=top; break;
top=temp; case 4:stk.display();
} break;
} default:return;
void pop() }
{ }
if(top==null) }
System.out.println("stack underflow"); }
else
{ Linked Queues
System.out.println("the element to be deleted is "+top.data); we have a circular queue of fixed size. However, there are many drawbacks of implementing
if(top.next!=null) queues using arrays. The fixed sizes do not give flexibility to the user to dynamically exceed
top=top.next; the maximum size. The declaration of arbitrarily maximum size leads to poor utilization of
else memory. In addition, the major drawback is the updating of front and rear.
top=null;
}
}
int peek()
{
if(top==null) Implementation of Queue using linked lists
return -1; import java.util.Scanner;
else class MyNode
return top.data; {
} int data;
void display() MyNode next;
{ }
MyNode temp; class MyQueue
for(temp=top;temp!=null;temp=temp.next) {
System.out.print(temp.data+"<-"); MyNode front,rear;
System.out.println("null"); MyQueue()
} {
} front=rear=null;
class LinkedStackDemo }
{ void enQueue(int e)
public static void main(String[] args) {
{ MyNode temp=new MyNode();
Scanner s=new Scanner(System.in); temp.data=e;
MyStack stk=new MyStack(); temp.next=null;
int ch; if(front==null)
while(true) front=rear=temp;
{ else
System.out.println("enter your choice"); {
ch=s.nextInt(); rear.next=temp;
switch(ch) rear=temp;
{ }
case 1:stk.push(s.nextInt()); }
break; void deQueue()
{
if(front!=null)
{
System.out.println("the element to be deleted is "+front.data);
if(front.next!=null)
front=front.next;
else
front=rear=null;
}
else
System.out.println("Queue underflow");
}
void display()
{
MyNode temp;
for(temp=front;temp!=null;temp=temp.next)
System.out.print(temp.data+"->");
System.out.println("null");
}
}
class LinkedQueueDemo
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
MyQueue q=new MyQueue();
int ch;
while(true)
{
System.out.println("enter your choice ");
ch=s.nextInt();
switch(ch)
{
case 1:q.enQueue(s.nextInt());
break;
case 2:q.deQueue();
break;
case 3:q.display();
break;
default:return;
}
}
}
}
19 VVIT, NAMBUR