Professional Documents
Culture Documents
Trees and Graphs are widely used non-linear data structures. Tree and graph
structures represents hierarchial relationship between individual data elements. Graphs
are nothing but trees with certain restrictions removed.
Data structures are divided into two types:
Primitive Data Structures are the basic data structures that directly operate upon the
machine instructions. They have different representations on different computers.
Integers, floating point numbers, character constants, string constants and pointers
come under this category.
Non-primitive data structures are more complicated data structures and are derived
from primitive data structures. They emphan on grouping same or different data items
with relationship between each data item. Arrays, lists and files come under this
category.
ALGORITHM:
An algorithm is a finite sequence of instructions, each of which has a clear
meaning and can be performed with a finite amount of effort in a finite length of time.
No matter what the input values may be, an algorithm terminates after executing a finite
number of instructions. In addition every algorithm must satisfy the following criteria:
Input: there are zero or more quantities, which are externally supplied;
Output: at least one quantity is produced;
Definiteness: each instruction must be clear and unambiguous;
Finiteness: if we trace out the instructions of an algorithm, then for all cases the
algorithm will terminate after a finite number of steps;
Effectiveness: every instruction must be sufficiently basic that it can in principle be
carried out by a person using only pencil and paper. It is not enough that each operation
be definite, but it must also be feasible.
Its examples are: array, stack, queue, While its examples are: trees
6. linked list, etc. and graphs.
Applications of non-linear
Applications of linear data structures are data structures are in Artificial
mainly in application software Intelligence and image
7. development. processing.
LINKED LISTS:
L inked lists and arrays are similar since they both store collections of data. Array
is the most common data structure used to store collections of elements. Arrays
are convenient to declare and provide the easy syntax to access any element by
its pos Number.
Once the array is set up, access to any element is convenient and fast. The disadvantages
of arrays are:
1.The n of the array is fixed. Most often this n is specified at compile time. This makes
the programmers to allocate arrays, which seems "large enough" than required.
Linked lists have their own strengths and weaknesses, but they happen to be strong
where arrays are weak. Generally array's allocates the memory for all its elements in
one block whereas linked lists use an entirely different strategy. Linked lists allocate
memory for each element separately and only when necessary.
Linked lists have many advantages. Some of the very important advantages are:
1. Linked lists are dynamic data structures. i.e., they can grow or shrink during the
execution of a program.
2. Linked lists have efficient memory utilization. Here, memory is not pre-
allocated. Memory is allocated whenever it is required and it is de-allocated
(removed) when it is no longer needed.
3. Insertion and Deletions are easier and efficient. Linked lists provide flexibility in
inserting a data item at a specified pos and deletion of the data item from the given
pos.
4. Many complex applications can be easily carried out with linked lists.
INSERTION:
One of the most primitive operations that can be done in a singly linked list is the
insertion of a node. Memory is to be allocated for the new node (in a similar way that is
done while creating a list) before reading the data. The new node will contain empty
data field and empty next field. The data field of the new node is then stored with the
information read from the user. The next field of the new node is assigned to NULL.
The new node can then be inserted at three different places namely:
• Inserting a node at the beginning.
• Inserting a node at the end.
• Inserting a node at intermediate pos.
void delfromend()
{
if(n<1)
{
System.out.println("no dtata within a linked list");
}
else if(n==1)
{
head=null;
n--;
}
else
{
Node t=head;
while(t.next.next!=null)
{
t=t.next;
}
t.next=null;
n--;
}
}
3.Deleting a node at intermediate pos:
void delfromspecifiedpos(int pos)
{
if(pos>n)
{
System.out.println("invalid pos");
}
else
{
int c=1;
Node t=head;
if(pos==n)
{
delfromend();
}
else if(pos==1)
{
delfrombeg();
}
else
{
while(c<pos-1)
{
t=t.next;
c++;
}
t.next=t.next.next;
n--;
}
}
}
TRAVERSING:
void disp()
{
Node t = new Node();
t = this.head;
if(t != null)
{
System.out.print("The list contains: ");
while(t != null) {
System.out.print(t.data + " ");
t = t.next;
}
System.out.println();
}
else
{
System.out.println("The list is empty.");
}
}
void disp()
{
Node t=head;
System.out.println("-------------------------------");
while(t!=null)
{
System.out.println(t.data);
t=t.next;
}
}
void dispcount()
{
System.out.println("number of nodes: "+n);
}
public static void main(String[] args) {
// TODO Auto-generated method Nodeb
Scanner s=new Scanner(System.in);
int data;
Sllsample l=new Sllsample();
System.out.println("Enter number of : ");
int x=s.nextInt();
for(int i=0;i<x;i++)
{
data=s.nextInt();
l.insert(data);
}
sl:
for(;;)
{
System.out.println("1. Insert at BEGIN \n 2. Insert at END \n 3. insert at Specified Pos
\n 4. Delete at BEGIN \n 5.Delete at END \n 6.Delete at Specified Pos \n 7. Display data \n 8. Display
no.of nodes \n 9. exit ");
int ch=s.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter data: ");
data=s.nextInt();
l.insertatbegin(data);
break;
case 2:
System.out.println("Enter data: ");
data=s.nextInt();
l.insert(data);
break;
case 3:
System.out.println("Enter data: ");
data=s.nextInt();
System.out.println("Enter new node pos");
int pos=s.nextInt();
l.insertat(data, pos);
break;
case 4:
l.delfrombeg();
break;
case 5:
l.delfromend();
break;
case 6:
int y=s.nextInt();
l.delfromspecifiedpos(y);
break;
case 7:
l.disp();
break;
case 8:
l.dispcount();
break;
case 9:
break sl;
}
}
}
CREATION:
Creating a node for Double Linked list:
public class Node {
int data;
int n;
Node head=null;
Node tail=null;
Node next;
Node(int data)
{
this.data=data;
this.prev=null;
this.next=null;
}
}
INSERTION:
The new node can then be inserted at three different places namely:
• Inserting a node at the beginning.
• Inserting a node at the end.
• Inserting a node at intermediate pos.
Deletion of a node:
Another primitive operation that can be done in a singly linked list is the deletion of a
node. Memory is to be released for the node to be deleted. A node can be deleted from
the list from three different places namely.
• Deleting a node at the beginning.
• Deleting a node at the end.
• Deleting a node at specified pos.
TRAVERSING:
1. To display from first to tail:
void displayHeadToTail()
{
Node t = head;
if(t!=null)
{
System.out.print("The doubly linked list is : ");
while (t != null)
{
System.out.println(+t.data);
t = t.next;
}
}
else
{
System.out.println(“The list is Empty”);
}
}
•The major advantage is that we can head from any node and still we can
traverse the entire list.
• We can maintain one pointer “tail” and get the first (head) node using the
next pointer of the tail node.
• We no longer need a NULL assignment unless the list itself is empty.
• It is very useful in CPU round robin scheduling.
Disadvantages of a Circular Singly Linked List
INSERTION:
1. Insert the new node as the first node:
void insertatbegin(int data)
{
Node newnode = new Node(data);
if (head == null)
{
head = newnode;
tail = newnode;
newnode.next = head;
}
Else
{
Node t = head;
newnode.next = t;
head = newnode;
tail.next = head;
}
}
2. Insert at specified pos:
void insertatpos(int data, int pos)
{
Node t, newnode;
int i, count;
newnode = new Node();
Node t = head;
Int n=0;
if (t == null || n < pos)
System.out.println("Index is greater than n of the list");
else
{
newnode.data = data;
while(t.next!=null)
{
t = t.next;
}
newnode.next = t.next;
t.next = newnode;
}
DELETION:
Advantages:
• List can be traversed from both directions i.e. from head to tail or from tail
to head.
• Jumping from head to tail or from tail to head is done in constant time
O(1).
• Circular Doubly Linked Lists are used for the implementation of advanced
data structures like Fibonacci Heap.
Disadvantages
• It takes slightly extra memory in each node to accommodate the previous
pointer.
• Lots of pointers involved while implementing or doing operations on a list.
So, pointers should be handled carefully otherwise data of the list may get
lost.
CREATION:
class Node
{
int data;
Node next;
Node prev;
}
INSERTION:
1.Insertion at the end:
void insertatend(int value)
{
if (head == null)
{
Node newnode = new Node();
newnode.data = value;
newnode.next = newnode.prev = newnode;
head = newnode;
return;
}
Node tail = (head).prev;
Node newnode = new Node();
newnode.data = value;
newnode.next = head;
head.prev = newnode;
newnode.prev = tail;
tail.next = newnode;
}
2.Insertion at the beginning of the list:
void insertatbegin(int value)
{
Node tail = head.prev;
Node newnode = new Node();
newnode.data = value;
newnode.next = head;
newnode.prev = tail;
tail.next = head.prev = newnode;
head = newnode;
}
3.Insertion in between the nodes of the list:
void insertAt(int value1,int value2)
{
Node newnode = new Node();
newnode.data = value1;
Node t = head;
while (t.data != value2)
t = t.next;
Node next = t.next;
t.next = newnode;
newnode.prev = t;
newnode.next = next;
next.prev = newnode;
}
DELETION:
void delfrombegin()
{
if(this.head != null)
{
if(this.head.next == this.head)
{
this.head = null;
}
else
{
Node t = this.head;
Node firstNode = this.head;
while(t.next != this.head)
{
t = t.next;
}
this.head = this.head.next;
this.head.prev = t;
t.next = this.head;
firstNode = null;
}
}
}
TRAVERSING:
void PrintList()
{
Node t = new Node();
t = this.head;
if(t != null)
{
System.out.print("The list contains: ");
while(true)
{
System.out.print(t.data + " ");
t = t.next;
if(t == this.head)
break;
}
System.out.println();
}
else
{
System.out.println("The list is empty.");
}
}