0% found this document useful (0 votes)
37 views

Data Structures

The document discusses data structures and linked lists. It defines data structures as a way to organize and store data for efficient use. Linked lists are introduced as a linear data structure that can dynamically allocate nodes in memory, as opposed to arrays which have fixed sizes. The key aspects of linked lists are nodes that contain data and a pointer to the next node. Operations like adding a node to the end of the linked list are demonstrated.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Data Structures

The document discusses data structures and linked lists. It defines data structures as a way to organize and store data for efficient use. Linked lists are introduced as a linear data structure that can dynamically allocate nodes in memory, as opposed to arrays which have fixed sizes. The key aspects of linked lists are nodes that contain data and a pointer to the next node. Operations like adding a node to the end of the linked list are demonstrated.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Data Structures 

can be defined as a way to store and organize data so that it can be used efficiently. Data
structures play a very important role in any software development as they help us to store and retrieve data as
fast as possible.

Why do we need Data Structures?

 Large amounts of data is processed by different systems and if this is not stored properly, it will lead to
data loss.
 It is easier to search for a particular data if it is organized in a proper format.
 Data Structures help a program to minimize the usage of resources such as memory and space.

There are different types of data structures like list, stack, queue, etc. Each of them are suitable for specific
type of tasks.

Data structures can also be categorized as linear and non-linear. 

A linear data structure is the one in which elements are arranged in a sequential manner and each element is
connected to its previous and next element. 

A non-linear data structure is the one in which elements are not arranged in a sequential manner and each
element is connected to other elements through multiple paths. 

You have already seen and worked with Array. Array is a linear data structure used to store similar type of
data. It is very easy to access any member of an array by using index.

You will now see how to insert and delete elements in an array.

To insert an element at a specific position, you first need to shift all the elements from that position by one to
the right.

Consider the array [A, B, C, D, E, F]. Let's say you want to insert 'J' at the third position.

First, all the elements from the third position to the last position are shifted.

Then, the specific element is inserted at the given position.


You will now see how to delete an element from an array.

For deleting an element from an array, the elements need to be shifted towards the position from which the
element has to be deleted.

Consider the array [A, B, C, D, E, F] and let's say you want to delete the element at the third position.
Some of the limitations are:

 An array can contain data of only similar type. Heterogeneous elements cannot be stored in an array.
 Array is of fixed size. Once declared, the size cannot be changed. Let us consider an example of a
school which uses an array to store names of all the students in a class. Initially based on the number
of students, an array is created and the information is stored. Suppose a student joins after few
days, storing the information of this student now becomes very difficult.
 You have seen the insertion and deletion operations in an array. Those operations are expensive in
terms of memory as they require shifting of elements. This shifting may also sometime lead to loss of
data if we try to exceed the capacity of the array. Let us take the example of a car having 5 seats. 5
people are seated and another person comes and wants to sit at the second seat. In order to
accommodate this person, the person sitting at the second seat shifts to the third seat and so on. At
the end, the person who was sitting at the last has no place to sit.
 The elements of an array are stored in continuous memory locations. If the required number of blocks
are not present together in the memory, then the array will not be created. 

Linked list

Let us consider that a person named Noah is travelling to Europe for a holiday. He plans to travel to different
cities by train. The trip will cover most of the major cities like Milan, Venice, Paris, Amsterdam, etc.

He wants to store the information as to what cities he will be covering and in which order. The duration of the
trip is not fixed and also the number of cities that Noah wants to cover are not finalized and may change on the
way. 
The trip details need to be stored in some data structure. Since the number of cities is not fixed, it is not a good
idea to use an array for storing the information.

This kind of information can be stored using another data structure called Linked list. Linked list eliminates the
disadvantages of an array that we had discussed previously.

A Linked List is a linear data structure used to store collections of objects. A Linked List is made up of
individual nodes which may be present at different memory locations, i.e., they need not be contiguous unlike
arrays. These nodes are connected or linked to each other. A node is made up of two parts - one to store the
data and a link to connect to the next node. 

We need to first represent a node. A node can be represented by creating a class.

The class can have two attributes

 data - for storing the data


 next - for storing the link to the next node
