You are on page 1of 19

Module 1

What is data structure?


A data structure is a method of storing data in a computer so that it can be used efficiently. The
data structures mainly deal with:

The study of how data is organized in the memory


How efficiently can data be stored in memory
How efficiently data can be retrieved and manipulated
The possible ways in which different data items are logically related
Data structures provide a means to manage huge amounts of data efficiently, such as large
databases and internet indexing services

What is an abstract data type?


An abstract data type is defined as a data declaration packaged together with the
operations that are allowed on the data type. In other words, we encapsulate the data
and the operations on the data, and we hide their implementation from the user.
With abstract data type, the user is not concerned with how the task is done but rather
with what it can do. In other words, the ADT consists of a set of prototype definitions that
allow the programmer to use the function while hiding the implementation.
The generalization of operations with the unspecified implementations is known as
abstraction. We abstract the essence of the process and leave the implementation details
hidden. In the concept of abstraction, we know what a data type can do but how it is done
is hidden.
In an abstraction data type, we declare the data and the operations. The representation of
the data structure is hidden. The only means of modifying the data structure or retrieving
information about it is to call one of the operations associated with the abstract data type.
ADT we will be looking at are stacks, queue, singly linked list, searching and sorting
arrays.
Data Structures Singly linked list

When we want to work with unknown number of data values, we use a linked list data
structure to organize that data. Linked list is a linear data structures that contains
sequence of elements such that each element links to its next element in the
sequence. Each element in a linked list is called as Node.
Every Node contains two fields, data and next. The data field is used to store actual
value of the node and next field used to store the address of the next node in the
sequence.
NB in a single linked list, the address of the first node is always stored
in a reference node known as front {sometimes known as head}
Always next part (reference part) of the last node must be NULL

Single linked list Diagram

Stores address of next


nodes

Data

Link

Stores actual
value
Example of Linear List

Mode
Address
1001
10 1004

1004
25 1008

1008
18 1012

*Font
1001

Operations
In a single linked list we perform the following:
-

Insertion
Deletion
Display

Creating an empty list

1012
55 NULL

Before we implement actual operations first we need to set up an empty list. First perform the
following steps before implementing actual operations.
-

Step 1: Include all the header files which are used in the program
Step 2: Declare all user defined functions
Step 3: Define a node structure with two members, data and next
Step 4: Define a node pointer head and set it to NULL
Step 5: Implement the main method by displaying operations menu and make suitable
function calls in the main method to perform user select operation.

How to insert an element into the list?


In a single linked list, the insertion operation can be performed in three ways. They are as
follows:
-

Inserting at the beginning of the list


Inserting at the end of the list
Inserting at a specific location in the list

Inserting at the beginning of the list


We can use the following steps to insert a new node at the beginning of the file single linked
list
-

Step 1: Creates a new node with a given value


Step 2: Check whether list is empty (headNull)
Step 3: If empty then, set newNode next =NULL and head = newNode.
Step 4: If it is not empty set newNode next = head and head = newNode

Example:

Head

NULL

20

30

30
Current Node
NewNode

40

30

Inserting at the end of the list


-

Step 1 create a newNode with given value and newNode next as Null
Step 2 check whether list is empty (head null)
Step 3 if it is empty then set head = newnode
Step 4 if it is not empty then, define a node pointer temp and initialize with head
Step 5 Keep moving the temp to its next node until it reaches to the last node in the list
(until temp next is equal to Null )
Step 6 Set temp next = newNode.

Example:
Newcode
30 Next

NULL

Temp
10 Next

20 Next

Head

Create this new link

Inserting at a specific location in the list (after the node)


-

Step 1 create a newnode with given value


Step 2 check whether list is empty (head = = null)
Step 3 if it is empty then set newnode next = null and head = newNode,
Step 4 if it is not empty then, define a node pointer temp and initialize with head
Step 5 Keep moving the temp o the next node until it reaches the node after
which we want to inset the new node (until temp data is equal to location ,
here location is the node value after which we want to insert the newNode).
Step 6 Every time check whether temp has reached to the last node or not. If it has
reached the last node, then display Given node is not found in the list!!! Insertion
not possible!!! and terminate the function. Otherwise move the temp to the next
node.
Step 7 Finally set newNode next = tempnext and tempnext = newNode

Example:

head

temp
location

V
New name

Definition of a node
In a single linked list, the deletion operation can be performed in three ways. They are as
follows:
Deleting from the beginning of the list First create a temporary node pointer
that points to the head of a list. We then change the linked lists head pointer.
Finally, we delete the front of the list by deleting the node that the temporary
pointer points to (its points to the front node)

NULL

Diagram Explain:
In 1 we create our temporary pointer. IN 2 we move our head pointer one node
(3), we delete the node that the temporary pointer points to.
Deleting from the end of the list - The solution is to start at the front of the list
and move to the node that is before the last node

