0% found this document useful (0 votes)
109 views10 pages

Differences Between Arrays and Linked Lists

Linked lists are a dynamic data structure that allows for efficient insertion and deletion of elements. Unlike arrays, linked list elements are not stored consecutively in memory but can be scattered. Each element of a linked list, called a node, contains data and a pointer to the next node. There are different types of linked lists including singly linked lists where each node points to the next one, doubly linked lists where each node points to both the next and previous nodes, and circular linked lists where the last node loops back to the first one. Common linked list operations include insertion and deletion of nodes, searching, sorting, and determining the size of the list.

Uploaded by

Hemanth Goli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views10 pages

Differences Between Arrays and Linked Lists

Linked lists are a dynamic data structure that allows for efficient insertion and deletion of elements. Unlike arrays, linked list elements are not stored consecutively in memory but can be scattered. Each element of a linked list, called a node, contains data and a pointer to the next node. There are different types of linked lists including singly linked lists where each node points to the next one, doubly linked lists where each node points to both the next and previous nodes, and circular linked lists where the last node loops back to the first one. Common linked list operations include insertion and deletion of nodes, searching, sorting, and determining the size of the list.

Uploaded by

Hemanth Goli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

II B.Tech IT sem-1 II B.

Tech IT sem-1
DATA STRUCTURES DATA STRUCTURES

UNIT-3: Linked Lists: Linked Lists:


Single Linked List: Introduction, Representation, Operations, Applications. Similar to arrays, LinkedList is a linear data structure. However LinkedList elements are
Double Linked Lists: Introduction, Representation, Operations not stored in contiguous locations like arrays, they are linked with each other using pointer or
Circular Linked Lists: Introduction, Representation, Operations a link. Each element of the LinkedList has the reference(address/pointer) to the next element
of the LinkedList. Each element in the LinkedList is called the Node. Each Node of the
One of the drawbacks of an array is that it is a static data structure, that is, the maximum LinkedList contains two items: 1) Content of the element 2) Pointer/Address/Reference to the
capacity of an array should be known before the compilation process. Therefore, we must Next Node in the LinkedList.
explicitly define its size before compilation there by reduces effective space utilization.

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
{

1 VVIT, NAMBUR 2 VVIT, NAMBUR


II B.Tech IT sem-1 II B.Tech IT sem-1
DATA STRUCTURES DATA STRUCTURES

int data; //last.next=temp;


MyNode next; //last=temp;
} }
class MyLinkedList void insertP(int p)
{ {
MyNode head=null; MyNode temp=new MyNode();
MyNode last=null; System.out.println("enter the element to be inserted");
Scanner s=new Scanner(System.in); temp.data=s.nextInt();
void create(int n) MyNode temp1;
{ int i;
MyNode temp; for(i=0,temp1=head;i<p-1;i++,temp1=temp1.next);
for(int i=0;i<n;i++) temp.next=temp1.next;
{ temp1.next=temp;
temp=new MyNode(); }
System.out.println("enter the data"); void deleteB()
temp.data=s.nextInt(); {
temp.next=null; MyNode temp=head;
if(head==null) head=temp.next;
head=last=temp; temp=null;
else }
{ void deleteE()
last.next=temp; {
last=temp; MyNode temp,temp1;
} temp=head;
} temp1=null;
} while(temp.next!=null)
void display() {
{ temp1=temp;
MyNode temp; temp=temp.next;
for(temp=head;temp!=null;temp=temp.next) }
System.out.print(temp.data+"->"); temp1.next=null;
System.out.println("NULL"); temp=null;
} last=temp1;
void insertB() }
{
MyNode temp=new MyNode(); void deleteP(int p)
System.out.println("enter the element to be inserted"); {
temp.data=s.nextInt(); MyNode temp,temp1;
temp.next=head; int i;
head=temp; for(temp=head,i=0;i<p-1;i++,temp=temp.next);
} temp1=temp.next;
void insertE() temp.next=temp1.next;
{ temp1=null;
MyNode temp=new MyNode(); }
System.out.println("enter the element to be inserted"); }
temp.data=s.nextInt(); class LinkedListDemo
temp.next=null; {
MyNode temp1; public static void main(String[] args)
for(temp1=head;temp1.next!=null;temp1=temp1.next); {
temp1.next=temp; int ch;

3 VVIT, NAMBUR 4 VVIT, NAMBUR


II B.Tech IT sem-1 II B.Tech IT sem-1
DATA STRUCTURES DATA STRUCTURES

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.

5 VVIT, NAMBUR 6 VVIT, NAMBUR


II B.Tech IT sem-1 II B.Tech IT sem-1
DATA STRUCTURES DATA STRUCTURES

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

7 VVIT, NAMBUR 8 VVIT, NAMBUR


II B.Tech IT sem-1 II B.Tech IT sem-1
DATA STRUCTURES DATA STRUCTURES

}
}
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. }

9 VVIT, NAMBUR 10 VVIT, NAMBUR


