IT T33-Data Structures: SMVEC - Department of Information Technology 1
IT T33-Data Structures: SMVEC - Department of Information Technology 1
UNIT I
Basics: Abstract Data Type (ADT) - Introduction to Data Structures – Representation –
Implementation
Stack and List: Representation of Stack – Implementation – Application – Balancing Symbols –
Conversion of infix to postfix expression – Evaluating a postfix expression - Recursive function call
– Linked list ADT – Implementation using arrays – Limitations – Linked list using dynamic
Variables – Linked implementation of stacks – Circular list – Doubly Linked List
Objective
To introduce the primary data structures and their associated operations
Understand The concepts of linear data structure stack and its applications
To introduce the concept of different types of linked lists
Outcome:
Able to use the different types of data structures , stack and linked list concepts
Example:
Array Structure
int regno[10]; typedef struct Stud
float marks[10]; {
char result[20]; int a;
-----
-----;
}s;
Definition 1: A data structure is logical way of storing and organizing data either in
computer's memory or on the disk storage so that it can be used efficiently.
Definition 2: A data structure is defined as collection of elements and all possible operations
which are required for those set of elements.
Primary data structure is the basic data structure that directly operates upon the machine
instruction. They have different representation on different computers.
Example:
8085 8086 Motorola Dual Core
int 2 byte 3 byte 1 byte 4 byte
float 3 byte 4 byte 5 byte 6 byte
Secondary data structure are more complicated data structures derived from primary data
structures.
Example: array, structure
Tree Graph
A tree is a non-linear dynamic data structure A graph is similar to the tree except that it
that may point to one or more nodes at a has no hierarchical relationship between its
time. adjacent elements.
The Abstract Data Type (ADT) is a set of operations .They are the mathematical abstractions
and define how the set of operations are implemented.
In Abstract Data Type all the implementation details are hidden.
Operations- Operations represent basic operation that can be performed on those elements.
Operations Axioms
createlist() Create an initialized list object.
destroylist() Free the memory allocated for the list.
getdata() Returns the data stored in the current node or index.
getcount() Returns the number of elements in the list.
inserrtfirst() Insert a new element in the first position of the list.
insertintermediate() Insert a new element in any intermediate position of the list.
insertlast() Insert a new element in the last position of the list.
Note: The list ADT can be used as the building block for many other types of abstract data type
such as STACK ADT and QUEUE ADT.
Stack is a linear data structure in which data is added and removed at only one end.
The end from which the elements are added and deleted is referred as the „top’ of the
stack.
Thus, the last element that is pushed into the stack, is the first element to be popped out of
the stack, so it is called as Last In First Out (LIFO).
Stack is also referred as PILES or PUSH down list.
The primary operations that can be carried out on the stack are:
1. PUSH (Insertion)
2. POP (Deletion)
3. PEEK(Display the top of the stack)
4. DISPLAY
PUSH
The procedure of inserting a new element tothe top of the stack is known as Push
Operation.
While implementing push the overflow condition is checked to prevent pushing an element
,when the stack is full
POP
The procedure of removing element from the top of the stack is called Pop Operation
While implementing the pop operation, the underflow condition is checked to prevent pop
out an element ,when the stack is empty.
PEEK
Peek operation is used to display the element from the stack pointed by the point by
top.
While implementing the peek operation, the underflow condition is checked to prevent
peek,when the stack is empty.
DISPLAY
To read or display the element from the stack, traverse in the stack from top to 0th position.
Thee underflow condition of the stack is to be checked to avoid retrieving an element from
empty stack.
Algorithm display(stack,top)
{
if top=-1
{
write "Stack is in underflow condition. Peek operation is not possible.";
return;
}
for i=top to step-1
{
write stack[top];
}
return;
}
Push Operation
Pop Operation
Algorithm pop(struct node *top)
{
struct node *temp;
if top=NULL
{
write "Stack is in underflow condition. Pop operation is not possible.";
return;
}
temp=top;
top=top->link;
write "Delete rollno.";
delete temp;
}
Peek Operation
Algorithm pop(struct node *top)
{
struct node *temp;
if top=NULL
{ write "Stack is in underflow condition. Pop operation is not possible.";
return;
}
write top->data;
return;
}
Display Operation
Algorithm pop(struct node *top)
{
struct node *temp;
if top=NULL
{
write "Stack is in underflow condition. Pop operation is not possible.";
return;
}
temp=top;
while temp!=NULL
{ write temp->data;
temp=temp->link;
}
return;
}
Balanced Parenthesis
Conversion from infix to postfix
Evaluation of arithmetic expression
Recursion
Types of Expression
1. Infix expression: operand1 operator operand2 : a+b
2. Postfix expression: opeand1 operand2 operator: ab+
3. Prefix expression: operator operand1 operand2+ab
Rules which has to be followed while converting infix expression to postfix form :
The expression is to be read from left to right.
Read one character at a time from infix expression.
Make use of stack to store the operators.
There should not be any parenthesis in the postfix form
When it comes to the evaluation of the expression, following rules are used.
pop the data from the stack and return popped data;
}
Example :5 3 2 + 8 * +
Recursive function: A recursive function is defined as a function that calls itself to solve a smaller
version of its task until a final call is made which doesn‟t require a call to itself.
Application using Recursion: Tower of Hanoi is one of the main applications of a recursion. It
says that, "if you can solve n-1 cases, then you can easily solve the nth case.
Every program to be executed the main is loaded into a program stack at the beginning of
execution. It remains there until it completes, at which time it is popped off of the stack.
If main calls a function, that function is loaded onto the top of the stack and remains there until it
is complete at which point it is popped off of the stack.
A recursive function calls itself. That means another instance of the function will be placed on
the stack and will remain there until the function completes.
Array
The number of elements in the array is fixed and it is not possible to change the number of
elements in the array because insertion and deletion are not allowed in array.
The size of the list varies continuously when the elements are deleted or inserted.
The list is stored in a part of array. So, an array can be declared large enough to hold
maximum number of elements of a list.
During the execution of the program, the size of the list can be varied within the space
reserved for it.
One end of the array is fixed and other end of the array is constantly changed depending
upon the element inserted and deleted in the list.
Algorithm insertlast()
{
if top=listsize-1 then
{
write "List is full. Insertion is not possible";
return;
}
read rollno;
top=top+1;
roll[top]=rollno;
return;
}
Algorithm intermediate()
{
if top=listsize-1 then
{
write "List is full. Insertion is not possible.";
return; }
read key;
for i=0 to top
{
if roll[i]=key
{ for j=top to i
{
roll[j+1]=roll[j];
}
}
read rollno;
roll[i]=rollno;
}
top=top+1;
return;
}
A linked list is a linear collection of data elements. These data elements are called nodes.
Every node in the list points to the next node in the list.
Therefore, in a linked list every node contains two types of information:
o Data field - Data field contains actual data of an element to be stored in a list.
o Link/Address field- It is also referred to as next address field which contains the
address of next node in the list. It is a pointer or link to the next node in the list.
External Address- The address of the first node in the list is the external address. This address
is stored in the header pointer which points to the first node in the list. The entire link can be
accessed only with the help of the header pointer.
Internal Address- Internal address is the address stored in each and every node of the linked
list except the last node.
NULL Address- NULL address is the address stored by the NULL pointer from the last node
of the list which indicates the end point of the list.
Creation of a list
Insertion of a node in a list
o Inserting a first node in the list
o Inserting a last node in the list
o Inserting an intermediate node in the list
Deletion of a node in a list
Modification of a node in a list
Searching a node in a list
Traverse of a list
In the linear linked list the node is divided into two parts, first part contains information field and the
second part holds the address of the next node.
struct node
{ int data;
struct node *next;
} *start =NULL;
Start node
Start
X Next
p
Algorithm:
X Next
P
Algorithm:
p=(node*)malloc(sizeof(node));
q = start;
while(key != q->data && q != null)
q=q->next;
p->next = q->next;
q->next=p
Start q
X NULL
P
P=(node*)malloc(sizeof(node));
p->data=X;
p->next=NULL;
if(start == NULL)
start=p;
q=start;
while(q->next!=NULL)
q=q->next;
q->next=p
1.8.5 Deletion:
This operation is used to delete an item from the linked list. Here we have three situations.
Start
P Before Deletion
Start
4 Next 7 NULL
After Deletion
1. Store the address of the preceding node in a pointer variable q. Node to be deleted is
marked as key node.
2. Store the address of the key node in a pointer variable p, so that it can be freed
subsequently.
3. Make the successor of the key node as the successor of the node pointed by q.
q->next = p->next
4. Free the node whose address is stored in the pointer variable p.
free(p)
Start node
q p
If(start->next == NULL)
free(start)
else
q=start;
while(q-> next != NULL)
q=q->next
p=q->next
free(p)
Start node
q
Data NULL
q=start;
while(q!=NULL)
write q->data
q=q->next
Basic Operations:
1. Insertion
2. Deletion
3. Traversal / Display
A queue data structure can be implemented using a circular linked list with a single pointer “rear”
as the front node can be accessed through the rear node
Start node
Address of the
first node
rear P
Algorithm:
If(rear==NULL)
rear=p;
p->next =p;
return p;
else
rear->next = p;
p->next= rear->next;
rear=p;
Data Next
1.9.3 Deletion:
q start
p
Algorithm:
Algorithm:
For some applications, especially for those where it is necessary to traverse the list in both
direction, doubly linked list works much better than singly linked list. Doubly linked list is an
advanced form of singly linked list in which each node contains three fields:
Previous Address Field
Data Field
Next Address Field
The previous link field contains the address of its previous node. This field is also referred
as backward link field. The data field stores the information part of the node. The next field
contains address of its next node in the list. This field is also referred to as forward link field.
Basic operations
1. Insert
2. Delete
3. Traverse
One of the most striking disadvantages of these lists is that the inability to traverse the list in the
backward.
In most of the real world applications it is necessary to reverse the list both in forward
direction and backward direction. The most appropriate data structure for such an application is a
doubly linked list.
A double linked list is one in which all nodes are linked together by multiple number of links
which help in accessing both the successor node and predecessor node form the given node
position. It provides bi-directional traversing.
struct node
{
Int data;
struct node *prev, *next;
} *start=NULL;
Start node
q=(node*)malloc(sizeof(node));
q->data=x;
q->prev=NULL; q->next=NULL
Insertion and deletion are two basic operations on such lists. Consider that a new node pointed to
by p is to be inserted after the node pointed to by q in a doubly linked list. Links of two nodes are
altered during insertion.
Start q
Prev 3 next
Start q
P
Previous link
1.10.3 Algorithm for Insertion at beginning:
insertbeg()
{
p=(node*)malloc(sizeof(node));
p->data=x
p->prev=NULL
p->next=start;
if( start!=NULL)
start ->prev=p;
}
insertend()
{
p=(node*)malloc(sizeof(node));
p->data=x
p->next=NULL;
q=start;
while(q->next!=NULL)
q=q->next;
p->prev=q;
q->next=p
}
When a node, pointed to by P is to be deleted then its predecessor node x and its successor node
y will be affected.
Start X Y
P
Start X Y
p->prev->next=p->next;
p->next->prev=p->prev;
free(p);
Polynomials:
One classic example of an ordered list is a polynomial.
Definition:
A polynomial is the sum of terms where each term consists of variable, coefficient and
exponent.
Various operations which can be performed on the polynomial are,
Addition of two polynomials
Multiplication of two polynomials
Evaluation of polynomials
struct list
{
int coef;
int pow;
struct list *next;
};
struct list *p1=NULL,*p2=NULL,*p=NULL;
while(p1->next || p2->next)
{
if(p1->next)
{
p->pow=p1->pow;
p->coef=p1->coef;
p1=p1->next;
}
if(p2->next)
{
p->pow=p2->pow;
p->coef=p2->coef;
p2=p2->next;
}
p->next=(struct list *)malloc(sizeof(struct list));
p=p->next;
p->next=NULL;
}
}
Definition 1: A data structure is logical way of storing and organizing data either in
computer's memory or on the disk storage so that it can be used efficiently.
Definition 2: A data structure is defined as collection of elements and all possible operations
which are required for those set of elements.
Linked list consists of a series of structures, which are not necessarily adjacent in memory.
Each structure contains the element and a pointer to a structure containing its successor. We call
this theNext Pointer. The last cell‟sNext pointer points to NULL.
It is not necessary to specify the number of elements in a linked list during its declaration
Linked list can grow and shrink in size depending upon the insertion and deletion that
occurs in the list
Insertions and deletions at any place in a list can be handled easily and efficiently
A linked list does not waste any memory space
SMVEC- Department of Information Technology 31
IT T33- Data Structures
Some of the important applications of linked lists are manipulation of polynomials, sparse
matrices, stacks and queues.
11. State the difference between arrays and linked lists (November 2013)
13. List out the basic operations that can be performed on a stack (December 2014)
• Push operation
• Pop operation
• Peek operation
• Empty check
• Fully occupied check
• Infix Notation
• Prefix Notation
• Postfix Notation
16. State the rules to be followed during infix to postfix conversions (December 2014)
Rules which has to be followed while converting infix expression to postfix form :
• The expression is to be read from left to right.
• Read one character at a time from infix expression.
• Make use of stack to store the operators.
• There should not be any parenthesis in the postfix form
The difference between stacks and linked lists is that insertions and deletions may occur
anywhere in a linked list, but only at the top of the stack
18. Mention the advantages of representing stacks using linked lists than arrays
• It is not necessary to specify the number of elements to be stored in a stack during its
declaration, since memory is allocated dynamically at run time when an element is added
to the stack
• Linked list representation of stacks can grow and shrink in size without wasting memory
space, depending upon the insertion and deletion that occurs in the list
• Multiple stacks can be represented efficiently using a chain for each stack
Stack. Because of its LIFO (Last In First Out) property it remembers its „caller‟ so knows
whom to return when the function has to return. Recursion makes use of system stack for storing
the return addresses of the function calls. Every recursive function has its equivalent iterative (non-
recursive) function. Even when such equivalent iterative procedures are written, explicit stack is to
be used.
An abstract data type is a set of operations. ADTs are mathematical abstractions; In ADT‟s
definition ,implementation of operations is not mentioned. Objects such as lists, sets and graphs,
along with their operations can be viewed as abstract data types.
To identify and create useful mathematical entities and operations to determine what
classes of problems can be solved using these entities and operations
• Towers of Hanoi
• Reversing a string
• Balanced parenthesis
• Recursion using stack
• Evaluation of arithmetic expressions
Many languages such as BASIC and FORTRAN do not support pointers. If linked lists are
required and pointers are not available, then an alternative implementation must be used known as
cursor implementation.
Assignment Questions
3. Write an array implementation of self-adjusting lists. In a self-adjusting list, all insertions are
performed at the front. A self-adjusting list adds a find operation, and when an element is
accessed by a find, it is moved to the front of the list without changing the relative order of
the other items.
4. Write a routine to perform polynomial addition using singly linked list.
5. Write a routine to maintain the singly linked list in sorted order.