Data structures


Published on

KSOU books

Published in: Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Data structures

  1. 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.inEducate Anytime...Anywhere..."Greetings For The Day"About EduprozWe, at EduProz, started our voyage with a dream of making higher education available for everyone. Sinceits inception, EduProz has been working as a stepping-stone for the students coming from variedbackgrounds. The best part is – the classroom for distance learning or correspondence courses for bothmanagement (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 variousindustrial domains, lead students not only to secure good marks in examination, but also to get an edge overothers in their professional lives. Our study materials are sufficient to keep students abreast of the presentnuances of the industry. In addition, we give importance to regular tests and sessions to evaluate ourstudents’ progress. Students can attend regular classes of distance learning MBA, BBA, MCA and BCA courses at EduProzwithout 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 careercounselling and career development cell where we help student to prepare for interviews. Our dedicatedplacement 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 MetroStation and 4-minutes drive from the national highway); students can easily come to our centre fromanywhere Delhi and neighbouring Gurgaon, Haryana and avail of a quality-oriented education facility atapparently 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. 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. 3. Karnataka State Open University(KSOU) was established on 1st June 1996 with the assent of H.E. Governor ofKarnatakaas a full fledged University in the academic year 1996 vide GovernmentnotificationNo/EDI/UOV/dated 12th February 1996 (Karnataka State Open UniversityAct – 1992).The act was promulgated with the object to incorporate an Open University at theState level for the introduction and promotion of Open University and DistanceEducation systems in theeducation pattern of the State and the country for the Co-ordination anddetermination of standard of such systems. Keeping in view the educationalneeds of our country, in general, and state in particular the policies andprogrammes have been geared to cater to the needy.Karnataka State Open University is a UGC recognised University of DistanceEducation Council (DEC), New Delhi, regular member of the Association ofIndian Universities (AIU), Delhi, permanent member of Association ofCommonwealth Universities (ACU), London, UK, Asian Association of OpenUniversities (AAOU), Beijing, China, and also has association withCommonwealth of Learning (COL).Karnataka State Open University is situated at the North–Western end of theManasagangotri campus, Mysore. The campus, which is about 5 kms, from thecity centre, has a serene atmosphere ideally suited for academic pursuits. TheUniversity houses at present the Administrative Office, Academic Block, LectureHalls, a well-equipped Library, Guest HouseCottages, a Moderate Canteen, Girls Hostel and a few cottages providing limitedaccommodation to students coming to Mysore for attending the ContactProgrammes or Term-end examinations. Unit1 Arrays, Pointers and Structures This unit covers the Definitions and concept of an array, single and double dimensionarray, Definition of pointers, Declaring pointer variable, pointer operators, Pointers andArrays, Pointers and Functions, structures, Declaring initializing of structure, processing of structure, Structure with array.IntroductionThe need of the arrays can be best understood by thinking, what if its not there. If we hadto write a program to add three integers, we may declare a,b,c as three integers and
  4. 4. display the result as “a+b+c”. What if we had to add 100 numbers only after accepting allof them? We would need 100 different variables with 100 different identifiers with no ofvariable locations as relative of each other. This approach is very cumbersome andlengthy. Hence is the need of arrays. Arrays are the basis for creating any new datastructures; 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 oneidentifier. 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 specialvariables as they simply point to other variable. The pointers are the powerful tool forinstance, are used to setup complicated data structure and are used to link variablestogether.In general variables are defined with its type and these can be find with the addresses forordinary variables by using the address operator ‘&’. Thus in C and C++ variables thattell a computer where data is placed that are know as pointer variable.ObjectivesAt 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 ArrayArray is a list or collection of data items which are stored in the form of a table, referredto 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 name2) Arrays saves the memory space3) Arrays helps to arrange the data (Sorting) items in particular order [Ascending /descending]4) Data items searching is faster.
  5. 5. Concept of an Array:Concept of an array can be classified into two types that are :1. Single dimensional array2. Double/ multi dimensional array or matrix.· Concept of Single dimensional Array.Data items are stored in one single column with specified subscript variable name byusing subscript range.Example:Note : In the single dimensional Array , values are stored or arranged Column wise withits respective range and each element range addressed by common single variable name.Concept of two Dimensional or Double Dimension ArrayIn double dimensional array, values are referred with respect to specified Row & columnusing subscript variable name. double dimensional array also called as matrix.Example : Holding the Different Brands Electrical Bulbs quantity with respect to itswattages(W).Above Example indicates the 4 / 5 Matrix, all cell values are referred by the subscriptname (Array name) QTY with respect to row and column.Array Used in ‘C’ Language
  6. 6. In any High Level Language[HLL] when we used an array the following 3 sequencesteps 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)Exampleint 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 totalsale & best sales amount./* Storing 10 sales amount in an array andfind out total sale & Best sale amount */#include <iostream.h>
  7. 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] > bestbest = sale_amt [i];}/* Printing Total and Best Sale amount */printf( “ n Total sale amount = %d ”, tot_amt);
  8. 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 ascendingorder./* 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. 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 displayappropriate 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. 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).;elseprintf( “ n given value is not found” ) ;}
  11. 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 arraywith help of two for() statements forming nested loop, we can store the element values inan arrayReading values from an array for process just printing array values.
  12. 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. 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 Questions1. Define an array? Write its advantages using in Program.2. Write the syntax of declaration of an array with example of each.
  14. 14. 3. What are the points should remember using array in program.1.2 PointersDefinition of pointer“A pointer is a variable that can hold the address of the variables, structures and functionsthat are used in the program. It contains only the memory location of the variable ratherthan 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 structures2. Manipulation of data at different memory locations is easier.3. To achieve a clarity and simplicity4. More compact and efficient coding.5. To return multiple value via functions.6. Dynamic memory allocations.Declaring a pointer variablePointers are declared similar to normal variables, but we must specify when we declarethem what they are going to point to it. We declare a pointer to point to an integer, then itcant be used to point a floating-point value etc.Pointer Operators:To declare and refer a pointer variable, provides two special operators & and *.
  15. 15. Types of pointer variable declaration:Example :char *cptr; pointer to character type variablesint *iptr; *num pointer to integer type variablesfloat *fptr; pointer to float type variableschar *name[15] pointer to character arrayNote: * 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. 16. printf (“value of y = %d n” , y);printf( “Address of y = %d n ” , x);printf( “value of y = %d n ”, *x);}outputAddress of y = 65555Value of y = 10Address of y = 65555Value of y = 10Note: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. 17. printf( “The value of y is = %d n “, y);}outputThe value of y is = 8Note: Variable y is assigned to value at the address stored in ipt. since ipt containsaddress 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. 18. There is a close association between pointers and arrays, array elements can be accessedusing pointers.Example: Program to reads 10 array elements & prints the elements using pointertechnique.#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 startfrom 0 element i.e. *(arpt = 0).
  19. 19. In the second technique (*arpt = 0) in first cycle then increment operation i is used withthe pointer instead of adding loop index to the pointer.Example: Program to read n number of element and find the biggest elements amongthem.#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. 20. }Pointers used in functionIt is mechanism by which pointers can be passed as arguments to the function. Thus, thedata items of the calling program can be accessed by the called program. No values iscopied when pointers are passed as arguments, as in the called by value method. Anotherimportant point is that, if the values are changed in the function this will modify theoriginal 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 ofactual 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 bepreceeded 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 of2 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. 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 ArrayPointers 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 designatessome memory location & this location in memory is the address of the very first elementof an array, the address of the first element of array & num[0], where num is an arrayname.Example :Write a program to use an array of 5 elements & illustrate the relationship betweenelements of an array & their address.main(){int arrlist[5];int *ptr,index,value=3;
  22. 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)= 7Example :Write a program to find the sum of 5 elements static in nature using pointers withfunction.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. 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.StructuresDefinitions : Structure is a meaningful organized Collection of data items of differenttype 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 reserveword ‘struct’ .1.7.1 Declaration of structureEach and every structure must be defined or declared before it appears or using inprogram.Syntax: struct <structurer name>{<type1> <field/data1><type2> <field/data2><type3> <field/data3>………………………
  24. 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 structureInitializing a structure description of structure member is similar to initializing static typedeclaration.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 otherstructure declared next.Example:
  25. 25. 1.7.3 Processing of StructureThe process of structure is mainly concerned with the accessing structure member. Eachmember of a structure is accessed with .(dot) operator to access a particular member ofthe structure, the dot operator must be placed between the name of the structure & thename 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 usingstructure 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. 26. printf(“enter the Name n”);scanf(“%s “,;printf(“enter the city n”);scanf(“%d “,;printf(“enter the Marks n”);scanf(“%d “, &st.marks);/* printing details */printf(“Roll Number : %d”,st.rollno);printf(“Name : %s”,;printf(“City : %s”,;printf(“Marks : %d”,st.marks)}1.7.4 Structure used with an ArrayHowever we know that different type of data sets cannot be stored an array, So, toovercome this disadvantage structure can be stored along with its members in arraystructure.Example: Storing 10 students details structure in an array.Self Assessment Questions1. Define Structure ?
  27. 27. 2. Write a advantages of Structure over Arrays using in programs.3. Give one suitable example of Structure using an array.SummaryArrays are the basis for creating any new data structures; understanding of arrays isessential and becomes vital role of programmer while implementing the codes. Usingarrays one can declare and define multiple variables of same type with one identifier. Fore.g. int a[10] is a declaration of 10 variables which are clustered together. It also saves amemory 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 specialvariables as they simply point to other variable. The pointers are the powerful tool forinstance, are used to setup complicated data structure and are used to link variablestogether.1.9 Terminal Questions1. Define Array ? Write the Syntax with example of declaring a single and doubledimension array in ‘C’.2. Write a ‘C’ program to read N number of observations and print them in ascendingorder.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 theuser.5. Numbers in array are stored in linear fashion, find the biggest and the smallest of 10numbers in the given array.6. Array elements are stored from 0th location, relocate the elements to start from 4thlocation7. 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 thearray.11. Without using string functions find the length of the string.
  28. 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 pointerswith functions.15. Define Structure ? Write Syntax with appropriate example for declaration ofStructure. 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.IntroductionData structures represent places to store data for use by a computer program. As youwould imagine, this describes a spectrum of data storage techniques, from the verysimple to the very complex. We can look at this progression, from the simple to thecomplex, in the following way.At the lowest level, there are data structures supplied and supported by the CPU (orcomputer chip), itself. These vary from chip to chip, but are almost always of the veryprimitive 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 achip 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 theALU support floating point operations?At the second level of the data structures spectrum are the data structures supported byparticular programming languages. These vary a lot from language to language. Mostlanguages offer arrays, and many offer arrays of arrays (matrices). Most of the popularlanguages provide support for some sort of record structure. In C these are structs and inPascal 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 theprogrammer, using a particular programming language. In this regard, it is important to
  29. 29. note what tools are provided by a language to facilitate the implementation of complexdata 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, aprogrammer 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 usuallycreated 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 .ObjectivesAt 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 exampledata structures are: array, stack, queue, linked list, binary tree, hash table, heap, andgraph. Data structures are often used to build databases. Typically, data structures aremanipulated using various algorithms.Based on the concept of Abstract Data Types (ADT), we define a data structure by thefollowing three components.1) Operations: Specifications of external appearance of a data structure2) Storage Structures: Organizations of data implemented in lower-level data structures3) Algorithms: Description on how to manipulate information in the storage structures toobtain the results defined for the operationsWorking with and collecting information on any subject, it doesn’t take very long beforeyou have more data than you know how to handle. Enter the data structure. In his book
  30. 30. Algorithms, Data Structures and Problem Solving with C, Mark Allen Weiss writes “Adata 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 relatedpieces of information.”Definition of data structure“a specification, an application and an implementation view of a collection of one ormore items of data, and the operations necessary and sufficient to interact with thecollection. 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 applicationlevel is a way of modeling real-life data in a specific context. The implementation is aconcrete data type expressed in a programming language. There may be intermediatelevels of implementation, but ultimately the data structure implementation must beexpressed in terms of the source language primitive data types”.The Abstract LevelThe abstract (or logical) level is the specification of the data structure -the “what” but notthe “how.” At this level. the user or data structure designer is free to think outside thebounds of anyone programming language. For instance. a linear list type would consist ofa collection of list nodes such that they formed a sequence. The operations defined forthis list might be insert. delete, sort and retrieve.The Application LevelAt the application or user level, the user is modeling real-life data in a specific context. Inour list example. we might specify what kind of items were stored in the list and howlong 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 thanif we were talking about a grocery list.Implementation LevelThe implementation level is where the model becomes compilable, executable code. Weneed to determine where the data will reside and allocate space in that storage area. Wealso need to create the sequence of instructions that will cause the operations to performas specified.Self Assessment Questions1. Define data Structure? Explain its three components.2. Discuss the data structure implementation in terms of the source language primitivedata type.
  31. 31. Data Types and Structured Data TypeThe 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 DefinitionsThere are two different approaches to specifying a domain : we can give a structuraldefinition 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 anyinternal structure on them. Instead it Just adds an operation that creates fractions out ofother 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 functionfor any valid combination of inputs. The parameter names were chosen to suggest itsintended behavior: CREATE_FRACTION(N.D) should return a value representing thefraction N/D (N for numerator. D for denominator).You are probably thinking. this is crazy. CREATE_FRACTION could be any old randomfunction. how do we guarantee that CREATE_FRACTION(N,D) actually returns thefraction N/D? The answer is that we have to constrain the behavior of this function. byrelating it to the other operations on fractions. For example, One of the key properties ofmultiplication is that: NORMALIZE ((N/D) .(DIN)) = 1/1This 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 highlyconstrained, because we can write down lots and lots of constraints like this. And that’sthe reason we call this sort of definition behavioral, because the definition is strictly in
  32. 32. terms of a set of operations and constraints or axioms relating the behavior of theoperations to one another.In this style of definition, the domain of a data type -the set of permissible values -playsan almost negligible role. Any set of values will do, as long as we have an appropriate setof operations to go along with it.Common StructuresLet us stick with structural definitions for the moment. and briefly survey the mainkinds 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. 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<-B2.2.2 Abstract Data TypesHandling ProblemsThis implies that the model focuses only on problem related stuff and that you try todefine properties of the problem. These properties include: • the data which are -affected and • the operations which are identified by the problemIt is said that “computer science is the science of abstraction.” But what exactly isabstraction? Abstraction is “the idea of a quality thought of apart from any particularobject or real thing having that quality. For example. we can think about the size of anobject without knowing what that object is. Similarly, we can think about the way a car isdriven without knowing Its model or make.As an example consider the administration of employees in an institution. The head of theadministration comes to you and ask you to create a program which allows to administerthe employees. Well. this is not very specific. For example, what employee information isneeded by the administration? What tasks should be allowed? Employees are real personswho 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. 34. employee for the problem. This model only implies properties which are needed to fulfillthe requirements of the administration. for instance name, date of birth and socialnumber. These properties are called the data of the (employee) model. Now you havedescribed real persons with help of an abstract employee.Of course, the pure description is not enough. There must be some operations definedwith which the administration is able to handle the abstract employees. For example theremust be an operation which allows you to create a new employee once a new personenters the institution. Consequently, you have to identify the operations which should beable to be performed on an abstract employee. You also decide to allow access to theemployees’ data only with associated operations. This allows you to ensure that dataelements are always in a proper state. For example you are able to check if a provideddate is valid.Abstraction is used to suppress irrelevant details while at the same time emphasizingrelevant ones. The benefit of abstraction is that it makes it easier for the programmer tothink about the problem to be solved.To sum up. abstraction is the structuring of a nebulous problem into well-defined entitiesby defining their data and operations. Consequently, these entities combine data andoperations. 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. 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 thatx can represent an integer in the range (-231, 231-1) and that we can performoperations 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 thequalities of an int apart from any real thing having that quality. In other words, wedon’t need to know how ints are represented nor how the. operations areimplemented to be able to be. able to use them or reason about them.In designing object-oriented programs, one of the primary concerns of theprogrammer is to develop an appropriate collection of abstractions for theapplication at hand, and then to define suitable abstract data types to representthose abstractions. In so doing, the programmer must be conscious of the fact thatdefining an abstract data type requires the specification of both a set of values anda set of operations on those values.Indeed, it has been only since the advent of the so-called object-orientedprogramming languages that the we see programming languages which providethe necessary constructs to properly declare abstract data types. For example, inJava, the class construct is the means by which both a set of values and anassociated set of operations is declared. Compare this with the struct construct ofC or Pascal’s record, which only allow the specification of a set of values!Properties of Abstract Data TypesThe example of the quoted before shows, that with abstraction you create a well-defined entity which can be properly handled. These entities define the datastructure 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 withdefined operations. This set of operations is called interface and abstract data typeis exported by the entity. An entity with the properties just described is called anabstract data type (ADT).
  36. 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 ADTas exemplified with the employee example.Example of the fraction data type, how might we actually implement this datatype in C?Implementation 1:typedef struct { int numerator, denominator; } fraction;main(){fraction f;f.numerator = 1;f.denominator = 2;……………
  37. 37. }Implementation 2 :#define numerator 0#define denominator 1typedef int fraction[2];main(){fraction f;f[numerator] = 1;f[denominator] = 2;……………}These are just 2 of many different possibilities. Obviously these differences are insome sense extremely trivial -they do not affect the domain of values or meaningof the operations of fractions.Generic Abstract Data TypesADTs are used to define a new type from which instances can be created. Forinstance, one of lists of apples, cars or even lists. The semantically the definitionof a list is always the same. Only the type of the data elements change accordingto what type the list should operate on.This additional information could be specified by a generic parameter which isspecified at instance creation time. Thus an instance of a generic ADT is actuallyan instance of a particular variant the ADT. A list of apples can therefore bedeclared as follows:List<Apple> listOfApples;The angle brackets now enclose the data type for which a variant of the genericADT List should be created. ListOf Apples offers the same interface as any otherlist, but operates on of type Apple.
  38. 38. Notation :As ADTs provide an abstract view to describe properties of sets of entities, theiruse is independent from a particular programming language. We thereforeintroduce 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 aninteger expression: o ADT integer isDataA sequence of digits optionally prefixed by a plus or minus sign. We refer to thissigned whole number as N.OperationsConstructorCreates 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 confusethis with assign statements as used in programming languages, It is rather amathematical equation which yields “true” for each value sum, N and k after addhas been performed.sub(k)similar to add. this operation creates a new integer of the difference of bothinteger values. Therefore the postcondition for this operation is sum = N-k.
  39. 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. 40. The implementation: The part that implements the abstract data type.These two pieces are completely independent. It should be possible to take theimplementation developed for one application and use it for a completely differentapplication with no changes.If programming in teams, implementers and application-writers can work completelyindependently once the specification is set.SpecificationLet us now look in detail at how we specify an abstract datatype. We will use ’stack’ asan example. The data structure stack is based on the everyday notion of a stack, such as astack of books, or a stack of plates. The defining property of a stack is that you can onlyaccess the top element of the stack, all the other elements are underneath the top one andcan’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 providehardware implementations of the basic stack operations.First, let us see how we can define, or specify, the abstract concept of a stack. The mainthing to notice here is how we specify everything needed in order to use stacks withoutany 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 ConditionsPreconditionsThese are properties about the inputs that are assumed by an operation. If they aresatisfied by the inputs, the operation is guaranteed to work properly. If the preconditionsare not satisfied, the operation’s behavior is unspecified: it might work properly (bychance), it might return an incorrect answer, it might crash.PostconditionsSpecify the effects of an operation. These are the only things you may assume have beendone by the operation. They are only guaranteed to hold if the preconditions are satisfied.
  41. 41. Note: The definition of the values of type ’stack’ make no mention of an upper bound onthe size of a stack. Therefore, the implementation must support stacks of any size. Inpractice, there is always an upper bound -the amount of computer storage available. Thislimit is not explicitly mentioned, but is understood -it is an implicit precondition on alloperations that there is storage available, as needed. Sometimes this is made explicit, inwhich case it is advisable to add an operation that tests if there is sufficient storageavailable for a given operation.OperationsThe operations specified before are core operations -any other operation on stacks can bedefined in terms of these ones. These are the operations that we must implement in orderto implement ’stacks’, everything else in our program can be independent of theimplementation 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_STACK2. Those that ‘destroy’ stacks (opposite of create) e.g. DESTROY_STACK3. Those that ‘inspect’ or ‘observe’ a stack, e.g. TOP, IS_EMPTY, WRITE_STACK4. Those that takes stacks (and possibly other things) as input and produce other stacks asoutput, e.g. PUSH, POPA specification must say what an operation’s input and outputs are, and definitely mustmention when an input is changed. This falls short of completely committing theimplementation to procedures or functions (or whatever other means of creating ‘blocks’of code might be available in the programming language). Of course, these detailseventually need to be decided in order for code to actually be written. But these details donot need to be decided until code-generation time; throughout the earlier stages ofprogram design, the exact interface (at code level) can be left unspecified.Checking Pre ConditionsIt is very important to state in the specification whether each precondition will bechecked by the user or by the implementer. For example, the precondition for POP maybe checked either by the procedure(s) that call POP or within the procedure thatimplements POP? Either way is possible. Here are the pros and cons of the 2possibilities:User Guarantees Preconditions
  42. 42. The main advantage, if the user checks preconditions -and therefore guarantees that theywill 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 isguaranteed to be satisfied because it is a postcondition of PUSH.Implementation Checks PreconditionsThere 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. implementationdetails about space requirements), although this is often a sign of a poorly constructedspecification.2. Programs won’t bomb mysteriously -errors will be detected (and reported?) at theearliest possible moment. This is not true when the user checks preconditions, becausethe user is human and occasionally might forget to check, or might think that checkingwas unnecessary when in fact it was needed.3. Most important of all, if we ever change the specification, and wish to add, delete, ormodify preconditions, we can do this easily, because the precondition occurs in exactlyone place in our program.There are arguments on both sides. The literatures specifies that procedures should signalan error if their preconditions are not satisfied. This means that these procedures mustcheck their own preconditions. That’s what our model solutions will do too. We willthereby sacrifice some efficiency for a high degree of maintainability and robustness.An additional possibility is to selectively include or exclude the implementation’scondition checking code, e.g. using #ifdef:#ifdef SAFEif (! condition) error(”condition not satisfied”);#endifThis code will get included only if we supply the DSAFE argument to the compiler (orotherwise define SAFE). Thus, in an application where the user checks carefully for allpreconditions, we have the option of omitting all checks by the implementation.
  43. 43. Self Assessment Questions1. Explain the pre and Post conditions with an suitable example.2. Discuss the advantages of implementation checks preconditions.Linear Data StructureThe Array Data StructureAs an example, most programming languages have an array type as one of the built-intypes. We will define an array as a homogeneous, ordered, finite, fixed-length list ofelements. To further define these terms in the context of an array:a) homogeneous -every element is the sameb) ordered -there is a next and previous in the natural order of the structure c) finite -thereis a first and last elementd) fixed-length -the list size is constantMapping 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 operator2. 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 StructureAn array can be used to implement containers.
  44. 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 indicatethe end of the array); however, inserting a value into an array can be time consumingbecause existing elements must be rotated.Since array elements are typically stored in contiguous memory locations, loopingthrough an array can be done easily and efficiently.When elements of an array are sorted, then binary searching can be used to find particularvalues 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 SlowThe 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 arrayis 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 datastructure: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 abstractoperations explains why we can use them without thinking, while the fact that we havedifferent implementation of the same abstract operations enables us to optimizeperformance.
  45. 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 aboutevery case, we have the option of implementing the ADT using an array or using somekind of linked data structure.Because they are the base upon which almost all of the ADTs are built, we call the arrayand the linked list the foundational data structures. It is important to understand that wedo not view the array or the linked list as ADTs, but rather as alternatives for theimplementation of ADTs.ArraysProbably the most common way to aggregate data is to use an array. In C an array is avariable 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 elementof 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 ofarray elements.How are C arrays represented in the memory of the computer? The specification of the Clanguage leaves this up to the system implementers. However, Figure illustrates a typicalimplementation scenario.The elements of an array typically occupy consecutive memory locations. That way giveni, it is possible to find the position of a[I] in constant time. On the basis of Figure. we cannow 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 byS(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 representationof 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. 46. In practice. an array object may contain additional fields. For example. it is reasonable toexpect that there is a field which records the position in memory of the first arrayelement. In any event the overhead associated with a fixed number of fields is 0(1).Therefore, S(n)=O(n).Multi-Dimensional ArraysA multi-dimensional array of dimension n (i.e. an n-dimensional array or simply n-Darray) is a collection of items which is accessed via n subscript expressions. For a language that supports it. (i, j)th the element of the two-dimensional array x isaccessed by writing x[i,j].The C programming language does not really support multi-dimensional arrays. It doeshowever support arrays of arrays. In C a two-dimensional array x is really an array ofone- dimensional arrays:int x[3][5];The expression x[i] selects the ith one-dimensional array; the expression x[i][j]selects thej 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 assignmentoperator. The number of dimensions and the size of each dimension is fixed once thearray has been allocated.Self Assessment Questions1. 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 onlycares how it behaves and not how it is implemented.ListGeneric term for a collection of objects. May or may not contain duplicates. Applicationmay or may not require that it be kept in a specified order.Ordered list
  47. 47. A list in which the order matters to the application. Therefore for example. theimplementer cannot scramble the order to improve efficiency.SetList where the order does not matter to the application (implementer can pick order so asto optimize performance) and in which there are no duplicates.Multi-setLike 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. Thatis elements cannot be inserted into the middle of the list or deleted from the middle of thelist.StackAn ordered list in which insertion and deletion both occur only at one end (e.g. at thestart).QueueAn ordered list in which insertion always occurs at one end and deletion always occurs atthe other end.Ordered Lists and Sorted ListsThe most simple yet one of the most versatile containers is the list. In this section weconsider lists as abstract data types. A list is a series of items. In general, we can insertand remove items from a list and we can visit all the items in a list in the order in whichthey appear.In this section we consider two kinds of lists-ordered lists and sorted lists. In an orderedlist the order of the items is significant. The order of the items in the list corresponds tothe order in which they appear in the book. However, since the chapter titles are notsorted alphabetically, we cannot consider the list to be sorted. Since it is possible tochange the order of the chapters in book, we must be able to do the same with the itemsof 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 somecollating sequence. For example, the index of this book is a sorted list. The items in theindex are sorted alphabetically. When an item is inserted into a sorted list, it must beinserted at the correct position.
  48. 48. Ordered ListsAn ordered list is a list in which the order of the items is significant. However, the itemsin an ordered lists are not necessarily sorted. Consequently, it is possible to change theorder 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 ListsThe next type of searchable container that we consider is a sorted list. A sorted list is likean ordered list: It is a searchable container that holds a sequence of objects. However, theposition of an item in a sorted list is not arbitrary .The items in the sequence appear inorder, say, from the smallest to the largest. Of course, for such an ordering to exist, therelation 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 ListsMakes 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 inthe opposite way).
  49. 49. Comparison of List ImplementationsArray-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 fullLinked 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 ListsElements 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. 50. · Dynamic (run time) allocationImplementation methodsThere are a variety of options for the person implementing a list (or set or stack orwhatever).a) arrayWe all know what arrays are. Arrays are included here because a list can be implementedusing a I D array. If the maximum length of the list is not known in advance. code mustbe provided to detect array overflow and expand the array. Expanding requires allocatinganew, 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 thelist 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 themiddle of an array-based list is slow.)b) linked listA 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 offields of data. Optionally, there may be a second “list” class (or struct) used as a headerfor the list. One field of the list class is a pointer to the first node in the list. Other fieldsmay also be included in the “list” object, such as a pointer to the last node in the list, thelength of the list, etc.Linked lists are commonly used when the length of the list is not known in advanceand/or when it is frequently necessary to insert and/or delete in the middle of the list.c) doubly-linked vs. singly-linked listsIn 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 previousnode.d) circular listA linked list in which the last node points to the first node. If the list is doubly-linked, thefirst node must also point back to the last node.Non Linear Data StructuresTrees
  51. 51. we consider one of the most Important non-linear Information structures- trees. A tree Isoften used to represent a hierarchy. This is because the relationships between the Items Inthe hierarchy suggest the branches of a botanical tree.For example, a tree-like organization charts often used to represent the lines ofresponsibility in a business as shown in Figure. The president of the company is shown atthe 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 clerkreports to a manager. Each manager reports to a vice-president, and each vice-presidentreports 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 calledthe root of the tree and the clerks are the leaves.A tree is extremely useful for certain kinds of computations. For example. Suppose wewish to determine the total salaries paid to employees by division or by department. Thetotal of the salaries in division A can be found by computing the sum of the salaries paidin 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 ofdepartment Al and of the two clerks below her.Clearly, in order to compute all the totals. It is necessary to consider the salary of everyemployee. Therefore, an implementation of this computation must visit all the employeesin the tree. An algorithm that systematically visits all the items in a tree is called a treetraversal.In the same chapter we consider several different kinds of trees as well as severaldifferent tree traversal algorithms. In addition. We show how trees can be used torepresent arithmetic expressions and how we can evaluate an arithmetic expression bydoing 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. 52. 2. The remaining nodes are partitioned into n≥ O subsets T, T. …Tn each of which is atree 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 donot have a problem with infinite recursion because every tree has a finite number ofnodes 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 treeTa = {D, {E. {F}}, {G.{H,II}}, {J, {K}. {L}}, {M}}}How do Ta Tb. & Tc resemble their arboreal namesake? The similarity becomes apparentwhen we consider the graphical representation of these trees shown in Figure. To drawsuch a pictorial representation of a tree, T = {r. T1 ,T2, …Tn, beside each other below theroot. Finally, lines are drawn from rto the roots of each of the subtrees. T1T2…….TnFigure : Examples of trees.Of course, trees drawn in this fashion are upside down. Nevertheless, this is theconventional way in which tree data structures are drawn. In fact, it is understood thatwhen we speak of “up” and “down,” we do so with respect to this pictorialrepresentation. For example, when we move from a root to a subtree, we will say that weare moving down the tree.The inverted pictorial representation of trees is probably due to the way that genealogicallineal charts are drawn. A lineal chart is a family tree that shows the descendants of someperson. And it is from genealogy that much of the terminology associated with tree datastructures is taken.Figure shows one representation of the tree Tc defined in Equation. In this case, the treeis represented as a set of nested regions in the plane. In fact, what we have is a Venndiagram which corresponds to the view that a tree is a set of sets.
  53. 53. Figure: An alternate graphical representation for trees.Binary TreeUsed 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 isirrelevant 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 containspointers to a left child node and a right child node. In some implementations, it may alsocontain 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 theroot of the tree (the node with no parent) and whatever other information the programmerwants 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 Nis 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 tocontain equal values.Hash TablesA very common paradigm in data processing involves storing information in a table andthen later retrieving the information stored there. For example, consider a database ofdriver’s license records. The database contains one record for each driver’s licenseissued. Given a driver’s license number. we can look up the information associated withthat number. Similar operations are done by the C compiler. The compiler uses a symboltable to keep track of the user-defined symbols in a Java program. As it compiles aprogram, the compiler inserts an entry in the symbol table every time a new symbol isdeclared. In addition, every time a symbol is used, the compiler looks up the attributesassociated with that symbol to see that it is being used correctly.Typically the database comprises a collection of key-and-value pairs. Information isretrieved 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 symboltable, the key is the name of the symbol.
  54. 54. In general, an application may perform a large number of insertion and/ or look-upoperations. Occasionally it is also necessary to remove items from the database. Becausea 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, itassumes 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 tointegers.In bucket sort, our hash function mapped the key to a bucket based on the first letters ofthe key. “Collisions” were the set of keys mapped to the same bucket. If the keys wereuniformly distributed. then each bucket contains very few keys!The resulting short lists were easily sorted, and could just as easily be searchedWe examine data structures which are designed specifically with the objective ofproviding efficient insertion and find operations. In order to meet the design objectivecertain concessions are made. Specifically, we do not require that there be any specificordering of the items in the container. In addition, while we still require the ability toremove items from the container, it is not our primary objective to make removal asefficient as the insertion and find operations.Ideally we would’ build a data structure for which both the insertion and find operationsare 0(1) in the worst case. However, this kind of performance can only be achieved withcomplete a priori knowledge. We need to know beforehand specifically which items areto be inserted into the container. Unfortunately, we do not have this information in thegeneral case. So, if we cannot guarantee 0(1) performance in the worst case, then wemake it our design objective to achieve 0(1) performance in the average case.The constant time performance objective immediately leads us to the followingconclusion: Our implementation must be based in some way Kh element of an array inconstant 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 thesorted list. In the case of an ordered list, the cost of an insertion is 0(1) and the cost of thefind operation is O(n). For a sorted list the cost of insertion is O(n) and the cost of thefind operation is O(log n) for the array implementation.Clearly, neither the ordered list nor the sorted list meets our performance objectives. Theessential problem is that a search, either linear or binary, is always necessary. In theordered list, the find operation uses a linear search to locate the item. In the sorted list, a
  55. 55. binary search can be used to locate the item because the data is sorted. However, in orderto keep the data sorted, insertion becomes O(n).In order to meet the performance objective of constant time insert and find operations. weneed a way to do them without performing a search. That is, given an item x, we need tobe able to determine directly from x the array position where it is to be stored.Hash FunctionsIt is the job of the hash function to map keys to integers. A good hash function:1. Is cheap to evaluate2. 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 examplek=wthh = S 1284 x char (key[I])1=0This last number must be reduced to an integer whose size is between 1 and the size ofour hash table. One way is by h(k) = k mod M where M is best a large prime not tooclose to 2i -1, which would just mask off the high bits. This works on the same principleas a roulette wheel!Self Assessment Questions1. Define Trees. Discuss its usage in different applications.2. Write note on:a) Binary Tree b) Hash TablesSummaryThis unit covers all overview and concepts of data structure with its applications. Datastructures represent places to store data for use by a computer program. As you wouldimagine, this describes a spectrum of data storage techniques, from the very simple to thevery complex. We can look at this progression, from the simple to the complex, At thelowest level, there are data structures supplied and supported by the CPU (or computerchip), itself. These vary from chip to chip, but are almost always of the very primitive
  56. 56. sort. They typically include the simple data types, such as integers, characters, floatingpoint numbers, and bit strings. On these contacts discussed the various structured datatypes, Abstract data types, Linear and non linear data structure.Terminal Questions1. 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 Structuresb) Ordered listc) Linked listd) Queuee) Slackf) Binary treeg) 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’,IntroductionDefinitions and operations:We know that in a cafeteria the plates are placed one above the other and every new plateis added at the top. When a plate is required, it is taken off from the top and it is used. We
  57. 57. call this process as stacking of plates. Thus, the operations that can be performed if platesare stacked are:· Addition/insertion of plate at one end· Deletion of plate at the same endUsing this analogy a stack is defined as a special type of data structure where items areinserted 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 alsocalled Last In First Out (LIFO) data structure.ObjectivesAt 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 StructureOperations of StackThe 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 stackFrom the definition of stack it is clear that it is a collection of similar type of items andnaturally we can use an array (An array is a collection of similar data types) to hold theitems 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 asshown in following figure.It is clear from this figure that initially stack is empty and top points tobottom of stack. As the items are inserted top pointer is incremented and it points to thetopmost item. Here, the items 30, 20, 25, 10 and 40 are inserted one after the other. After
  58. 58. inserting 40 the stack is full. In this situation it is not possible to insert any new item. Thissituation is calledstack overflow. When an item is to be deleted, it should be deleted from the top as shownin following figure.Since items are inserted from one end, in stack deletions should be done from the sameend. So, as the items are deleted, the item below the top item becomes the new top itemand so the position of the top most item is decremented as shown in above figure Theitems deleted in order are 40, 10, 25, 20 and 30. Finally, when all items are deleted, toppoints to bottom of stack. When the stack is empty, it is not possible to delete any itemand this situation is called under flow of stack.So, the main operations to be performed on stacks are insertion and deletion. Inserting anitem into the stack when stack is not full is called push operation and deleting an itemfrom the stack when stack is not empty is called pop operation. Other operations that canbe performed are display the contents of the stack, check whether the stack is empty ornot, etc., Let us see how push and pop operations are implemented.Self Assessment Questions1. Define stack with its different operations.2. Discuss the stack Insertion and deletion of element from/to stack with suitableexample.Insert/Push operationTo design a C function, to start with let us assume that three items are already added tothe stack and stack is identified by s as shown in figure a.
  59. 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. Thecorresponding C statements are:top = top + 1;s[top] = item;These two statements can also be written ass[+ + top] = itemBut, as we insert an item we must take tare of the overflow situation i.e., when topreaches STACK_SIZE-l, stack results in overflow condition and appropriate errormessage 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 ofwhich 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 asif (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 bys and top are affected and so they should be passed and used as pointers as shown inbelow example
  60. 60. Example 1: C function to insert an integer itemvoid 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 anitem of character data type, the changes done are provided in below example.Example 2: C function to insert a character itemvoid 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. 61. Deleting an element from the stack is called ‘pop’ operation. This can be achieved byfirst 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 emptythe top will be -1 and so, it is not possible to delete any item from the stack. The abovestatement has be executed only if stack is not empty. Hence, the code to delete an itemfrom stack can be written asif (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 pointervariable. The complete function is shown in below example 1. The example 2 shows howto delete a character item from the stack.Example 1: C function to delete an integer itemint pop(int *top, int s[ ] ){int item;if (*top == -1){return 0; /* Indicates empty stack */}item = s[(*top)--];/* Access the item and delete */
  62. 62. return item; /* Send the item deleted to the calling function */}Example 2: C function to delete a character itemchar 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 */}DisplayAssume 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 thestack is reached. So, first item to be displayed is 10, next item to be displayed is 20 andfinal item to be displayed is 30. So, the code corresponding to this can take the followingform
  63. 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 takesthe value -1. So, the modified code can be written as shown in below example.Example 1: C function to display the contents of the stackvoid 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 Questions1. Explain the POP and PUSH operations with an example.2. Write steps to display elements from the STACK.
  64. 64. Stack implementation using arraysIn 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 inbelow example. Two semicolons (i.e.,;;} in the for loop indicates that for loop is aninfinite 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. 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”);elseprintf(”Item deleted = %dn”, item);break;case 3:display( top,s );break;default:exit(0);}}}Output1 ush 2: Pop
  66. 66. 3: Display 4: ExitEnter the choice1Enter the item to be inserted101: Push 2: Pop3: Display 4: ExitEnter the choice1Enter the item to be inserted201: Push 2: Pop3: Display 4: ExitEnter the choice3Contents of the stack10201: Push 2: Pop3: Display 4: ExitEnter the choice2Item deleted = 20
  67. 67. 1: Push 2: Pop3: Display 4: ExitEnter the choice2Item deleted = 101: Push 2: Pop3: Display 4: ExitEnter the choice2Stack is empty1: Push 2: Pop3: Display 4: ExitEnter the choice4Applications of stackA stack is very useful in situations when data have to be stored and then retrieved in thereverse 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 mainprogram calls a function named F, a stack frame for F gets pushed on top of the stackframe for main. If F calls another function G, a new stack frame for G is pushed on top ofthe frame for F. When G finishes its processing and returns, its frame gets popped off thestack, restoring F to the top of the stack.ii. Large number Arithmetic:As another example, consider adding very large numbers. Suppose we wanted to add353,120,457,764,910,452,008,700 and 234,765,000,129,654,080,277. First of all notethat it would be difficult to represent the numbers as integer variables, as they cannot
  68. 68. hold such large values. The problem can be solved by treating the numbers as strings ofnumerals, store them on two stacks, and then perform addition by popping numbers fromthe stacks.iii. Evaluation of arithmetic expressions:Stacks are useful in evaluation of arithmetic expressions. Consider the expression 5 * 3+2 + 6 * 4The 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 inB. We finish off by adding A and B and leaving the final answer in A.A = 15 2 += 17B=64*= 24A = 17 24 += 41We 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 shallshortly show how this form can be generated using a stack.Basically there are 3 types of notations for expressions. The standard form is known asthe infix form. The other two are postfix and prefix forms.Infix: operator is between operands A + BPostfix : operator follows operands A B +Prefix: operator precedes operands + A BNote that all infix expressions can not be evaluated by using the left to right order of theoperators inside the expression. However, the operators in a postfix expression areALWAYS in the correct evaluation order. Thus evaluation of an infix expression is donein two steps. The first step is to convert it into its equivalent postfix expression. Thesecond step involves evaluation of the postfix expression. We shall see in this section,
  69. 69. how stacks are useful in carrying out both the steps. Let us first examine the basic processof 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 multiplicationa + ( b c * ) convert the additiona (b c * ) + Remove parenthesesa b c * + Postfix formNote 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 multiplicationA B + C * Postfix formNo need of parenthesis anywhereExample 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 examplesInfix Postfix
  70. 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 ExpressionWe can evaluate a postfix expression using a stack. Each operator in a postfix stringcorresponds to the previous two operands. Each time we read an operand we push it ontoa stack. When we reach an operator its associated operands (the top two elements on thestack) are popped out from the stack. We then perform the indicated operation on themand push the result on top of the stack so that it will be available for use as one of theoperands for the next operator. The following example shows how a postfix expressioncan be evaluated using a stack.Example6523+8*+3+*The process stops when there are no more operator left in the string. The result ofevaluating the expression is obtained just by popping off the single element. Moreexamples will be done in the lecture and recitation labs.