1. E-528-529, sector-7,
Dwarka, New delhi-110075
(Nr. Ramphal chowk and Sector 9 metro station)
Ph. 011-47350606,
(M) 7838010301-04
www.eduproz.in
Educate Anytime...Anywhere...
"Greetings For The Day"
About Eduproz
We, at EduProz, started our voyage with a dream of making higher education available for everyone. Since
its inception, EduProz has been working as a stepping-stone for the students coming from varied
backgrounds. The best part is – the classroom for distance learning or correspondence courses for both
management (MBA and BBA) and Information Technology (MCA and BCA) streams are free of cost.
Experienced faculty-members, a state-of-the-art infrastructure and a congenial environment for learning -
are the few things that we offer to our students. Our panel of industrial experts, coming from various
industrial domains, lead students not only to secure good marks in examination, but also to get an edge over
others in their professional lives. Our study materials are sufficient to keep students abreast of the present
nuances of the industry. In addition, we give importance to regular tests and sessions to evaluate our
students’ progress.
Students can attend regular classes of distance learning MBA, BBA, MCA and BCA courses at EduProz
without paying anything extra. Our centrally air-conditioned classrooms, well-maintained library and well-
equipped laboratory facilities provide a comfortable environment for learning.
Honing specific skills is inevitable to get success in an interview. Keeping this in mind, EduProz has a career
counselling and career development cell where we help student to prepare for interviews. Our dedicated
placement cell has been helping students to land in their dream jobs on completion of the course.
EduProz is strategically located in Dwarka, West Delhi (walking distance from Dwarka Sector 9 Metro
Station and 4-minutes drive from the national highway); students can easily come to our centre from
anywhere Delhi and neighbouring Gurgaon, Haryana and avail of a quality-oriented education facility at
apparently no extra cost.
Why Choose Edu Proz for distance learning?
• Edu Proz provides class room facilities free of cost.
• In EduProz Class room teaching is conducted through experienced faculty.
• Class rooms are spacious fully air-conditioned ensuring comfortable ambience.
• Course free is not wearily expensive.
2. • Placement assistance and student counseling facilities.
• Edu Proz unlike several other distance learning courses strives to help and motivate pupils to get
high grades thus ensuring that they are well placed in life.
• Students are groomed and prepared to face interview boards.
• Mock tests, unit tests and examinations are held to evaluate progress.
• Special care is taken in the personality development department.
"HAVE A GOOD DAY"
3. Karnataka State Open University
(KSOU) was established on 1st June 1996 with the assent of H.E. Governor of
Karnataka
as a full fledged University in the academic year 1996 vide Government
notification
No/EDI/UOV/dated 12th February 1996 (Karnataka State Open University
Act – 1992).
The act was promulgated with the object to incorporate an Open University at the
State level for the introduction and promotion of Open University and Distance
Education systems in the
education pattern of the State and the country for the Co-ordination and
determination of standard of such systems. Keeping in view the educational
needs of our country, in general, and state in particular the policies and
programmes have been geared to cater to the needy.
Karnataka State Open University is a UGC recognised University of Distance
Education Council (DEC), New Delhi, regular member of the Association of
Indian Universities (AIU), Delhi, permanent member of Association of
Commonwealth Universities (ACU), London, UK, Asian Association of Open
Universities (AAOU), Beijing, China, and also has association with
Commonwealth of Learning (COL).
Karnataka State Open University is situated at the North–Western end of the
Manasagangotri campus, Mysore. The campus, which is about 5 kms, from the
city centre, has a serene atmosphere ideally suited for academic pursuits. The
University houses at present the Administrative Office, Academic Block, Lecture
Halls, a well-equipped Library, Guest House
Cottages, a Moderate Canteen, Girls Hostel and a few cottages providing limited
accommodation to students coming to Mysore for attending the Contact
Programmes or Term-end examinations.
Unit1 Arrays, Pointers and Structures
This unit covers the Definitions and concept of an array, single and double dimension
array, Definition of pointers, Declaring pointer variable, pointer operators, Pointers and
Arrays, Pointers and Functions, structures, Declaring initializing of structure, processing
of structure, Structure with array.
Introduction
The need of the arrays can be best understood by thinking, what if its not there. If we had
to write a program to add three integers, we may declare a,b,c as three integers and
4. display the result as “a+b+c”. What if we had to add 100 numbers only after accepting all
of them? We would need 100 different variables with 100 different identifiers with no of
variable locations as relative of each other. This approach is very cumbersome and
lengthy. Hence is the need of arrays. Arrays are the basis for creating any new data
structures; understanding of arrays is essential and becomes the backbone in this field.
Using arrays one can declare and define multiple variables of same type with one
identifier. For e.g. int a[10] is a declaration of 10 variables which are clustered together.
The pointer are special type of variables that hold the address value. Pointers are special
variables as they simply point to other variable. The pointers are the powerful tool for
instance, are used to setup complicated data structure and are used to link variables
together.
In general variables are defined with its type and these can be find with the addresses for
ordinary variables by using the address operator ‘&’. Thus in C and C++ variables that
tell a computer where data is placed that are know as pointer variable.
Objectives
At the end of this unit, you will be able to understand the:
· Arrays and its usage in programming languages
· Brief introduction of pointers
· Pointer operators and
· Implementation concepts of Pointers using Arrays, Functions
· Brief about the structure and its usages.
1.1 Definition and Concept of an Array
Array is a list or collection of data items which are stored in the form of a table, referred
to by a common variable name. i.e., called “Array name” or “Subscript variable name”.
· Advantage of using an array:
1) Multi huge quantity of data items can be stored under single variable name
2) Arrays saves the memory space
3) Arrays helps to arrange the data (Sorting) items in particular order [Ascending /
descending]
4) Data items searching is faster.
5. Concept of an Array:
Concept of an array can be classified into two types that are :
1. Single dimensional array
2. Double/ multi dimensional array or matrix.
· Concept of Single dimensional Array.
Data items are stored in one single column with specified subscript variable name by
using subscript range.
Example:
Note : In the single dimensional Array , values are stored or arranged Column wise with
its respective range and each element range addressed by common single variable name.
Concept of two Dimensional or Double Dimension Array
In double dimensional array, values are referred with respect to specified Row & column
using subscript variable name. double dimensional array also called as matrix.
Example : Holding the Different Brands Electrical Bulbs quantity with respect to its
wattages(W).
Above Example indicates the 4 / 5 Matrix, all cell values are referred by the subscript
name (Array name) QTY with respect to row and column.
Array Used in ‘C’ Language
6. In any High Level Language[HLL] when we used an array the following 3 sequence
steps must be followed.
1. Defining or Declaring an array (Creating a specified blank table in main memory)
2. Storing values in an Array (By the source accepting values and storing)
3. Reading or retrieving a values from an array for any process task.
Single – Dimensional Arrays (One Dimensional Array)
· Declaration or Defining an Array.
<data type><array name or subscript variable> [< array Range or order >]
(or array type)
Example
int num [10];
char name [20];
float avg [100];
· Initialize An Array:
Example:
int num [10] = {40, 50, 10, 20, 11, 20, 15, 100, 90, 17}
char name [6] = {‘s’, ‘h’, ‘a’, ‘k’, ‘t’, ‘i’}
char Colorx [5] = {‘W’, ‘h’, ‘i’, ‘t’, ‘e’}
float avg[3] = {55.99,78.50,80.70}
Example: Write a program to store 10 salesmen’s amount in an array and find out total
sale & best sales amount.
/* Storing 10 sales amount in an array and
find out total sale & Best sale amount */
#include <iostream.h>
7. #include <conio.h>
main ( )
{
const int n = 10;
int sale_amt [100]; tot_amt; best = 0;
clrscr ( )
/* storing values in an array */
for (i = 0; i< = n ; i + +)
{
printf(“Enter sales amount: n”);
scanf(“%d”, &sale_amt[i]);
}
/* Calculate the total sale & find out best amount */
tot_sale = 0; best = 0;
for (i = 0; i<n; i+ +)
{
prntf(“ n sale amount %d , = %d ” , i, sale_amt [i]);
tot_amt = tot_amt + sale_amt[i];
if sale_amt[i] > best
best = sale_amt [i];
}
/* Printing Total and Best Sale amount */
printf( “ n Total sale amount = %d ”, tot_amt);
8. printf(“ n Best sale amount = %d ” ,best);
}
Sorting an array : [ Using Bubble sorting Technique]
Example : Write program to read N number of observations and print them in ascending
order.
/* sorting an array. */
#include<iostream.h>
#include <conio.h>
main()
{
int num [100], i, j, temp, n;
clrscr( );
printf( “Enter the number of observations:”);
scanf(“%d “, &n);
/* Entering value of observation */
printf( “Enter the Observations = n”;
for (i=0; i<n; i+ +)
{
scanf(“%d”, &num [i]) ;
}
/* sorting observations in ascending order */
for (i = 0; i < n; i + +)
{
for (j = i +1; j < n; j + +)
9. {
if num [i]>num [j]
{
temp = num [i];
num [i] = num [j];
num [j] = temp;
}
}
}
/ * Printing sorted array */
printf( “observations in Ascending order n”);
for (i = 0; i < n; i + +)
printf(“%d n “, num[i] );
}
Example: Write a program to search a given number from an array and display
appropriate message. [linear search]
#include < iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <iomanip.h>
{
/* Program for linear search for given number */
int a[100], i, n, sc, key ,pos;
clrscr( );
10. printf( “Enter the array limit: n”);
scanf(“%d”, &n);
/* Enter array values */
printf( “Enter the elements value n”);
for (i = 0; i< n; i + +);
scanf(“%d”, &a[i]);
printf( “Enter the key value for searching: n ”);
scanf(“%d”, &key);
/* linear search */
sc=0;
for (i = 0; i< n; i + +);
{
if (a[i]==key )
{
sc = 1;
pos=i;
}
}
if (sc ==1)
printf( “ n The element is found in location : %d ”, pos+1).;
else
printf( “ n given value is not found” ) ;
}
11. 1.1.3 Two Dimensional Arrays. [Matrix]
It is possible for array to have two or more dimensions. We shall go through two-
dimensional array only. Two dimensional array is called matrix.
· Declaration of TWO dimensional array
<type> < subscript Name> [ Row Range ] [col Range ];
Example: int a[3][3]; /* declaration of 3/3matrix int type */
float x[3][3]; /* declaration of 3/3matrix float type */
Initialization of an array.
Example: int a[3][2] = {
{10, 15};
{25, 11};
{9, 3};
}
Storing values in an array
with help of two for() statements forming nested loop, we can store the element values in
an array
Reading values from an array for process just printing array values.
12. Key Note for array in C :
1. Selection of array name is similar to selecting a variable name or identifiers in C.
2. The range of subscript start from zero (0) up to specified final value.
3. Subscript range must be +ve integer constant .
Example: Write a program to define order of matrix and find the sum of all elements .
#include < iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <iomanip.h>
main ( )
{
/* Array declaration */
int a[10][10];
int i, j, sum = 0, m, n;
clrcsr ( );
printf( “Enter the order of matrix n” );
printf( “Enter Row Range: n” );
scanf(“%d “,&m);
printf( “Enter Col Range: n”);
scanf(“%d”,&n);
13. /* storing values in an array */
printf( “Enter Elements values n ”);
for (i = 0; i < m; i + +)
{
for (j = 0; j < n; j + +)
scanf(“%d”,&a[i] [j]);
}
/* Printing matrix &finding sum of elements */
printf( “printing given matrix n”)
for (i = 0; i < m; i + +)
{
for (j = 0; j < n; j + +)
{
printf(“%d ”,&a[i] [j]);
sum = sum + a[i] [j];
}
printf(“n”);
}
printf( “sum of all element = %d ”, sum);
}
Self Assessment Questions
1. Define an array? Write its advantages using in Program.
2. Write the syntax of declaration of an array with example of each.
14. 3. What are the points should remember using array in program.
1.2 Pointers
Definition of pointer
“A pointer is a variable that can hold the address of the variables, structures and functions
that are used in the program. It contains only the memory location of the variable rather
than its containts”.
Pointers are used with followings:
1. Basic data type variable.
2. Array Subscript variable.
3. Function names.
4. Structure and Union names.
Advantages of Pointers:
1. Pointers are pointing to different data types and structures
2. Manipulation of data at different memory locations is easier.
3. To achieve a clarity and simplicity
4. More compact and efficient coding.
5. To return multiple value via functions.
6. Dynamic memory allocations.
Declaring a pointer variable
Pointers are declared similar to normal variables, but we must specify when we declare
them what they are going to point to it. We declare a pointer to point to an integer, then it
cant be used to point a floating-point value etc.
Pointer Operators:
To declare and refer a pointer variable, provides two special operators & and *.
15. Types of pointer variable declaration:
Example :
char *cptr; pointer to character type variables
int *iptr; *num pointer to integer type variables
float *fptr; pointer to float type variables
char *name[15] pointer to character array
Note: * symbol is part of the variables type.
Example : long int *x, *y;
float *avg, *ratio; etc.
Example: Program to assign the pointer values. (using operator & and *)
#include< iostream.h>
#include<conio.h>
main( )
{
int *x, y; /* xis pointer to integer variable */
clrscr ( );
y = 10;
x = &y; /* y value stored in pointer x.*/
printf( “Address of y = %d n ” , &y);
16. printf (“value of y = %d n” , y);
printf( “Address of y = %d n ” , x);
printf( “value of y = %d n ”, *x);
}
output
Address of y = 65555
Value of y = 10
Address of y = 65555
Value of y = 10
Note:
i) 65555 is a address of &y it should be unsigned +ve.
ii) last statement value of y indirectly by using *x. *x-value at address stored by x.
Therefore * is called indirection operator when used in conjunction with pointers.
Example: Program to assign the values using operator *and &.
#include <iostream.h>
#include <conio.h>
main()
{
int x, y, *ipt; /* ipt is a pointer to integer variable */
clrscr ( );
x = 8;
ipt = & x; /*Address of x is stored in ipt */
y = *ipt; /* Content of pointer goes to y */
17. printf( “The value of y is = %d n “, y);
}
output
The value of y is = 8
Note: Variable y is assigned to value at the address stored in ipt. since ipt contains
address of x, the value at address of x is 8, so * ipt is equal to 10.
Example: Program to use arithmetic operations with pointers.
#include <iostream.h>
#include <conio.h>
main ( )
{
int a, *ipt; /* ipt is a pointer to integer variable. */
int m, n, k;
clrscr( );
a = 150;
ipt = &a; /* address of a is assign to pointer */
m = (*ipt) + +;
n = (*ipt) – -;
k = (*ipt) + +;
print( “value of m = %d n” ,m);
print( “value of n = %d n ” , n);
print( “value of k = %d n ”,k);
}
Pointers and Arrays
18. There is a close association between pointers and arrays, array elements can be accessed
using pointers.
Example: Program to reads 10 array elements & prints the elements using pointer
technique.
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
main ( )
{
int a[10], *arpt, i;
clrscr( );
printf( “Enter arry valuesn”);
for (i = 0; i < 10; i + +)
scanf(“%d n ”,&a[i]);
/* arpt points to array */
arpt = a;
/* printing by technique 1 */
for (i = 0; i < 10; i + +)
printf(“%d n “ arpt +i);
/*printing by technique 2 */
for (i = 0; i < 10; i + +)
printf(“%d” , *(arpt + +);
}
Note: arpt is a pointer variable, in the first technique, in the for loop *(arpt + i) it start
from 0 element i.e. *(arpt = 0).
19. In the second technique (*arpt = 0) in first cycle then increment operation i is used with
the pointer instead of adding loop index to the pointer.
Example: Program to read n number of element and find the biggest elements among
them.
#include <iostream.h>
#include <conio.h>
main ( )
{
int a[100], *arpt, i; big, n;
clrscr();
printf( “ Enter number of elements: n”);
sacnf(“%d “, &n);
printf( “Enter number of elements: n”);
for (i = 0; i < n; i + +)
scanf(“ %d”, &a[i]);
/*the first element address stored in arpt */
arpt = a;
big =*arpt /* first element value stored in big */
for (i = 1; i < n; i + +)
{
if big <*(arpt + i)
big = *(arpt + i);
}
printf( “The biggest among the elements = n ”, big );
20. }
Pointers used in function
It is mechanism by which pointers can be passed as arguments to the function. Thus, the
data items of the calling program can be accessed by the called program. No values is
copied when pointers are passed as arguments, as in the called by value method. Another
important point is that, if the values are changed in the function this will modify the
original contents of the actual parameters, this is not true in case of call by value method.
When the pointers are passed as an argument we must follow the following points.
a. In the calling program, the function is invoked with a function name and addresses of
actual parameters enclosed within the parenthesis.
Example :
< Function Name>(&var1,&var2,&var3………….&var n)
var à all are actual parameters.
b. In the called program parameter list, each & every formal parameter (pointers) must be
preceeded by an indirection operatore(*)
Example :
<data type> <function Name>(*v1,*v2,*v3…………*vn )
v –> all are formal parameters (pointers)
Example : Program to illustrate the call by reference method to interchange the value of
2 integer variable.
main()
{
int num1,num2;
int interchange( int *n1, int *n2);
printf( “Enter any Two integer numbern”);
scanf(“%d %d “, &num1,&num2);
printf(“before interchanging n);
21. printf(“num1 = %d and num2 = %d”,num1,num2);
interchange(&num1,&num2);
printf(“after interchanging n);
printf(“num1 = %d and num2 = %d” num1, num2);
}
int interchange(int *n1, int *n2)
{
int temp;
temp=*n1;
* n1=*n2;
*n2=temp;
}
Pointers used in an Array
Pointers can be used with array to increase the efficiency of the execution of the program.
Pointers can be used with single dimensional or multi-dimensional arrar.
Pointers using in Single Dimensional Array.: The name of a array itself designates
some memory location & this location in memory is the address of the very first element
of an array, the address of the first element of array & num[0], where num is an array
name.
Example :
Write a program to use an array of 5 elements & illustrate the relationship between
elements of an array & their address.
main()
{
int arrlist[5];
int *ptr,index,value=3;
22. ptr = arrlist;
for(index=0; index<5; index++)
{
*(ptr+index)=value++;
printf(“*(ptr+index)=%dtarrlist(index)=%d n”,*(ptr+index),arrlist[index]);
}
}
Output :
*(ptr+index)= 3 arrlist(index)= 3
*(ptr+index)= 4 arrlist(index)= 4
*(ptr+index)= 5 arrlist(index)= 5
*(ptr+index)= 6 arrlist(index)= 6
*(ptr+index)= 7 arrlist(index)= 7
Example :
Write a program to find the sum of 5 elements static in nature using pointers with
function.
main()
{
static int array[5]={200,400,600,800,1000};
int addnum(int *ptr); / * function protype */
int sum;
sum = addnum(array);
printf(“ Sum of all array elements = %d n”,sum);
}
23. int addnum(int *ptr)
{
int total = 0, index;
for(index=0; index<5; index++)
total +=(ptr+index);
return(total);
}
Self Assessment Questions [ 1.2 to 1.6 ]
1. Define pointer?
2. Write a advantages of using pointers in programs.
3. Explain with an example of Pointers operators.
Structures
Definitions : Structure is a meaningful organized Collection of data items of different
type under a unique name that name we called as structure name.
In ‘C’ declaration of such related data items or fields of different types by using reserve
word ‘struct’ .
1.7.1 Declaration of structure
Each and every structure must be defined or declared before it appears or using in
program.
Syntax: struct <structurer name>
{
<type1> <field/data1>
<type2> <field/data2>
<type3> <field/data3>
………………………
24. ……………………….
<type n> <field/data n>
};
Example :
Struct student
{
int rollno;
char name[30];
char address[30];
char city[15];
float marks;
};
1.7.2 Initialization of structure
Initializing a structure description of structure member is similar to initializing static type
declaration.
Example : structure student={122,”Sakshi”, “Arvind Appt.”,”Manipal”,560};
Embedded Structure declaration : [Nested]
It means that, Structure within the another structure is called an embedded structure.
These type of structure declared mainly in two ways that are:
a) Structure may completely defined within the another structure.
b) There may be a separate structure, the embedded structure declared first and the other
structure declared next.
Example:
25. 1.7.3 Processing of Structure
The process of structure is mainly concerned with the accessing structure member. Each
member of a structure is accessed with .(dot) operator to access a particular member of
the structure, the dot operator must be placed between the name of the structure & the
name of the structure member.
Examples :
emp.emp_name, emp.empno , emp.salary etc.
1. Write a program to accept the student details as roll_no, name, city, marks using
structure and print the details.
struct std
{
int rollno;
char name[30];
char city[15];
int marks;
} st; /* structure definition */ /* st -> is the structure point */
main() /*main program */
{
printf(“enter the Roll no n”);
scanf(“%d “, &st.rollno);
26. printf(“enter the Name n”);
scanf(“%s “, st.name);
printf(“enter the city n”);
scanf(“%d “, st.city);
printf(“enter the Marks n”);
scanf(“%d “, &st.marks);
/* printing details */
printf(“Roll Number : %d”,st.rollno);
printf(“Name : %s”, st.name);
printf(“City : %s”, st.city);
printf(“Marks : %d”,st.marks)
}
1.7.4 Structure used with an Array
However we know that different type of data sets cannot be stored an array, So, to
overcome this disadvantage structure can be stored along with its members in array
structure.
Example: Storing 10 students details structure in an array.
Self Assessment Questions
1. Define Structure ?
27. 2. Write a advantages of Structure over Arrays using in programs.
3. Give one suitable example of Structure using an array.
Summary
Arrays are the basis for creating any new data structures; understanding of arrays is
essential and becomes vital role of programmer while implementing the codes. Using
arrays one can declare and define multiple variables of same type with one identifier. For
e.g. int a[10] is a declaration of 10 variables which are clustered together. It also saves a
memory space, easy for sorting and searching the homogeneous type of data.
The pointer are special type of variables that hold the address value. Pointers are special
variables as they simply point to other variable. The pointers are the powerful tool for
instance, are used to setup complicated data structure and are used to link variables
together.
1.9 Terminal Questions
1. Define Array ? Write the Syntax with example of declaring a single and double
dimension array in ‘C’.
2. Write a ‘C’ program to read N number of observations and print them in ascending
order.
3. Accept an array of elements and divide each element in array by 3.
4. Find total occurrence of the given number ‘n’ in an array of 10 numbers entered by the
user.
5. Numbers in array are stored in linear fashion, find the biggest and the smallest of 10
numbers in the given array.
6. Array elements are stored from 0th location, relocate the elements to start from 4th
location
7. Find occurrence of each number in the array.
8. Check whether the given array is a palindrome or not.
9. Reverse the given array without using extra memory.
10. Store a string in an array and find the frequency of occurrence of each character in the
array.
11. Without using string functions find the length of the string.
28. 12. Define Pointer? Discuss the advantages of using pointers in Program.
13. Explain the pointer operators with an example of each.
14. Illustrates the ‘C’ programs which is represents the pointers with array and pointers
with functions.
15. Define Structure ? Write Syntax with appropriate example for declaration of
Structure.
Unit2 Overview of Data Structures
This unit cover the overview of the Data structure, Definition of Data structure, Data
types and Structured data type, Abstract data type, pre and post conditions, Linear Data
structure, and also discussed the implementation methods using C, Non linear data
structures.
Introduction
Data structures represent places to store data for use by a computer program. As you
would imagine, this describes a spectrum of data storage techniques, from the very
simple to the very complex. We can look at this progression, from the simple to the
complex, in the following way.
At the lowest level, there are data structures supplied and supported by the CPU (or
computer chip), itself. These vary from chip to chip, but are almost always of the very
primitive sort. They typically include the simple data types, such as integers, characters,
floating point numbers, and bit strings. To some extent, the data types supported by a
chip reflect the hardware design of the chip. Things such as, how wide (how many bits)
are the registers, how wide is the data bus, does the ALU have an accumulator, does the
ALU support floating point operations?
At the second level of the data structures spectrum are the data structures supported by
particular programming languages. These vary a lot from language to language. Most
languages offer arrays, and many offer arrays of arrays (matrices). Most of the popular
languages provide support for some sort of record structure. In C these are structs and in
Pascal these are records. A few offer strings as a first class data type (e.g. C++ and Java).
A few languages support linked lists directly in the language (e.g. Lisp and Scheme).
Object oriented languages often offer general lists, stacks, and even trees.
At the top level of this taxonomy are those data structures that are created by the
programmer, using a particular programming language. In this regard, it is important to
29. note what tools are provided by a language to facilitate the implementation of complex
data structures envisioned by a programmer. Things such as arrays, arrays of arrays,
pointers, record structures are all helpful in this regard. Using the available tools, a
programmer can build general lists, stacks, queues, dequeues, tress (of many types),
graphs, sets, and much, much more.
In this book we will focus on those data structures in the top level, those that are usually
created by the application programmer. These are the data structures that. generally,
impact the problem solution and implementation in the most dramatic ways: size,
efficiency, readability , and maintainability .
Objectives
At the end of this unit, you will be able to understand the:
· Meaning and brief introduction of Data Structure
· Discussed the various types of abstract levels
· Brief introduction of Abstract data type and its properties
· Operations and implementations of methods of Pre and Post Conditions.
· Concepts and methods of Linear and Non Linear Data structure.
2.1.1 What is a Data Structure?
A data structure is the organization of data in a computer’s memory or in a file.
The proper choice of a data structure can lead to more efficient programs. Some example
data structures are: array, stack, queue, linked list, binary tree, hash table, heap, and
graph. Data structures are often used to build databases. Typically, data structures are
manipulated using various algorithms.
Based on the concept of Abstract Data Types (ADT), we define a data structure by the
following three components.
1) Operations: Specifications of external appearance of a data structure
2) Storage Structures: Organizations of data implemented in lower-level data structures
3) Algorithms: Description on how to manipulate information in the storage structures to
obtain the results defined for the operations
Working with and collecting information on any subject, it doesn’t take very long before
you have more data than you know how to handle. Enter the data structure. In his book
30. Algorithms, Data Structures and Problem Solving with C, Mark Allen Weiss writes “A
data structure is a representation of data and the operations allowed on that data.”
Webopedia states, “the term data structure refers to a scheme for organizing related
pieces of information.”
Definition of data structure
“a specification, an application and an implementation view of a collection of one or
more items of data, and the operations necessary and sufficient to interact with the
collection. The specification is the definition of the data structure as an abstract data type.
The specification forms the programming interface for the data structure. The application
level is a way of modeling real-life data in a specific context. The implementation is a
concrete data type expressed in a programming language. There may be intermediate
levels of implementation, but ultimately the data structure implementation must be
expressed in terms of the source language primitive data types”.
The Abstract Level
The abstract (or logical) level is the specification of the data structure -the “what” but not
the “how.” At this level. the user or data structure designer is free to think outside the
bounds of anyone programming language. For instance. a linear list type would consist of
a collection of list nodes such that they formed a sequence. The operations defined for
this list might be insert. delete, sort and retrieve.
The Application Level
At the application or user level, the user is modeling real-life data in a specific context. In
our list example. we might specify what kind of items were stored in the list and how
long the list is. The context will determine the definitions of the operations. For example,
if the list was a list of character data, the operations would have a different meaning than
if we were talking about a grocery list.
Implementation Level
The implementation level is where the model becomes compilable, executable code. We
need to determine where the data will reside and allocate space in that storage area. We
also need to create the sequence of instructions that will cause the operations to perform
as specified.
Self Assessment Questions
1. Define data Structure? Explain its three components.
2. Discuss the data structure implementation in terms of the source language primitive
data type.
31. Data Types and Structured Data Type
The definition for the term data type and structured data type and data type consists of
• a domain(= a set of values)
• a set of operations.
Example : Boolean or logical data type provided by most programming languages.
• two values : true, false.
• Many operations including: AND , OR, NOT etc.
Structural and Behavioral Definitions
There are two different approaches to specifying a domain : we can give a structural
definition or can give a behavioral definition. Let us see what these two are like.
Behavioral Definition of the domain for ‘Fraction’
The alternative approach to defining the set of values for fractions does not impose any
internal structure on them. Instead it Just adds an operation that creates fractions out of
other things. such as CREATE_FRACTION(N.D) where N is any integer. D is any non-
zero integer.
The values of type fraction are defined to be the values that are produced by this function
for any valid combination of inputs. The parameter names were chosen to suggest its
intended behavior: CREATE_FRACTION(N.D) should return a value representing the
fraction N/D (N for numerator. D for denominator).
You are probably thinking. this is crazy. CREATE_FRACTION could be any old random
function. how do we guarantee that CREATE_FRACTION(N,D) actually returns the
fraction N/D? The answer is that we have to constrain the behavior of this function. by
relating it to the other operations on fractions. For example, One of the key properties of
multiplication is that: NORMALIZE ((N/D) .(DIN)) = 1/1
This turns into a constraint on CREATE_FRACTION:
NORMALIZE (CREATE_FRACfION(N,D) * CREATE_FRACfION(D,)) =
CREATE_FRACTION(1,1)
So you see CREATE_FRACTION cannot be any old function, its behavior is highly
constrained, because we can write down lots and lots of constraints like this. And that’s
the reason we call this sort of definition behavioral, because the definition is strictly in
32. terms of a set of operations and constraints or axioms relating the behavior of the
operations to one another.
In this style of definition, the domain of a data type -the set of permissible values -plays
an almost negligible role. Any set of values will do, as long as we have an appropriate set
of operations to go along with it.
Common Structures
Let us stick with structural definitions for the moment. and briefly survey the main
kinds of data types, from a structural point of view.
• Atomic Data Types
First of all, there are atomic data types. These are data types that are defined
without imposing any structure on their values. Boolean, our first example, is an
atomic type. So are characters, as these are typically defined by enumerating all
the possible values that exist on a given computer.
• Structured Data Types
The opposite of atomic is structured. A structured data type has a definition that
imposes structure upon its values. As we saw above, fractions normally are a
structured data type. In many structured data types, there is an internal structural
relationship, or organization, that holds between the components. For example, if
we think of an array as a structured type, with each position in the array being a
component, then there is a structural relationship of ‘followed by’: we say that
component N is followed by component N+ 1.
• Structural Relationships
Not all structured data types have this sort of internal structural relationship.
Fractions are structured, but there is no internal relationship between the sign,
numerator, and denominator. But many structured data types do have an internal
structural relationship, and these can be classified according to the properties of
this relationship.
• Linear Structure:
The most common organization for components is a linear structure. A structure
is linear if it has these 2 properties:
Property P1 Each element is ‘followed by’ at most one other element.
Property P2 No two elements are ‘followed by’ the same element.
33. ‘An array is an example of a linearly structured data type‘. We generally write a
linearly structured data type like this:
A->B->C->D (this is one value with 4 parts).
- counter example 1 (violates Pl): A points to B and C B<-A->C
- counter example 2 (violates P2): A and B both point to C A->C<-B
2.2.2 Abstract Data Types
Handling Problems
This implies that the model focuses only on problem related stuff and that you try to
define properties of the problem. These properties include:
• the data which are -affected and
• the operations which are identified by the problem
It is said that “computer science is the science of abstraction.” But what exactly is
abstraction? Abstraction is “the idea of a quality thought of apart from any particular
object or real thing having that quality. For example. we can think about the size of an
object without knowing what that object is. Similarly, we can think about the way a car is
driven without knowing Its model or make.
As an example consider the administration of employees in an institution. The head of the
administration comes to you and ask you to create a program which allows to administer
the employees. Well. this is not very specific. For example, what employee information is
needed by the administration? What tasks should be allowed? Employees are real persons
who can be characterized with many properties; very few are: name. size. date of birth.
shape. social number, room number. hair color, hobbies.
Certainly not all of these properties are necessary to solve the administration problem.
Only some of them are problem specific. Consequently you create a model of an
34. employee for the problem. This model only implies properties which are needed to fulfill
the requirements of the administration. for instance name, date of birth and social
number. These properties are called the data of the (employee) model. Now you have
described real persons with help of an abstract employee.
Of course, the pure description is not enough. There must be some operations defined
with which the administration is able to handle the abstract employees. For example there
must be an operation which allows you to create a new employee once a new person
enters the institution. Consequently, you have to identify the operations which should be
able to be performed on an abstract employee. You also decide to allow access to the
employees’ data only with associated operations. This allows you to ensure that data
elements are always in a proper state. For example you are able to check if a provided
date is valid.
Abstraction is used to suppress irrelevant details while at the same time emphasizing
relevant ones. The benefit of abstraction is that it makes it easier for the programmer to
think about the problem to be solved.
To sum up. abstraction is the structuring of a nebulous problem into well-defined entities
by defining their data and operations. Consequently, these entities combine data and
operations. They are not decoupled from each other.
• Abstract Data Types
A variable in a procedural programming language such as Fortran, Pascal, C,
etc. is an abstraction. The abstraction comprises a number of attributes -name.
address. value. lifetime. scope. type, and size. Each attribute has an associated
value. For example, if we declare an integer variable in C & C++. int x, we
say that the name attribute has value “x” and that the type attribute has value
“int”.
Unfortunately, the terminology can be somewhat confusing: The word “value”
has two different meanings-in one instance it denotes one of the attributes and
in the other it denotes the quantity assigned to an attribute. For example, after
the assignment statement x = 5, the value attribute has the value five.
The name of a variable is the textual label used to refer to that variable in the
text of the source program. The address of a variable denotes is location in
memory. The value attribute is the quantity which that variable represents.
The lifetime of a variable is the interval of time during the’ execution of the
program in which the variable is said to exist. The scope of a variable is the
set of statements in the text of the source program in which the variable is said
to be visible. The type of a variable denotes the set of values which can be
assigned to the value attribute and the set of operations which can be
performed on the variable. Finally. the size attribute denotes the amount of
storage required to represent the variable.
35. The process of assigning a value to an attribute is called binding. When a
value is assigned to an attribute. that attribute is said to be bound to the value.
Depending on the semantics of the programming language, and on the
attribute in question. The binding may be done statically by the compiler or
dynamically at run-time. For example. in Java the type of a variable is
determined at ‘compile time-static binding’. On the other hand, the value of a
variable is usually not determined until ‘run-time-dynamic binding’..
Here we are concerned primarily with the type attribute of a variable. The type
of a variable specifies two sets:
o a set of values; and,
o a set of operations.
For example, when we declare a variable, say x, of type int, we know that
x can represent an integer in the range (-231, 231-1) and that we can perform
operations on x such as addition, subtraction, multiplication, and division.
The type int is an abstract data type in the sense that we can think about the
qualities of an int apart from any real thing having that quality. In other words, we
don’t need to know how ints are represented nor how the. operations are
implemented to be able to be. able to use them or reason about them.
In designing object-oriented programs, one of the primary concerns of the
programmer is to develop an appropriate collection of abstractions for the
application at hand, and then to define suitable abstract data types to represent
those abstractions. In so doing, the programmer must be conscious of the fact that
defining an abstract data type requires the specification of both a set of values and
a set of operations on those values.
Indeed, it has been only since the advent of the so-called object-oriented
programming languages that the we see programming languages which provide
the necessary constructs to properly declare abstract data types. For example, in
Java, the class construct is the means by which both a set of values and an
associated set of operations is declared. Compare this with the struct construct of
C or Pascal’s record, which only allow the specification of a set of values!
Properties of Abstract Data Types
The example of the quoted before shows, that with abstraction you create a well-
defined entity which can be properly handled. These entities define the data
structure of a set of items. For example, each administered employee has a name,
date of birth and social number. The data structure can only be accessed with
defined operations. This set of operations is called interface and abstract data type
is exported by the entity. An entity with the properties just described is called an
abstract data type (ADT).
36. Let’s try to put the characteristics of an ADT in a more formal way:
Definition An abstract data type (ADT)
is characterized by the following properties:
1. It exports a type.
2. It exports a set of operations. This set is called interface.
3. Operations of the interface are the one and only access mechanism to the
type’s data structure.
4. Axioms and preconditions define the application domain of the type.
With the first property it is possible to create more than one instance of an ADT
as exemplified with the employee example.
Example of the fraction data type, how might we actually implement this data
type in C?
Implementation 1:
typedef struct { int numerator, denominator; } fraction;
main()
{
fraction f;
f.numerator = 1;
f.denominator = 2;
……………
37. }
Implementation 2 :
#define numerator 0
#define denominator 1
typedef int fraction[2];
main()
{
fraction f;
f[numerator] = 1;
f[denominator] = 2;
……………
}
These are just 2 of many different possibilities. Obviously these differences are in
some sense extremely trivial -they do not affect the domain of values or meaning
of the operations of fractions.
Generic Abstract Data Types
ADTs are used to define a new type from which instances can be created. For
instance, one of lists of apples, cars or even lists. The semantically the definition
of a list is always the same. Only the type of the data elements change according
to what type the list should operate on.
This additional information could be specified by a generic parameter which is
specified at instance creation time. Thus an instance of a generic ADT is actually
an instance of a particular variant the ADT. A list of apples can therefore be
declared as follows:
List<Apple> listOfApples;
The angle brackets now enclose the data type for which a variant of the generic
ADT List should be created. ListOf Apples offers the same interface as any other
list, but operates on of type Apple.
38. Notation :
As ADTs provide an abstract view to describe properties of sets of entities, their
use is independent from a particular programming language. We therefore
introduce a notation here. Each ADT description consists of two parts:
o Data: This part describes the structure of the data used in the ADT in an
informal way.
o Operations: This part describes valid operations for this ADT, hence, it
describes its interface. We use the special operation constructor to
describe the actions which are to be performed once an entity of this ADT
is created and destructor to describe the actions which are to be performed
once an entity is destroyed. For each operation the provided arguments as
well as preconditions and postconditions are given.
As an example the description of the ADT Integer is presented. Let k be an
integer expression:
o ADT integer
is
Data
A sequence of digits optionally prefixed by a plus or minus sign. We refer to this
signed whole number as N.
Operations
Constructor
Creates a new integer.
add(k)
Creates a new integer which is the sum of N and k.
Consequently, the postcondition of this operation is sum = N+k. Don’t confuse
this with assign statements as used in programming languages, It is rather a
mathematical equation which yields “true” for each value sum, N and k after add
has been performed.
sub(k)
similar to add. this operation creates a new integer of the difference of both
integer values. Therefore the postcondition for this operation is sum = N-k.
39. Set(k)
Set N to k. The postcondition for this operation is N = k
……
end
The description above is a specification for the ADT Integer. Please notice, that
we use words for names of operations such as “add”. We could use the more
intuitive “+” sign instead, but this may lead to some confusion: You must
distinguish the operation “+” from the mathematical use of “+” in the
postcondition. The name of the operation is just syntax whereas the semantics is
described by the associated pre- and postconditions. However, it is always a good
idea to combine both to make reading of ADT specifications easier.
Real programming languages are free to choose an arbitrary implementation for
an ADT. For example, they might implement the operation add with the infix
operator “+” leading to more intuitive look for addition of integers.
Programming with Abstract Data Types
By organizing our program this way -i.e. by using abstract data types – we can
change implementations extremely quickly: all we have to do is re-implement
three very trivial functions. No matter how large our application is.
In general terms, an abstract data type is a. specification of the values and the
operations that has 2 properties:
1. it specifies everything you need to know in order to use the datatype
2. it makes absolutely no reference to the manner in which the datatype will be
implemented.
When we use abstract data types, our programs into two pieces:
The Application: The part that uses the abstract datatype.
40. The implementation: The part that implements the abstract data type.
These two pieces are completely independent. It should be possible to take the
implementation developed for one application and use it for a completely different
application with no changes.
If programming in teams, implementers and application-writers can work completely
independently once the specification is set.
Specification
Let us now look in detail at how we specify an abstract datatype. We will use ’stack’ as
an example. The data structure stack is based on the everyday notion of a stack, such as a
stack of books, or a stack of plates. The defining property of a stack is that you can only
access the top element of the stack, all the other elements are underneath the top one and
can’t be accessed except by removing all the elements above them one at a time.
The notion of a stack is extremely useful in computer science, it has many applications,
and is so widely used that microprocessors often are stack-based or at least provide
hardware implementations of the basic stack operations.
First, let us see how we can define, or specify, the abstract concept of a stack. The main
thing to notice here is how we specify everything needed in order to use stacks without
any mention of how stacks will be implemented.
Self Assessment Questions
1. Define Structural and Behavioral definitions.
2. Define abstract data type?
3. Discuss the properties of ADT?
Pre and Post Conditions
Preconditions
These are properties about the inputs that are assumed by an operation. If they are
satisfied by the inputs, the operation is guaranteed to work properly. If the preconditions
are not satisfied, the operation’s behavior is unspecified: it might work properly (by
chance), it might return an incorrect answer, it might crash.
Postconditions
Specify the effects of an operation. These are the only things you may assume have been
done by the operation. They are only guaranteed to hold if the preconditions are satisfied.
41. Note: The definition of the values of type ’stack’ make no mention of an upper bound on
the size of a stack. Therefore, the implementation must support stacks of any size. In
practice, there is always an upper bound -the amount of computer storage available. This
limit is not explicitly mentioned, but is understood -it is an implicit precondition on all
operations that there is storage available, as needed. Sometimes this is made explicit, in
which case it is advisable to add an operation that tests if there is sufficient storage
available for a given operation.
Operations
The operations specified before are core operations -any other operation on stacks can be
defined in terms of these ones. These are the operations that we must implement in order
to implement ’stacks’, everything else in our program can be independent of the
implementation details.
lt is useful to divide operations into four kinds of functions:
1. Those that create stacks out of non-stacks, e.g. CREATE_STACK, READ_STACK,
CONVERT_ARRAY _TO_STACK
2. Those that ‘destroy’ stacks (opposite of create) e.g. DESTROY_STACK
3. Those that ‘inspect’ or ‘observe’ a stack, e.g. TOP, IS_EMPTY, WRITE_STACK
4. Those that takes stacks (and possibly other things) as input and produce other stacks as
output, e.g. PUSH, POP
A specification must say what an operation’s input and outputs are, and definitely must
mention when an input is changed. This falls short of completely committing the
implementation to procedures or functions (or whatever other means of creating ‘blocks’
of code might be available in the programming language). Of course, these details
eventually need to be decided in order for code to actually be written. But these details do
not need to be decided until code-generation time; throughout the earlier stages of
program design, the exact interface (at code level) can be left unspecified.
Checking Pre Conditions
It is very important to state in the specification whether each precondition will be
checked by the user or by the implementer. For example, the precondition for POP may
be checked either by the procedure(s) that call POP or within the procedure that
implements POP? Either way is possible. Here are the pros and cons of the 2
possibilities:
User Guarantees Preconditions
42. The main advantage, if the user checks preconditions -and therefore guarantees that they
will be satisfied when the core operations are invoked -is efficiency. For example,
consider the following:
PUSH(S, 1);
POP(S);
It is obvious that there is no need to check if S is empty -this precondition of POP is
guaranteed to be satisfied because it is a postcondition of PUSH.
Implementation Checks Preconditions
There are several advantages to having the implementation check its own preconditions:
1. It sometimes has access to information not available to the user (e.g. implementation
details about space requirements), although this is often a sign of a poorly constructed
specification.
2. Programs won’t bomb mysteriously -errors will be detected (and reported?) at the
earliest possible moment. This is not true when the user checks preconditions, because
the user is human and occasionally might forget to check, or might think that checking
was unnecessary when in fact it was needed.
3. Most important of all, if we ever change the specification, and wish to add, delete, or
modify preconditions, we can do this easily, because the precondition occurs in exactly
one place in our program.
There are arguments on both sides. The literatures specifies that procedures should signal
an error if their preconditions are not satisfied. This means that these procedures must
check their own preconditions. That’s what our model solutions will do too. We will
thereby sacrifice some efficiency for a high degree of maintainability and robustness.
An additional possibility is to selectively include or exclude the implementation’s
condition checking code, e.g. using #ifdef:
#ifdef SAFE
if (! condition) error(”condition not satisfied”);
#endif
This code will get included only if we supply the DSAFE argument to the compiler (or
otherwise define SAFE). Thus, in an application where the user checks carefully for all
preconditions, we have the option of omitting all checks by the implementation.
43. Self Assessment Questions
1. Explain the pre and Post conditions with an suitable example.
2. Discuss the advantages of implementation checks preconditions.
Linear Data Structure
The Array Data Structure
As an example, most programming languages have an array type as one of the built-in
types. We will define an array as a homogeneous, ordered, finite, fixed-length list of
elements. To further define these terms in the context of an array:
a) homogeneous -every element is the same
b) ordered -there is a next and previous in the natural order of the structure c) finite -there
is a first and last element
d) fixed-length -the list size is constant
Mapping the array to the three levels of a data structure:
1. At the abstract level
· Accessing mechanism is direct, random access
· Construction operator
· Storage operator
· Retrieval operator
2. At the application level
· Used to model lists (characters, employees. etc).
3. At the implementation level
· Allocate memory through static or dynamic declarations
· Accessing functions provided -[ ] and =.
Using an Array and Lists as a Data Structure
An array can be used to implement containers.
44. Given an index (i.e. subscript), values can be quickly fetched and/or stored in an array.
Adding a value to the end of an array is fast (particularly if a variable is used to indicate
the end of the array); however, inserting a value into an array can be time consuming
because existing elements must be rotated.
Since array elements are typically stored in contiguous memory locations, looping
through an array can be done easily and efficiently.
When elements of an array are sorted, then binary searching can be used to find particular
values in the array. If the array elements are not sorted, then a linear search must be used.
After an array has been defined, its length (i.e. number of elements) cannot be changed.
Arrays: Fast and Slow
The following are some comments on the efficiency of arrays:
a) Changing the length of an array can be slow.
b) Inserting elements at the end of an array is fast (assuming the index of the end-of array
is stored; if you have to search for the end-of-array, then this operation is slow).
c) Inserting elements near the beginning of an array can be slow.
d) Accessing an array element using an index is fast.
e) Searching a non-sorted array for a value can be slow.
f) Searching a sorted array for a value can be fast.
Elementary Data Structures
“Mankind’s progress is measured by the number of things we can do without thinking.”
Elementary data structures such as stacks, queues, lists, and heaps will be the “of-the-
shelf’ components we build our algorithm from. There are two aspects to any data
structure:
1) The abstract operations which it supports.
2) The implementation of these operations.
The fact that we can describe the behavior of our data structures in terms of abstract
operations explains why we can use them without thinking, while the fact that we have
different implementation of the same abstract operations enables us to optimize
performance.
45. In this book we consider a variety of abstract data types (ADTs), including stacks,
queues, deques, ordered lists, sorted lists, hash tables, trees, priority queues. In just about
every case, we have the option of implementing the ADT using an array or using some
kind of linked data structure.
Because they are the base upon which almost all of the ADTs are built, we call the array
and the linked list the foundational data structures. It is important to understand that we
do not view the array or the linked list as ADTs, but rather as alternatives for the
implementation of ADTs.
Arrays
Probably the most common way to aggregate data is to use an array. In C an array is a
variable that contains a collection of objects, all of the same type.
For example, int a[5]; allocates an array of five integers and assigns it to the variable a.
The elements of an array are accessed using integer-valued indices. In C the first element
of an array always has index zero. Thus, the five elements of array a are a[0]
,a[1]…..a[4]. All arrays in C have a length, the value of which is equal to the number of
array elements.
How are C arrays represented in the memory of the computer? The specification of the C
language leaves this up to the system implementers. However, Figure illustrates a typical
implementation scenario.
The elements of an array typically occupy consecutive memory locations. That way given
i, it is possible to find the position of a[I] in constant time. On the basis of Figure. we can
now estimate the total storage required to represent an array. Let S(n) be the total storage
(memory) needed to represent an array of n ints. S(n) is given by
S(n) ³ size of (int[n]) ³ (n+ 1) size of (int.)
where the function size of (x) is the number of bytes used for the memory representation
of an instance of an object of type x.
In C the sizes of the primitive data types are fixed constants. Hence size of (int.) = 0(1)
46. In practice. an array object may contain additional fields. For example. it is reasonable to
expect that there is a field which records the position in memory of the first array
element. In any event the overhead associated with a fixed number of fields is 0(1).
Therefore, S(n)=O(n).
Multi-Dimensional Arrays
A multi-dimensional array of dimension n (i.e. an n-dimensional array or simply n-D
array) is a collection of items which is accessed via n subscript expressions. For example.
in a language that supports it. (i, j)th the element of the two-dimensional array x is
accessed by writing x[i,j].
The C programming language does not really support multi-dimensional arrays. It does
however support arrays of arrays. In C a two-dimensional array x is really an array of
one- dimensional arrays:
int x[3][5];
The expression x[i] selects the ith one-dimensional array; the expression x[i][j]selects the
j th element from that array.
The built-in multi-dimensional arrays suffer the same indignities that simple one-
dimensional arrays do: Array indices in each dimension range from zero to length –1,
where length is the array length in the given dimension. There is no array assignment
operator. The number of dimensions and the size of each dimension is fixed once the
array has been allocated.
Self Assessment Questions
1. Write the advantages of linear data structure.
2. Write points on the efficiency of arrays in contact to data structure.
What the application needs ?
Terms describing the data structure from the point of view of the application. which only
cares how it behaves and not how it is implemented.
List
Generic term for a collection of objects. May or may not contain duplicates. Application
may or may not require that it be kept in a specified order.
Ordered list
47. A list in which the order matters to the application. Therefore for example. the
implementer cannot scramble the order to improve efficiency.
Set
List where the order does not matter to the application (implementer can pick order so as
to optimize performance) and in which there are no duplicates.
Multi-set
Like a set but may contain duplicates.
Double-ended queue (dequeue)
An ordered list in which insertion and deletion occur only at the two ends of the list. That
is elements cannot be inserted into the middle of the list or deleted from the middle of the
list.
Stack
An ordered list in which insertion and deletion both occur only at one end (e.g. at the
start).
Queue
An ordered list in which insertion always occurs at one end and deletion always occurs at
the other end.
Ordered Lists and Sorted Lists
The most simple yet one of the most versatile containers is the list. In this section we
consider lists as abstract data types. A list is a series of items. In general, we can insert
and remove items from a list and we can visit all the items in a list in the order in which
they appear.
In this section we consider two kinds of lists-ordered lists and sorted lists. In an ordered
list the order of the items is significant. The order of the items in the list corresponds to
the order in which they appear in the book. However, since the chapter titles are not
sorted alphabetically, we cannot consider the list to be sorted. Since it is possible to
change the order of the chapters in book, we must be able to do the same with the items
of the list. As a result, we may insert an item into an ordered list at any position.
On the other hand, a sorted list is one in which the order of the items is defined by some
collating sequence. For example, the index of this book is a sorted list. The items in the
index are sorted alphabetically. When an item is inserted into a sorted list, it must be
inserted at the correct position.
48. Ordered Lists
An ordered list is a list in which the order of the items is significant. However, the items
in an ordered lists are not necessarily sorted. Consequently, it is possible to change the
order of items and still have a valid ordered list.
A searchable container is a container that supports the following additional operations:
1) insert: used to put objects into the container;
2) withdraw: used to remove objects from the container;
3) find: used to locate objects in the container;
4) isMember: used to test whether a given object instance is in the container.
Sorted Lists
The next type of searchable container that we consider is a sorted list. A sorted list is like
an ordered list: It is a searchable container that holds a sequence of objects. However, the
position of an item in a sorted list is not arbitrary .The items in the sequence appear in
order, say, from the smallest to the largest. Of course, for such an ordering to exist, the
relation used to sort the items must be a total order.
Lists-Array Based Implementation :
Deleting and inserting an item requires moving up and pushing down the existing items
(O(n) in the worst case)
Linked Lists
Makes use of pointers, and it is dynamic. Made up of series of objects called the nodes.
Each node contains a pointer to the next node. This is remove process (insertion works in
the opposite way).
49. Comparison of List Implementations
Array-Based Lists: [Average and worst cases]
· Insertion and deletion are O(n).
· Direct access is O(1)
· Array must be allocated in advance
· No overhead if all array positions are full
Linked Lists:
· Insertion and deletion O(1)
· Direct access is O(n)
· Finding predecessor is O(n)
· Space grows with number of elements
· Every element requires overhead.
Linked Lists
Elements of array connected by contiguity
· Reside in contiguous memory
· Static (compile time) allocation (typically)
Elements of linked list connected by pointers
· Reside anywhere in memory
50. · Dynamic (run time) allocation
Implementation methods
There are a variety of options for the person implementing a list (or set or stack or
whatever).
a) array
We all know what arrays are. Arrays are included here because a list can be implemented
using a I D array. If the maximum length of the list is not known in advance. code must
be provided to detect array overflow and expand the array. Expanding requires allocating
anew, longer array, copying the contents of the old array, and deallocating the old array.
Arrays are commonly used when two conditions hold. First the maximum length of the
list can be accurately estimated in advance (so array expansion is rarely needed). Second,
insertion and deletion occur only at the ends of the list. (Insertion and deletion in the
middle of an array-based list is slow.)
b) linked list
A list implemented by a set of nodes, each of which points to the next. An object of class
(or struct) “node” contains a field pointing to the next node, as well as any number of
fields of data. Optionally, there may be a second “list” class (or struct) used as a header
for the list. One field of the list class is a pointer to the first node in the list. Other fields
may also be included in the “list” object, such as a pointer to the last node in the list, the
length of the list, etc.
Linked lists are commonly used when the length of the list is not known in advance
and/or when it is frequently necessary to insert and/or delete in the middle of the list.
c) doubly-linked vs. singly-linked lists
In a doubly-linked list, each node points to the next node and also to the previous node.
In a singly-linked list, each node points to the next node but not back to the previous
node.
d) circular list
A linked list in which the last node points to the first node. If the list is doubly-linked, the
first node must also point back to the last node.
Non Linear Data Structures
Trees
51. we consider one of the most Important non-linear Information structures- trees. A tree Is
often used to represent a hierarchy. This is because the relationships between the Items In
the hierarchy suggest the branches of a botanical tree.
For example, a tree-like organization charts often used to represent the lines of
responsibility in a business as shown in Figure. The president of the company is shown at
the top of the tree and the vice-presidents are indicated below her. Under the vice-
presidents we find the managers and below the managers the rest of the clerks. Each clerk
reports to a manager. Each manager reports to a vice-president, and each vice-president
reports to the president.
It just takes a little imagination to see the tree in Figure. Of course. The tree is upside-
down. However, this is the usual way the data structure is drawn. The president is called
the root of the tree and the clerks are the leaves.
A tree is extremely useful for certain kinds of computations. For example. Suppose we
wish to determine the total salaries paid to employees by division or by department. The
total of the salaries in division A can be found by computing the sum of the salaries paid
in departments Al and A2 plus the salary of the vice-president of division A. Similarly.
The total of the salaries paid in department Al is the sum of the salaries of the manager of
department Al and of the two clerks below her.
Clearly, in order to compute all the totals. It is necessary to consider the salary of every
employee. Therefore, an implementation of this computation must visit all the employees
in the tree. An algorithm that systematically visits all the items in a tree is called a tree
traversal.
In the same chapter we consider several different kinds of trees as well as several
different tree traversal algorithms. In addition. We show how trees can be used to
represent arithmetic expressions and how we can evaluate an arithmetic expression by
doing a tree traversal. The following is a mathematical definition of a tree:
Definition (Tree) A tree T is a finite. Non-empty set of nodes ,
T = {r} U TI, U T2 U …U Tn with the following properties:
1. A designated node of the set, r, is called the root of the tree: and
52. 2. The remaining nodes are partitioned into n≥ O subsets T, T. …Tn each of which is a
tree for convenience, we shall use the notation T= {r. T, T, …T} denote the tree T.
Notice that Definition is recursive-a tree is defined in terms of itself! Fortunately, we do
not have a problem with infinite recursion because every tree has a finite number of
nodes and because in the base case a tree has n=0 subtrees.
It follows from Definition that the minimal tree is a tree comprised of a single root node.
For example Ta = {A}.
Finally. The following Tb = {B, {C}} is also a tree
Ta = {D, {E. {F}}, {G.{H,II}}, {J, {K}. {L}}, {M}}}
How do Ta Tb. & Tc resemble their arboreal namesake? The similarity becomes apparent
when we consider the graphical representation of these trees shown in Figure. To draw
such a pictorial representation of a tree, T = {r. T1 ,T2, …Tn, beside each other below the
root. Finally, lines are drawn from rto the roots of each of the subtrees. T1T2…….Tn
Figure : Examples of trees.
Of course, trees drawn in this fashion are upside down. Nevertheless, this is the
conventional way in which tree data structures are drawn. In fact, it is understood that
when we speak of “up” and “down,” we do so with respect to this pictorial
representation. For example, when we move from a root to a subtree, we will say that we
are moving down the tree.
The inverted pictorial representation of trees is probably due to the way that genealogical
lineal charts are drawn. A lineal chart is a family tree that shows the descendants of some
person. And it is from genealogy that much of the terminology associated with tree data
structures is taken.
Figure shows one representation of the tree Tc defined in Equation. In this case, the tree
is represented as a set of nested regions in the plane. In fact, what we have is a Venn
diagram which corresponds to the view that a tree is a set of sets.
53. Figure: An alternate graphical representation for trees.
Binary Tree
Used to implement lists whose elements have a natural order (e.g. numbers) and either (a)
the application would like the list kept in this order or (b) the order of elements is
irrelevant to the application (e.g. this list is implementing a set).
Each element in a binary tree is stored in a “node” class (or struct). Each node contains
pointers to a left child node and a right child node. In some implementations, it may also
contain a pointer to the parent node. A tree may also have an object of a second “tree”
class (or struct) which as a header for the tree. The “tree” object contains a pointer to the
root of the tree (the node with no parent) and whatever other information the programmer
wants to squirrel away in it (e.g. number of nodes currently in the tree).
In a binary tree, elements are kept sorted in left to right order across the tree. That is if N
is a node, then the value stored in N must be larger than the value stored in left-child(N)
and less than the value stored in right-child(N). Variant trees may have the opposite order
(smaller values to the right rather than to the left) or may allow two different nodes to
contain equal values.
Hash Tables
A very common paradigm in data processing involves storing information in a table and
then later retrieving the information stored there. For example, consider a database of
driver’s license records. The database contains one record for each driver’s license
issued. Given a driver’s license number. we can look up the information associated with
that number. Similar operations are done by the C compiler. The compiler uses a symbol
table to keep track of the user-defined symbols in a Java program. As it compiles a
program, the compiler inserts an entry in the symbol table every time a new symbol is
declared. In addition, every time a symbol is used, the compiler looks up the attributes
associated with that symbol to see that it is being used correctly.
Typically the database comprises a collection of key-and-value pairs. Information is
retrieved from the database by searching for a given key. In the case of the driver’~
license database, the key is the driver’s license number and in the case of the symbol
table, the key is the name of the symbol.
54. In general, an application may perform a large number of insertion and/ or look-up
operations. Occasionally it is also necessary to remove items from the database. Because
a large number of operations will be done we want to do them as quickly as possible.
Hash tables are a very practical way to maintain a dictionary. As with bucket sort, it
assumes we know that the distribution of keys is fairly well-behaved.
Once you have its index. A hash function is a mathematical function which maps keys to
integers.
In bucket sort, our hash function mapped the key to a bucket based on the first letters of
the key. “Collisions” were the set of keys mapped to the same bucket. If the keys were
uniformly distributed. then each bucket contains very few keys!
The resulting short lists were easily sorted, and could just as easily be searched
We examine data structures which are designed specifically with the objective of
providing efficient insertion and find operations. In order to meet the design objective
certain concessions are made. Specifically, we do not require that there be any specific
ordering of the items in the container. In addition, while we still require the ability to
remove items from the container, it is not our primary objective to make removal as
efficient as the insertion and find operations.
Ideally we would’ build a data structure for which both the insertion and find operations
are 0(1) in the worst case. However, this kind of performance can only be achieved with
complete a priori knowledge. We need to know beforehand specifically which items are
to be inserted into the container. Unfortunately, we do not have this information in the
general case. So, if we cannot guarantee 0(1) performance in the worst case, then we
make it our design objective to achieve 0(1) performance in the average case.
The constant time performance objective immediately leads us to the following
conclusion: Our implementation must be based in some way Kh element of an array in
constant time, whereas the same operation in a linked list takes O{k) time.
In the previous section, we consider two searchable containers-the ordered list and the
sorted list. In the case of an ordered list, the cost of an insertion is 0(1) and the cost of the
find operation is O(n). For a sorted list the cost of insertion is O(n) and the cost of the
find operation is O(log n) for the array implementation.
Clearly, neither the ordered list nor the sorted list meets our performance objectives. The
essential problem is that a search, either linear or binary, is always necessary. In the
ordered list, the find operation uses a linear search to locate the item. In the sorted list, a
55. binary search can be used to locate the item because the data is sorted. However, in order
to keep the data sorted, insertion becomes O(n).
In order to meet the performance objective of constant time insert and find operations. we
need a way to do them without performing a search. That is, given an item x, we need to
be able to determine directly from x the array position where it is to be stored.
Hash Functions
It is the job of the hash function to map keys to integers. A good hash function:
1. Is cheap to evaluate
2. Tends to use all positions from O…M with uniform frequency.
3. Tends to put similar keys in different parts of the tables (Remember the Shifletts!!)
The first step is usually to map the key to a big integer, for example
k=wth
h = S 1284 x char (key[I])
1=0
This last number must be reduced to an integer whose size is between 1 and the size of
our hash table. One way is by h(k) = k mod M where M is best a large prime not too
close to 2i -1, which would just mask off the high bits. This works on the same principle
as a roulette wheel!
Self Assessment Questions
1. Define Trees. Discuss its usage in different applications.
2. Write note on:
a) Binary Tree b) Hash Tables
Summary
This unit covers all overview and concepts of data structure with its applications. Data
structures represent places to store data for use by a computer program. As you would
imagine, this describes a spectrum of data storage techniques, from the very simple to the
very complex. We can look at this progression, from the simple to the complex, At the
lowest level, there are data structures supplied and supported by the CPU (or computer
chip), itself. These vary from chip to chip, but are almost always of the very primitive
56. sort. They typically include the simple data types, such as integers, characters, floating
point numbers, and bit strings. On these contacts discussed the various structured data
types, Abstract data types, Linear and non linear data structure.
Terminal Questions
1. Define Data Structure? Explain the types of structured data type.
2. Explain Abstract data types with its characteristics.
3. Discuss the linear data structure with suitable example.
4. Discuss the various types of data structure applications.
5. Write note on:
a) Elementary Data Structures
b) Ordered list
c) Linked list
d) Queue
e) Slack
f) Binary tree
g) Hash tables
Unit3 Overview of Stack
In this unit discussed the Overview of Stack and its operations, related algorithms for
push and pop, various stack implementation arrays, structures using C. Illustration of
stack operation implementation using ‘C’,
Introduction
Definitions and operations:
We know that in a cafeteria the plates are placed one above the other and every new plate
is added at the top. When a plate is required, it is taken off from the top and it is used. We
57. call this process as stacking of plates. Thus, the operations that can be performed if plates
are stacked are:
· Addition/insertion of plate at one end
· Deletion of plate at the same end
Using this analogy a stack is defined as a special type of data structure where items are
inserted from one end called top of stack and items are deleted from the same end. Here,
the last item inserted will be on top of stack. Since deletion is done from the same end,
Last item Inserted is the First item to be deleted Out from the stack and so, stack is also
called Last In First Out (LIFO) data structure.
Objectives
At the end of this unit, you will be able to understand the:
• Stack Definition and its operations
• POP and PUSH operation implementation in C
• Various stack applications
• Stack implementation using Arrays and Structure
Operations of Stack
The various operations that can be performed on stacks are:
· Insert an item into the stack
· Delete an item from the stack
· Display the contents of the stack
From the definition of stack it is clear that it is a collection of similar type of items and
naturally we can use an array (An array is a collection of similar data types) to hold the
items of stack. Since array is used, its size is fixed. So, let us assume that 5 items 30, 20,
25, 10 and 40 are to be placed on the stack. The items can be inserted one by one as
shown in following figure.
It is clear from this figure that initially stack is empty and top points to
bottom of stack. As the items are inserted top pointer is incremented and it points to the
topmost item. Here, the items 30, 20, 25, 10 and 40 are inserted one after the other. After
58. inserting 40 the stack is full. In this situation it is not possible to insert any new item. This
situation is called
stack overflow. When an item is to be deleted, it should be deleted from the top as shown
in following figure.
Since items are inserted from one end, in stack deletions should be done from the same
end. So, as the items are deleted, the item below the top item becomes the new top item
and so the position of the top most item is decremented as shown in above figure The
items deleted in order are 40, 10, 25, 20 and 30. Finally, when all items are deleted, top
points to bottom of stack. When the stack is empty, it is not possible to delete any item
and this situation is called under flow of stack.
So, the main operations to be performed on stacks are insertion and deletion. Inserting an
item into the stack when stack is not full is called push operation and deleting an item
from the stack when stack is not empty is called pop operation. Other operations that can
be performed are display the contents of the stack, check whether the stack is empty or
not, etc., Let us see how push and pop operations are implemented.
Self Assessment Questions
1. Define stack with its different operations.
2. Discuss the stack Insertion and deletion of element from/to stack with suitable
example.
Insert/Push operation
To design a C function, to start with let us assume that three items are already added to
the stack and stack is identified by s as shown in figure a.
59. Here, the index top points to 30 which is the topmost item. Here, the value of top is 2.
Now, if an item 40 is to be inserted, first increment top by 1 and then insert an item. The
corresponding C statements are:
top = top + 1;
s[top] = item;
These two statements can also be written as
s[+ + top] = item
But, as we insert an item we must take tare of the overflow situation i.e., when top
reaches STACK_SIZE-l, stack results in overflow condition and appropriate error
message has to be returned as shown below:
if (top == STACK_SIZE -1)
{
printf(”Stack overflown”);
return;
}
Here, ST ACK_SIZE should be #defined and is called symbolic constant the value of
which cannot be modified. If the above condition fails, the item has to be inserted. Now,
the C code to insert an item into the stack can be written as
if (top == ST ACK_SIZE -1)
{
printf(”Stack overflown”);
return;
}
s[ + + top] = item;
It is clear from this code that as the item is inserted, the contents of the stack identified by
s and top are affected and so they should be passed and used as pointers as shown in
below example
60. Example 1: C function to insert an integer item
void push(int item, int *top, int s[])
{
if (*top == STACK_SIZE -1)
{
printf(”Stack overflown”);
return;
}
s[+ +(*top)] = item; /* Increment top and then insert an item */
}
Note: In above Example inserts an item of integer data type into the stack. To insert an
item of character data type, the changes done are provided in below example.
Example 2: C function to insert a character item
void push(char item, int *top, char s[])
{
if (*top == ST ACK_SIZE -1)
{
printf(”Stack overflown”);
return;
}
s[+ +(*top)] = item; /* Insert an item on the stack */
}
Delete/Pop operation
61. Deleting an element from the stack is called ‘pop’ operation. This can be achieved by
first accessing the top element s[top] and then decrementing top by one as shown below:
item = s[top--];
Each time, the item is deleted, top is decremented and finally, when the stack is empty
the top will be -1 and so, it is not possible to delete any item from the stack. The above
statement has be executed only if stack is not empty. Hence, the code to delete an item
from stack can be written as
if (top == -1)
{
return -1; /* Indicates empty stack */
}
/* Access the item and delete */
item = s[top--]; .
return item;
As the value of top changes every time the item is deleted, top can be used as a pointer
variable. The complete function is shown in below example 1. The example 2 shows how
to delete a character item from the stack.
Example 1: C function to delete an integer item
int pop(int *top, int s[ ] )
{
int item;
if (*top == -1)
{
return 0; /* Indicates empty stack */
}
item = s[(*top)--];/* Access the item and delete */
62. return item; /* Send the item deleted to the calling function */
}
Example 2: C function to delete a character item
char pop(int *top, chars[])
{
char item;
if(*top= =-1)
{
return 0; /* Indicates empty stack */
}
item = s[(*top)--];/* Access the item and delete */
return item; /* Send the item deleted to the calling function */
}
Display
Assume that the stack contains three elements as shown below:
The item 30 is at the top of the stack and item 10 is at the bottom of the stack. Usually,
the contents of the stack are displayed from the bottom of the stack till the top of the
stack is reached. So, first item to be displayed is 10, next item to be displayed is 20 and
final item to be displayed is 30. So, the code corresponding to this can take the following
form
63. for (i = 0; i <= top; i+ +)
{
printf(”%dn”, s[i]);
}
But, the above statement should not be executed when stack is empty i.e., when top takes
the value -1. So, the modified code can be written as shown in below example.
Example 1: C function to display the contents of the stack
void display(int top, int s[])
{
int i;
if(top= = -1)
{
printf(”Stack is emptyn”);
return;
}
printf(”Contents of the stackn”);
for (i = 0; i <= top; i++)
{
printf(”%dn”, s[i]);
}
}
Self Assessment Questions
1. Explain the POP and PUSH operations with an example.
2. Write steps to display elements from the STACK.
64. Stack implementation using arrays
In the previous sections we have seen how the stacks can be implemented using arrays.
The complete program to perform operations such as push, pop and display is provided in
below example. Two semicolons (i.e.,;;} in the for loop indicates that for loop is an
infinite loop.
Example : C Program to implement the stack using arrays
#include <stdio.h>
#include <process.h>
#define ST ACK_SIZE 5
/* Include function push shown in example 3.2 Eg. -1 */
/*Include function pop shown in example 3.3 Eg. -1 */
/* Include function display shown in example 3.4 Eg. -1 */
void main( )
{
int top; /* Points to top of the stack */
int s[10]; /* Holds the stack items */
int item; /* Item to be inserted or deleted item */
int choice; /* user choice for push, pop and display */
top = -1; /* Stack is empty to start with */
for (;;)
{
printf(”1: Push 2: Popn”);
printf(”3: Display 4: Exitn”);
printf(”Enter the choicen”);
scanf(”%d”,& choice);
65. switch( choice )
{
case 1:
printf(”Enter the item to be insertedn”); scanf(”%d”,& item);
push(item, & top,s);
break;
case 2:
item = pop(&top,s);
if (item = = 0)
printf(”Stack is emptyn”);
else
printf(”Item deleted = %dn”, item);
break;
case 3:
display( top,s );
break;
default:
exit(0);
}
}
}
Output
1 ush 2: Pop
66. 3: Display 4: Exit
Enter the choice
1
Enter the item to be inserted
10
1: Push 2: Pop
3: Display 4: Exit
Enter the choice
1
Enter the item to be inserted
20
1: Push 2: Pop
3: Display 4: Exit
Enter the choice
3
Contents of the stack
10
20
1: Push 2: Pop
3: Display 4: Exit
Enter the choice
2
Item deleted = 20
67. 1: Push 2: Pop
3: Display 4: Exit
Enter the choice
2
Item deleted = 10
1: Push 2: Pop
3: Display 4: Exit
Enter the choice
2
Stack is empty
1: Push 2: Pop
3: Display 4: Exit
Enter the choice
4
Applications of stack
A stack is very useful in situations when data have to be stored and then retrieved in the
reverse order. Some applications of stack are listed below:
i. Function Calls:
We have already seen the role stacks plays in nested function calls. When the main
program calls a function named F, a stack frame for F gets pushed on top of the stack
frame for main. If F calls another function G, a new stack frame for G is pushed on top of
the frame for F. When G finishes its processing and returns, its frame gets popped off the
stack, restoring F to the top of the stack.
ii. Large number Arithmetic:
As another example, consider adding very large numbers. Suppose we wanted to add
353,120,457,764,910,452,008,700 and 234,765,000,129,654,080,277. First of all note
that it would be difficult to represent the numbers as integer variables, as they cannot
68. hold such large values. The problem can be solved by treating the numbers as strings of
numerals, store them on two stacks, and then perform addition by popping numbers from
the stacks.
iii. Evaluation of arithmetic expressions:
Stacks are useful in evaluation of arithmetic expressions. Consider the expression 5 * 3
+2 + 6 * 4
The expression can be evaluated by first multiplying 5 and 3, storing the result in A,
adding 2 and A, saving the result in A. We then multiply 6 and 4 and save the answer in
B. We finish off by adding A and B and leaving the final answer in A.
A = 15 2 +
= 17
B=64*
= 24
A = 17 24 +
= 41
We can write this sequence of operations as follows:
53*2+64*+
This notation is known as postfix notation and is evaluated as described above. We shall
shortly show how this form can be generated using a stack.
Basically there are 3 types of notations for expressions. The standard form is known as
the infix form. The other two are postfix and prefix forms.
Infix: operator is between operands A + B
Postfix : operator follows operands A B +
Prefix: operator precedes operands + A B
Note that all infix expressions can not be evaluated by using the left to right order of the
operators inside the expression. However, the operators in a postfix expression are
ALWAYS in the correct evaluation order. Thus evaluation of an infix expression is done
in two steps. The first step is to convert it into its equivalent postfix expression. The
second step involves evaluation of the postfix expression. We shall see in this section,
69. how stacks are useful in carrying out both the steps. Let us first examine the basic process
of infix to postfix conversion. Infix to postfix conversion:
Example 1:
a + b * c Infix form
(precedence of * is higher than of +)
a + (b * c) convert the multiplication
a + ( b c * ) convert the addition
a (b c * ) + Remove parentheses
a b c * + Postfix form
Note that there is no need of parentheses in postfix forms.
Example 2:
( A + B ) * C Infix form
( A B + ) * C Convert the addition
(A B + ) C * Convert multiplication
A B + C * Postfix form
No need of parenthesis anywhere
Example 3:
a + (( b * c ) / d )
a + ( ( b c * ) /d )
(precedence of * and / are same and they are left associative)
a+(bc*d/)
abc*d/+
• More examples
Infix Postfix
70. (a + b) * (c – d) a b + c d – *
a – b / (c + d * e) a b c d e * + / -
((a + b) * c – (d – e))/(f + g) a b + c * d e – - f g + /
Order of precedence for operators:
multiplication (*) and division (/)
addition (+) and subtraction (-)
The association is assumed to be left to right.
i.e. a + b + c = (a+b)+c = ab+c+
Evaluating a Postfix Expression
We can evaluate a postfix expression using a stack. Each operator in a postfix string
corresponds to the previous two operands. Each time we read an operand we push it onto
a stack. When we reach an operator its associated operands (the top two elements on the
stack) are popped out from the stack. We then perform the indicated operation on them
and push the result on top of the stack so that it will be available for use as one of the
operands for the next operator. The following example shows how a postfix expression
can be evaluated using a stack.
Example
6523+8*+3+*
The process stops when there are no more operator left in the string. The result of
evaluating the expression is obtained just by popping off the single element. More
examples will be done in the lecture and recitation labs.
71. Self Assessment Questions
1. Discuss the various STACK applications with suitable example of each.
2. Explain how Stacks are useful in evaluation of arithmetic expressions with example.
3. Write a suitable example of following arithmetic notations
infix , postfix and prefix forms.
Stacks using structures
So far we have seen how a stack is implemented using an array and various applications
of stacks. In this approach whenever a function push() is called, we have to pass three
parameters namely item, top and S, where item is the element to be pushed, top is an
integer value which is the index of the top most element in the array S. But, as the
number of parameters increases, the overhead of programming also increases and
efficiency decreases.
In such cases, we group all related items under a common name using a structure and
pass structures as parameters, which eventually reduces the burden and increases the
efficiency. In our stack implementation, instead of passing two parameters top and S, we
can pass only one parameter if we use a structure. So, a stack can be declared as a
structure containing two objects viz., an array to store the elements of the stack and an
integer indicating the position of the topmost element in the array. The declaration can
take the following form:
#define STACK_SIZE 5
struct stack
{
int items[STACK_SIZE];
int top;
};
typedef struct stack STACK;
Once this definition is done, we can use a variable s to access the contents of the stack
and to obtain the position of the top most element. The declaration for this can take the
form
STACK s;
72. The position of the top most element and the element itself can be accessed (using the ‘.’
operator) by specifying?
s. top
and
s. items[s. top];
If the declaration is of the form STACK *s, the position of the top most element and top
most element can be accessed by specifying
s->top
and
s->items[s->top];
Let us implement stacks using structures also. We know that the stack is empty, if the
position of the top most element is -1. The function is_empty( ) which returns true
whenever the stack is empty and returns false whenever the stack is not empty is shown
in below example 1.
Example 1. : Function to check whether the stack is empty or not
int is_empty(STACK *s)
{
if (s->top = = -1) return -1; /* Stack empty */
return 0; /* Stack is not empty */
}
The function is_full( ) returns true if stack is full and returns false if the stack is not full.
This function is shown in below example 2.
Example 2: Function to check whether the stack is full or not
int is_full(STACK *s)
{
if ( s->top == STACK _ SlZE -1) return 1; /* Stack is full */
73. return 0; /* Stack is not full */
}
The function to insert an integer item into the stack is shown in below example 3.
Example 3: Function to insert an integer item into the stack
void push(int item, STACK *s)
{
if ( is_full(s) )
{
printf(”Stack Overflown”);
return;
}
s->top+ +; /* Update top to point to next item */
s->items[s->top] = item; /* Insert the item into the stack*/
}
The function to insert a character item into the stack is shown in below example 4.
Example 4 : Function to insert a character item into the stack
void push(char item. ST ACK *s)
{
if ( is_full(s) )
{
printf(”Stack Overflown”);
return;
}
74. s->top++; /* Update top to point to next item */
s->items[s->top] = item; /* Insert the item into the stack*/
}
The function to delete an integer item from the stack is shown in below example 5
Example 5 : Function to delete an integer item from the stack
int pop(ST ACK *s)
{
int item;
if ( is_ empty(s) )
{
printf(”Stack Underflown”);
return 0;
}
item = s->items[s->top]; /* Access the top element */
s->top–; /* Update the pointer to point to previous item
return item; /* Return the top item to the calling function */
}
The function to delete a character item from the stack is shown in below example 6
Example 6 : Function to delete a character item from the stack.
char pop(STACK *s)
{
char item;
if ( is_empty(s) )