Diagram Explain:
Now our temporary node pointer points to the node that will become the tail of
our list. This nodes Next pointer also just so happens to point to the current tail.
All we have to do now is delete the node that our current nodes (the one that our
temporary pointer points to) Next pointer points to (which is the current tail), set
the Next pointer to NULL, and make our linked lists tail pointer point to the node
our temporary pointer points to. Heres the picture:

Diagram Explain
In (1), we have a pointer to the node that will become the new tail. In (2), we
delete the current tail. In (3), we change the linked lists tail pointer to point to the
new tail.

Deleting a specific node Removing a node from the middle of a list is a lot like
removing a node from the end. We create a temporary node pointer and move it to
the node before the one we want to delete. Now we have to be careful though.
Lets see what happens when we just delete the node like we did with the tail
node.

Here weve delete the third node in our list, in doing so, weve lost all reference to
the fourth node in the list. What we need is another temporary node pointer to
point to the 4th node while we delete the 3rd node. That way, we can reconnect our
list.
Diagram Explain
In (1), we have two temporary variables pointing to the nodes before and after the
node well be deleting it. In (2), we delete the node, in (3), we set the node before
the deleted node to the node after the deleted node. After this process, well have
a properly connected list, minus the deleted node.

Display single linked list


We can use the following steps to display the elements of a single li ked list
Step 1 check whether list is empty (head==NULL)
Step 2 If it is empty then, display list is empty!!! And terminate the function
Step 3If it is Not Empty then, define the node pointer temp and initialize with
head
Step 4 Keep display temp data with an arrow () until temp reachse to the
last node
Step 5 Finally display temp data with an arrow to NULL
(tempdataNULL).

Data Structures Stacks

Stack is a linear data structure in which the insertion and deletion operations are
performed at only one end. In a stack, adding and removing of elements are
performed at a single position which is known as top. That means from the top
pf the stack. In the stack, the insertion and deletion operations are performed
based on LIFO (Last In First Out).

Top

Tom
Pam

With LIFO the first


in the stack Tom
(the top) will be the
first to be pushed or
popped.

Ben

In a stack, the insertion operation is performed using a function called push and
deletion operation is performed using a function called pop.
In the figure, PUSH and POP operations at top positions in the stack. That means,
both the insertion and the deletion operations are performed at one end (i.e. at top)
If we want to create a stack by inserting 10, 45, 12, 16, 35 and 50. Then 10
becomes the bottom most element and 50 becomes the top most element. Top is
50 as shown in the image below:

50
Push 50 ( Because
there is still space in
the stack 50 will be
inserted on top of 35
and will become the
new Top)

Top
35
16
12
45
10

Bottom most element

Pop 50 (Because there


is an element present
in the top (50) , 50
will be deleted and 35
will become the new
Top)

50

Top

35
16
12
45
10

Bottom most element

Operations on a stack
The following operations are performed on the stack
Push (To insert an element on the stack)
Pop (to delete an element from the stack)
Display (to display elements of the stacks)
Stack data structure can be implemented in two ways. They are as follows
Using Array
Using Linked List
When stack is implemented using array, that stack can organize only limited number of elements.
When stack is implemented using linked list, that stack can organize unlimited number of
elements.
Stack Using an Array
Implementing a stack using arrays is very simple, just define a one-dimensional array of specific
size and insert or delete the values into that array by using LIFO principle with the help of a
variable top. Initially top set is set to -1.

Whenever we want to insert a value into the stack, increment the top value by 1 and then insert.
Whenever we want to delete a value from the stack, then delete the top value and decrement the
top value by 1.
Stack Operations using stacks
A stack can be implemented using as follows:
Before implementing actual operation, first follow the steps below to create an empty stack.
-

Step 1: Include all the header files which are used in the program and define a constant
SIZE with specific value.
Step 2: Declare all the functions used in stack implementation
Step 3: Create a one-dimensional array with fixed size (int stack[SIZE])
Step 4: Define a integer variable top and initialize with -1.(int top = -1)
Step 5: In main method display menu with list of operations and make suitable function
calls to perform operation selected by the user on the stack.

Inserting value ito the stack using the PUSH operation


In a stack, push() is a function used to insert an element into the stack. In a stack, the new
element is always inserted at top position. Push function takes one integer value as parameter
and inserts that value into the stack. We can use the following steps to push an element on to the
stack.
Step 1: Check whether stack is FULL. (top==SIZE-1)
Step 2: If it is FULL, then display Stack is FULL!!! Insertion is not possible!!! and
terminate the function.
Step 3: If it is NOT FULL then increment top value by one (top++) and see stack(top) to value
(stack[top]=value).
Deleting using the POP function
in a stack, pop() is a function used to delete an element from the stack. In a stack, the element is
always deleted from the top position. Pop following steps to pop an element from the stack
Step 1: Check whether stack is EMPTY. (top==-1)
Step 2: If it is EMPTY, then display Stack is EMPTY!!! Deletion is not possible!!! and
terminate the function
Step 3: If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--)
Display the elements of a stack using the DISPLAY function
We can use the following steps to display the elements of a stack
Step 1: Check whether Stack is EMPTY, (top==-1)
Step 2: If it is Empty, then display stack is EMPTY!!! and terminate the function.