public class Node {

String data;

Node next;

Here, the data to be stored is of type String since city names have to be stored.

The data member variable can be of any primitive type or even objects of any class based on the requirement.

The complete implementation of the Node class is given below:

public class Node {

private String data;

private Node next;

public Node(String data){

this.data=data;

public void setData(String data){

this.data = data;

public void setNext(Node node){

this.next = node;

public String getData(){

return this.data;

public Node getNext(){

return this.next;
}

Note:- "next" attribute of the Node class will be set to null by the constructor implicitly hence, it’s not explicitly
set to null. This can also be done by adding "next=null;" in the constructor of Node class.

A linked list can be represented as a set of nodes connected together.

The node referred by head marks the beginning of the list and the node referred by tail, marks the end of the
list.

public class LinkedList {

Node head;

Node tail;

The complete implementation of LinkedList class is given below:

public class LinkedList {

private Node head;

private Node tail;

public Node getHead(){

return this.head;

public Node getTail(){

return this.tail;

Adding element to linked list

Noah decides that the next city he will go to after Munich is Vienna. He wants to add this to his list.

Currently, Munich is the tail node of the list. So, it means that Vienna should be added after Munich and
should be made as the tail node.
Adding a node at the end of a linked list is very simple if the tail is known. The new node is simply linked to the
tail node and the position of the tail is changed as shown below.

add(data)

1.Create a new node with the given data

2.If the linked list is empty, then make this new node the head and the tail node

3.Else,

a.Make the link of the tail node refer to the new node

b.Make the tail refer to the new node

class Node {

private String data;

private Node next;

public Node(String data){

this.data=data;
}

public void setData(String data){

this.data = data;

public void setNext(Node node){

this.next = node;

public String getData(){

return this.data;

public Node getNext(){

return this.next;

class LinkedList {

private Node head;

private Node tail;

public Node getHead(){

return this.head;

public Node getTail(){

return this.tail;
}

public void addAtEnd(String data){

//Create a new node

Node node = new Node(data);

//Check if the list is empty,

//if yes, make the node as the head and the tail

if(this.head == null)

this.head=this.tail=node;

else{

//If the list is not empty, add the element at the end

this.tail.setNext(node);

//Make the new node as the tail

this.tail=node;

class Tester{

public static void main(String args[]){

LinkedList list = new LinkedList();

list.addAtEnd("Milan");

list.addAtEnd("Venice");

list.addAtEnd("Munich");
list.addAtEnd("Vienna");

System.out.println("Adding an element to the linked list");

Problem Statement
Implement the method addAtBeginning(String data) to add a new element at the
beginning of a linked list.

You can make use of the below steps to implement the method.

1. Create a new node with the data passed to the method

2. If the linked list is empty, make the new node as the head and the tail node.

3. If the linked list is not empty, add the new node at the beginning, i.e., before
the current head node and make the new node as the head.
class Node {

private String data;

private Node next;

public Node(String data){

this.data=data;

public void setData(String data){

this.data = data;

public void setNext(Node node){

this.next = node;

}
public String getData(){

return this.data;

public Node getNext(){

return this.next;

class LinkedList {

private Node head;

private Node tail;

public Node getHead(){

return this.head;

public Node getTail(){

return this.tail;

public void addAtEnd(String data){

//Create a new node

Node node = new Node(data);

//Check if the list is empty,

//if yes, make the node as the head and the tail
if(this.head == null)

this.head=this.tail=node;

else{

//If the list is not empty, add the element at the end

this.tail.setNext(node);

//Make the new node as the tail

this.tail=node;

public void addAtBeginning(String data){

//Implement your code here

class Tester{

public static void main(String args[]){

LinkedList list = new LinkedList();

list.addAtEnd("Milan");

list.addAtEnd("Venice");

list.addAtEnd("Munich");
list.addAtBeginning("Nice");

you will see how to display the elements of a linked list.

To display the elements of a linked list from head to tail, you need to traverse the list using a temporary
reference and print the data at each point.

display()

1.Take a temp reference and assign it with the head node

2.While temp is not null,

a.Display the data at the temp node

b.Make the next node as temp

class Node {

private String data;

private Node next;

public Node(String data) {

this.data = data;

}
public void setData(String data) {

this.data = data;

public void setNext(Node node) {

this.next = node;

public String getData() {

return this.data;

public Node getNext() {

return this.next;

class LinkedList {

private Node head;

private Node tail;

public Node getHead() {

return this.head;

}
public Node getTail() {

return this.tail;

public void addAtEnd(String data) {

// Create a new node

Node node = new Node(data);

// Check if the list is empty,

// if yes, make the node as the head and the tail

if (this.head == null)

this.head = this.tail = node;

else {

// If the list is not empty, add the element at the end

this.tail.setNext(node);

// Make the new node as the tail

this.tail = node;

public void addAtBeginning(String data) {

// Create a new node

Node node = new Node(data);

// Check if the list is empty,

// if yes, make the node as the head and the tail


if (this.head == null)

this.head = this.tail = node;

else {

// If the list is not empty, add the element at the beginning

node.setNext(this.head);

// Make the new node as the head

this.head = node;

public void display() {

// Initialize temp to the head node

Node temp = this.head;

// Traverse the list and print data of each node

while (temp != null) {

System.out.println(temp.getData());

temp = temp.getNext();

public static void main(String args[]) {

LinkedList list = new LinkedList();

list.addAtEnd("Milan");

list.addAtEnd("Venice");

list.addAtEnd("Munich");

list.addAtEnd("Vienna");
list.display();

Searching an element in a Linked List.

Searching in linked list is similar to display operation. To find an element in a linked list, you need to traverse
the list using a temporary reference. If the data is found, the respective node is returned.
The algorithm for searching an element in a linked list is given below.

find(data)

1.Take a temp reference and assign it with head node

2.While temp is not null,

a.If the data at temp is equal to the data being searched for then, return temp

b.Else, make the next node as temp

3.If data is not found then, return null

class Node {

private String data;

private Node next;

public Node(String data) {

this.data = data;

public void setData(String data) {

this.data = data;

public void setNext(Node node) {

this.next = node;

public String getData() {

return this.data;

}
public Node getNext() {

return this.next;

class LinkedList {

private Node head;

private Node tail;

public Node getHead() {

return this.head;

public Node getTail() {

return this.tail;

public void addAtEnd(String data) {

// Create a new node

Node node = new Node(data);

// Check if the list is empty,

// if yes, make the node as the head and the tail

if (this.head == null)
this.head = this.tail = node;

else {

// If the list is not empty, add the element at the end

this.tail.setNext(node);

// Make the new node as the tail

this.tail = node;

public void addAtBeginning(String data) {

// Create a new node

Node node = new Node(data);

// Check if the list is empty,

// if yes, make the node as the head and the tail

if (this.head == null)

this.head = this.tail = node;

else {

// If the list is not empty, add the element at the beginning

node.setNext(this.head);

// Make the new node as the head

this.head = node;

}
public void display() {

// Initialize temp to the head node

Node temp = this.head;

// Traverse the list and print data of each node

while (temp != null) {

System.out.println(temp.getData());

temp = temp.getNext();

public Node find(String data) {

Node temp = this.head;

// Traverse the list and return the node

// if the data of it matches with the searched data

while (temp != null) {

if (temp.getData().equals(data))

return temp;

temp = temp.getNext();

return null;

public static void main(String args[]) {

LinkedList list = new LinkedList();

list.addAtEnd("Milan");
list.addAtEnd("Venice");

list.addAtEnd("Munich");

list.addAtEnd("Vienna");

list.display();

if (list.find("Munich") != null)

System.out.println("Node found");

else

System.out.println("Node not found");

Inserting an element into linked list


Noah's friend Alice who lives in Prague has invited him to her birthday and according to the schedule, Noah will
now have to go to Prague before he visits Vienna.

Noah needs to change his list and insert Prague after Munich.

To insert an element in a linked list, you first need to find the node after which the new node has to be inserted.
The new node should then be placed after the specific node.
The algorithm to insert an element in a linked list is given below.

insert(data,dataBefore)

1.Create a new node with the given data

3.Find the node with dataBefore. If found then,

a.Call this as the nodeBefore

b.Make the link of the new node refer to the link of nodeBefore

c.Make the link of nodeBefore refer to the new node

d.If link of the new node is null, make it as the tail node

4. If node with dataBefore is not found, display appropriate error


message

class Node {

private String data;

private Node next;


public Node(String data) {

this.data = data;

public void setData(String data) {

this.data = data;

public void setNext(Node node) {

this.next = node;

public String getData() {

return this.data;

public Node getNext() {

return this.next;

}
class LinkedList {

private Node head;

private Node tail;

public Node getHead() {

return this.head;

public Node getTail() {

return this.tail;

public void addAtEnd(String data) {

// Create a new node

Node node = new Node(data);

// Check if the list is empty,

// if yes, make the node as the head and the tail


if (this.head == null)

this.head = this.tail = node;

else {

// If the list is not empty, add the element at the end

this.tail.setNext(node);

// Make the new node as the tail

this.tail = node;

public void addAtBeginning(String data) {

// Create a new node

Node node = new Node(data);

// Check if the list is empty,

// if yes, make the node as the head and the tail

if (this.head == null)

this.head = this.tail = node;

else {
// If the list is not empty, add the element at the
beginning

node.setNext(this.head);

// Make the new node as the head

this.head = node;

public void display() {

// Initialize temp to the head node

Node temp = this.head;

// Traverse the list and print data of each node

while (temp != null) {

System.out.println(temp.getData());

temp = temp.getNext();

public Node find(String data) {


Node temp = this.head;

// Traverse the list and return the node

// if the data of it matches with the searched data

while (temp != null) {

if (temp.getData().equals(data))

return temp;

temp = temp.getNext();

return null;

public void insert(String data, String dataBefore) {

Node node = new Node(data);

// Check if the list is empty,

// if yes, make the node as the head and the tail

if (this.head == null)

this.head = this.tail = node;

else {

// Find the node after which the data has to be


inserted
Node nodeBefore = this.find(dataBefore);

if (nodeBefore != null) {

// Insert the new node after nodeBefore

node.setNext(nodeBefore.getNext());

nodeBefore.setNext(node);

// If nodeBefore is currently the tail node,

// make the new node as the tail node

if (nodeBefore == this.tail)

this.tail = node;

} else

System.out.println("Node not found");

public static void main(String args[]) {

LinkedList list = new LinkedList();

list.addAtEnd("Milan");

list.addAtEnd("Venice");

list.addAtEnd("Munich");

list.addAtEnd("Vienna");
list.insert("Prague", "Munich");

list.display();

Deleting an element from linked list


Noah's boss just called him and asked him to come back early from his vacation. Noah will now have to change
his plan and skip visiting some cities. He decides not to go to Venice and directly travel to Munich from Milan.
He wants to make the changes in his list and remove Venice from it.

To delete an element from a linked list, you first need to find the element and then connect the previous node to
the next node of the node to be deleted.
delete(data):

1. Find the node with the given data. If found,

a. If the node to be deleted is head node, make the next node as


head node

i. If it is also the tail node, make the tail node as null

b. Otherwise,

i. Traverse till the node before the node to be deleted, call it


nodeBefore

ii. Make link of nodeBefore refer to link of node to be deleted.

iii. If the node to be deleted is the tail node, call the nodeBefore as
tail node

iv. Make the link of the node to be deleted as null

2. If the node to be deleted is not found, display appropriate error


message

class Node {

private String data;

private Node next;

public Node(String data) {

this.data = data;
}

public void setData(String data) {

this.data = data;

public void setNext(Node node) {

this.next = node;

public String getData() {

return this.data;

public Node getNext() {

return this.next;

class LinkedList {
private Node head;

private Node tail;

public Node getHead() {

return this.head;

public Node getTail() {

return this.tail;

public void addAtEnd(String data) {

// Create a new node

Node node = new Node(data);

// Check if the list is empty,

// if yes, make the node as the head and the tail

if (this.head == null)

this.head = this.tail = node;


else {

// If the list is not empty, add the element at the end

this.tail.setNext(node);

// Make the new node as the tail

this.tail = node;

public void addAtBeginning(String data) {

// Create a new node

Node node = new Node(data);

// Check if the list is empty,

// if yes, make the node as the head and the tail

if (this.head == null)

this.head = this.tail = node;

else {

// If the list is not empty, add the element at the


beginning
node.setNext(this.head);

// Make the new node as the head

this.head = node;

public void display() {

// Initialize temp to the head node

Node temp = this.head;

// Traverse the list and print data of each node

while (temp != null) {

System.out.println(temp.getData());

temp = temp.getNext();

public Node find(String data) {

Node temp = this.head;

// Traverse the list and return the node


// if the data of it matches with the searched data

while (temp != null) {

if (temp.getData().equals(data))

return temp;

temp = temp.getNext();

return null;

public void insert(String data, String dataBefore) {

Node node = new Node(data);

// Check if the list is empty,

// if yes, make the node as the head and the tail

if (this.head == null)

this.head = this.tail = node;

else {

// Find the node after which the data has to be


inserted

Node nodeBefore = this.find(dataBefore);

if (nodeBefore != null) {
// Insert the new node after nodeBefore

node.setNext(nodeBefore.getNext());

nodeBefore.setNext(node);

// If nodeBefore is currently the tail node,

// make the new node as the tail node

if (nodeBefore == this.tail)

this.tail = node;

} else

System.out.println("Node not found");

public void delete(String data) {

// Check if the list is empty,

if (this.head == null)

System.out.println("List is empty");

else {

// Find the node to be deleted

Node node = this.find(data);

// If the node is not found


if (node == null)

System.out.println("Node not found");

// If the node to be deleted is the head node

else if (node == this.head) {

this.head = this.head.getNext();

node.setNext(null);

// If the node to be deleted is also the tail node

if (node == this.tail)

tail = null;

} else {

// Traverse to the node present before the node


to be deleted

Node nodeBefore = null;

Node temp = this.head;

while (temp != null) {

if (temp.getNext() == node) {

nodeBefore = temp;

break;

temp = temp.getNext();
}

// Remove the node

nodeBefore.setNext(node.getNext());

// If the node to be deleted is the tail node,

// then make the previous node as the tail

if (node == this.tail)

this.tail = nodeBefore;

node.setNext(null);

public static void main(String args[]) {

LinkedList list = new LinkedList();

list.addAtEnd("Milan");

list.addAtEnd("Venice");

list.addAtEnd("Munich");

list.addAtEnd("Prague");

list.addAtEnd("Vienna");

list.display();
System.out.println("--------------------------");

list.delete("Venice");

list.display();

/*

* if(list.find("Munich")!=null) System.out.println("Node
found"); else

* System.out.println("Node not found");

*/

}
Stack

class Stack {

private int top; // top represents the index position of the top most element in the stack

private int maxSize; // maxSize represents the maximum number of elements that can be stored
in the stack

private int[] arr;

Stack(int maxSize) {

this.top = -1; // top is -1 when the stack is created

this.maxSize = maxSize;

arr = new int[maxSize];

//Checking if the stack is full or not

public boolean isFull() {

if (top >= (maxSize - 1)) {

return true;

return false;
}

//Adding a new element to the top of the stack

public boolean push(int data) {

if (isFull()) {

return false;

} else {

arr[++top] = data;

return true;

class Tester {

public static void main(String args[]) {

Stack stack = new Stack(5);

System.out.println("Stack created.\n");

if (stack.push(1))

System.out.println("The element is pushed to the stack!\n");

else

System.out.println("Stack is full!\n");

if (stack.push(2))

System.out.println("The element is pushed to the stack!\n");

else

System.out.println("Stack is full!\n");

if (stack.push(3))

System.out.println("The element is pushed to the stack!\n");


else

System.out.println("Stack is full!\n");

if (stack.push(4))

System.out.println("The element is pushed to the stack!\n");

else

System.out.println("Stack is full!\n");

if (stack.push(5))

System.out.println("The element is pushed to the stack!\n");

else

System.out.println("Stack is full!\n");

Problem Statement
The code given below has the isFull, push, display and peek methods implemented.

Tester class is provided to push the elements. Play around with it and observe the
output.
class Stack {

private int top; // represents the index position of the top most element in the stack

private int maxSize; // represents the maximum number of elements that can be stored in the
stack

private int[] arr;

Stack(int maxSize) {
this.top = -1; // top is -1 when the stack is created

this.maxSize = maxSize;

arr = new int[maxSize];

// Checking if the stack is full or not

public boolean isFull() {

if (top >= (maxSize - 1)) {

return true;

return false;

// Adding a new element to the top of the stack

public boolean push(int data) {

if (isFull()) {

return false;

} else {

arr[++top] = data;

return true;

// Returning the top most element of the stack

public int peek() {


if (top < 0)

return Integer.MIN_VALUE;

else

return arr[top];

// Displaying all the elements of the stack

public void display() {

System.out.println("Displaying stack elements");

for (int index = top; index >= 0; index--) {

System.out.println(arr[index]); // accessing element at position index

class Tester {

public static void main(String args[]) {

Stack stack = new Stack(5);

System.out.println("Stack created.\n");

if (stack.push(1))

System.out.println("The element is pushed to the stack!\n");

else
System.out.println("Stack is full!\n");

if (stack.push(2))

System.out.println("The element is pushed to the stack!\n");

else

System.out.println("Stack is full!\n");

if (stack.push(3))

System.out.println("The element is pushed to the stack!\n");

else

System.out.println("Stack is full!\n");

if (stack.push(4))

System.out.println("The element is pushed to the stack!\n");

else

System.out.println("Stack is full!\n");

if (stack.push(5))

System.out.println("The element is pushed to the stack!\n");

else

System.out.println("Stack is full!\n");

stack.display();

if (stack.push(6))
System.out.println("The element is pushed to the stack!\n");

else

System.out.println("Stack is full!\n");

System.out.println("The top element is : " + stack.peek());

A1 a2 a3 a4

} 0 1 2 3 4

ham
Food adress

*ptr Add(food)
address

x=&food

You might also like