II B.Tech IT sem-1 II B.Tech IT sem-1
DATA STRUCTURES DATA STRUCTURES

Doubly linked list: if(head==null)


{
Doubly linked list is a type of linked list in which each node apart from storing its data has head=last=temp;
two links. The first link points to the previous node in the list and the second link points to head.prev=null;
the next node in the list. The cost of this is an extra link, which adds to the space }
requirements and also doubles the cost of insertions and deletions because there are more else
pointers to fix. On the other hand it simplifies deletion, because you no longer have to refer {
to a key by using pointers to the previous cell. last.next=temp;
Declaration of the node is, temp.prev=last;
last=temp;
class Node
}
{
}
Node previous;
}
Element_type data; void insertB()
Node next; {
} MyNode temp=new MyNode();
System.out.println("enter data");
A Sample Double Linked List temp.data=s.nextInt();
temp.prev=null;
temp.next=head;
head.prev=temp;
head=temp;
}
void insertE()
Implementation of Double Linked List: {
import java.util.Scanner; MyNode temp=new MyNode();
class MyNode MyNode temp1;
{ System.out.println("enter data");
MyNode prev; temp.data=s.nextInt();
int data; temp.next=null;
MyNode next; for(temp1=head;temp1.next!=null;temp1=temp1.next);
} temp.prev=temp1;
class MyLinkedList temp1.next=temp;
{ last=temp;
Scanner s=new Scanner(System.in); }
MyNode head,last; void insertP(int p)
MyLinkedList() {
{ MyNode temp=new MyNode();
head=null; MyNode temp1;
last=null; int i;
} System.out.println("enter data");
void create(int n) temp.data=s.nextInt();
{ for(i=0,temp1=head;i<p-1;temp1=temp1.next,i++);
MyNode temp; temp.next=temp1.next;
for(int i=0;i<n;i++) temp.prev=temp1;
{ temp1.next.prev=temp;
temp=new MyNode(); temp1.next=temp;
System.out.println("enter data"); }
temp.data=s.nextInt(); void deleteB()
temp.next=null; {

11 VVIT, NAMBUR 12 VVIT, NAMBUR


II B.Tech IT sem-1 II B.Tech IT sem-1
DATA STRUCTURES DATA STRUCTURES

MyNode temp; while(true)


temp=head; {
head=temp.next; System.out.println("enter your choice");
head.prev=null; ch=s.nextInt();
temp=null; switch(ch)
} {
void deleteE() case 1: l.insertB();
{ break;
MyNode temp,temp1; case 2: l.insertE();
for(temp=head;temp.next!=null;temp=temp.next); break;
temp1=temp.prev; case 3:System.out.println("enter the position");
temp1.next=null; l.insertP(s.nextInt());
temp=null; break;
last=temp1; case 4:l.deleteB();
} break;
void deleteP(int p) case 5:l.deleteE();
{ break;
MyNode temp,temp1; case 6: System.out.println("emter the position");
int i; l.deleteP(s.nextInt());
for(temp=head,i=0;i<p-1;i++,temp=temp.next); break;
temp1=temp.next; case 7:l.displayF();
temp.next=temp1.next; break;
temp1.next.prev=temp; case 8:l.displayB();
temp1=null; break;
} default:return;
void displayF() }
{ }
MyNode temp; }
for(temp=head;temp!=null;temp=temp.next) }
System.out.print(temp.data+"->");
System.out.println("null"); Advantages over singly linked list
} 1) A DLL can be traversed in both forward and backward direction.
void displayB() 2) The delete operation in DLL is more efficient if pointer to the node to be deleted is given.
{ 3) We can quickly insert a new node before a given node.
MyNode temp; Disadvantages over singly linked list
for(temp=last;temp!=null;temp=temp.prev) 1) Every node of DLL Require extra space for an previous pointer.
System.out.print(temp.data+"->"); 2) All operations require an extra pointer previous to be maintained.
System.out.println("null");
} Circular linked list
} Circular linked list is a linked list in which the last node of the list contains a pointer to the
class DoubleLinkedListDemo first node of the list. We can have circular singly linked list as well as circular doubly linked
{ list.
public static void main(String[] args)
{ Circular single linked list
Scanner s=new Scanner(System.in);
MyLinkedList l=new MyLinkedList();
System.out.println("enter the size o the list");
int ch;
l.create(s.nextInt()); Circular double linked list

13 VVIT, NAMBUR 14 VVIT, NAMBUR


II B.Tech IT sem-1 II B.Tech IT sem-1
DATA STRUCTURES DATA STRUCTURES

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

15 VVIT, NAMBUR 16 VVIT, NAMBUR


II B.Tech IT sem-1 II B.Tech IT sem-1
DATA STRUCTURES DATA STRUCTURES

} 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()

17 VVIT, NAMBUR 18 VVIT, NAMBUR


II B.Tech IT sem-1
DATA STRUCTURES

{
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

You might also like