Step 3: If it is NOT EMPTY then define a variable i and initialize with top. Display stack[i]
value and decrement I value by one (i--).
Step 4 : Repeat the above steps until i reaches a value 0.

Examples.
1. The diagram below shows items stored in a stack. Which diagram gives the state of the
stack after the operations listed are performed in order:

Operations
Pop
Apple

Top

Melon

Pop
Push (Pear)

Mango

Pop
ANSWER

Pop: The current top, Apple, is


deleted from stack and melon
becomes the new Top.
Pop: The current top, Melon, is
deleted from stack and Mango
becomes the new Top.

Mango

Top

Push (pear): Because there is still


space, Pear is entered above
Mango and becomes the new Top.
Pop: The current top Pear is
deleted from stack and Mango
becomes the new Top

2. The diagram below shows three items stored in a stack. Which sequence of operations
would transform the stack from the initial state to the final state shown below.
Initial Stock

Final State

Top

Top

ANSWER:
Pop, Pop, Push (C), Push (A)
Pop: Delete current Top C, new Top A; Pop: Delete current Top A, new Top is B; Push (C):
Enter C into the empty slot above B, C is the new Top; Push (A): Enter A into empty slot
above C, A is now the new Top.

3. The diagram below shows the state of a stack after a series of operations were performed.
What operations were used to reflect the stack shown below:

Adele

Top

Mary
John

ANSWER:
Push (John), Push (Mary), Pop, Push (Mary), Push (Adele)
Push (John): Enter John into empty slot, John is the new Top; Push (Mary): Enter Mary
into the empty slot above John, Mary is now the new Top; Pop: Delete current Top, Mary,
new Top is John; Push (Mary): Enter Mary into empty slot above John, Mary is the new
Top; Push (Adele): Enter Adele into empty slot above Mary, Adele is now the new Top.

Stacks in the form of C Code


#include<stdio.h>
#include<conio.h>
#define MAXSIZE 200

int stack[MAXSIZE]
int top = -1;
int main()
{
Void push(int);
int pop();
int choice =1, i, num;
void clrscr();
While(choice == 1)
{
printf(MAINMENU: \n 1. Add element to stack \n 2. Delete element from
stack\n);
scanf(%d, &choice);
Switch(choice)
{
case 1:
printf(Enter the data);
scanf(%d, &num);
push(num);
break;
case 2: i = pop();
printf(Value returned from pop function is %d, i);

break;
default: printf(Invalid Choice);
}
printf(Do you want to do more operation (1 for yes, any other key to end.);
scanf(%d, &choice);
}//end of outer loop
}//end of main
Void push(int y)
{
If(top > MAXSIZE)
{
printf(STACK FULL);
return;
}
Else
{
top++;
stack[top] = y;
}
}
int pop()
{
Int a;
If (top <= -1)
{
printf(STACK EMPTY);
return 0;
}

Else
{
a = stack[top];
top--;
}
return (a);
}
Code 2
#include<stdio.h>
#define MAXSIZE 100

int stack[MAXSIZE];
int top = 0;
void push(int);
int pop (void);
void display (void);

int main()
{
int choice = 0; val = 0;
do
{
printf("\n \t 1. Push\n");
printf("\t 2. Pop \n");
printf("\t 3. Dislay \n");
printf("\t 4. Exit \n");
printf("\n \t Select your choice:");
scanf("%d", &choice);

Switch (choice)
{
case 1:
printf("\t Element to be Pushed:");
scanf("%d", &val);
push(val);
break;
case 2:
val = pop();
if (val= -1)
printf("\t Popped element: %d\n",val);
break;
case 3:
display();
break;
case 4:
break;
default("\t Wrong Choice");
break;
}
} while (choice != 4);
return 0;
}
void push(int val)
{
if (top < MAXSIZE)
stack[top++] = val;
else

printf("Stack overflow");
}
int pop()
{
int a;
if (top > 0)
{
top--;
a = stack[top];
return a;
}
else
{
printf("Stack is empty");
return -1;
}
}
void display()
{
int i = 0;
if (top > 0)
{
printf("\t Elements are:");
while (i < top)
{
printf("\t %d", stack[i++]);
}
print("\n");

}
else
printf("stock is Empty\n");
}

You might also like