Docsity
Docsity

Prepara tus exámenes
Prepara tus exámenes

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity


Consigue puntos base para descargar
Consigue puntos base para descargar

Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium


Orientación Universidad
Orientación Universidad


estructura de datos2, Ejercicios de Programación Java

un buen libro para empezar a programar

Tipo: Ejercicios

2019/2020

Subido el 05/09/2020

wilmer-andres-perez
wilmer-andres-perez 🇨🇴

1 documento

1 / 72

Toggle sidebar

Esta página no es visible en la vista previa

¡No te pierdas las partes importantes!

bg1
P.O.Box 342-01000 Thika
Web: www.ku.ac.ke
DEPATMENT OF INFORMATION TECHNOLOGY
COURSE CODE: BIT 2202
COURSE TITLE: DATA STRUCTURES AND ALGORITHMS
Instructional manual for BBIT- Distance Learning
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48

Vista previa parcial del texto

¡Descarga estructura de datos2 y más Ejercicios en PDF de Programación Java solo en Docsity!

P.O.Box 342-01000 Thika Email: [email protected] Web: www.ku.ac.ke

DEPATMENT OF INFORMATION TECHNOLOGY

COURSE CODE: BIT 2202

COURSE TITLE: DATA STRUCTURES AND ALGORITHMS

Instructional manual for BBIT- Distance Learning

Contents

  • CHAPTER ONE
    • DATA STRUCTURES
    • What is Linked List?
    • 1.4.1 Pros and Cons of Linked Lists
    • 1.4.2 What is the good and bad thing about linked list?
    • 1.4.3 Types of linked lists
      • Linearly linked lists
      • Circularly linked lists
      • Singly Linked Lists
      • Doubly linked lists
      • Multiply-linked Lists
      • Binary Trees
    • 1.5.4 Traversal methods
  • CHAPTER TWO
    • INFIX AND POSTFIX EXPRESSION (REVERSE POLISH NOTATION)
  • CHAPTER THREE
  • CHAPTER FOUR
    • SEARCH AND SORT ALGORITHMS
  • SAMPLE EXAMS QUESTIONS

 Algorithms for manipulating linked list  Algorithm for manipulating trees  Algorithms for manipulating postfix and infix expressions 

4. Recursive functions  Factorial functions  Power functions  Fibonacci functions 5. Postfix and infix expressions 6 .Traversal of the tree  Pre-order traversal  In-order traversal  Post-order traversal 7. Search  Linear search  Binary search 8. Sorting  Bubble sort algorithm  Selection sort algorithm  Insertion sort algorithm  Quick sort algorithm AAsssseessssmmeennttss  Continuous Assessment Tests (CATs) (30%)  End of semester examination (70%) Total = 100%

RReeqquuiirreedd^ tteexxtt^ bbooookkss

Salamis S, Data structures, Algorithms and application in Java, McGraw Hill Banachowski I et al, Analysis of algorithms and Data structures, Addison wiley

TTeexxtt^ bbooookkss^ ffoorr^ ffuurrtthheerr^ rreeaaddiinngg

MKlaus W., Algorithms, data structures, programmes, New Delhi, MCGraw Hill

Compiled by : Joshua Agola

CHAPTER ONE

DATA STRUCTURES

1.1 ARRAY DATA STRUCTURES

What is an array?

Array is a very basic data structure provided by every programming language. Let’s talk about an example scenario where we need to store ten employees’ data in our C/C++ program including name, age and salary. One of the solutions is to declare ten different variables to store employee

Learning objectives:

By the end of the chapter a student shall be able to:  Understand and Manipulate data structures  Design and apply algorithms to various data structures  Understand the application areas of various data structures

int Age[NUM_EMPLOYEE];

How to initialise an array?

Initialisation of array is very simple in c programming. There are two ways you can initialise arrays.

Declare and initialise array in one statement.

Declare and initialise array separately.

Look at the following C code which demonstrates the declaration and initialisation of an array.

int Age [5] = {30, 22, 33, 44, 25};

int Age [5];

Age [0]=30;

Age [1]=22;

Age [2]=33;

Age [3]=44;

Age [4]=25;

Array can also be initialised in a ways that array size is omitted, in such case compiler automatically allocates memory to array.

int Age [ ] = {30, 22, 33, 44, 25};

Let’s write a simple program that uses arrays to print out number of employees having salary more than 3000.

Array in C Programming

#include

#include

#include

#define NUM_EMPLOYEE 10

int main(int argc, char *argv[]){

int Salary[NUM_EMPLOYEE], lCount=0,gCount=0,i=0;

printf("Enter employee salary (Max 10)\n ");

for (i=0; i

1.1.2 How to declare and initialize multi-dimensional arrays?

Often there is need to manipulate tabular data or matrices. For example if employee salary is increased by 20% and you are required to store both the salaries in your program. Then you will need to store this information into a two dimensional arrays. C/C++ gives you the ability to have arrays of any dimension.

Multi dimension arrays

Consider the example above, you have to store, previous salary, present salary and amount of increment. In that case you will need to store this information in three dimensional arrays.

First I will show you how to declare a two dimensional array and initialise it. Then write a complete program to use multidimensional arrays.

int Salary[10][2];

This defines an array containing 10 elements of type int. Each of these elements itself is an array of two integers. So to keep track of each element of this array is we have to use two indices. One is to keep track of row and other is to keep track of column.

Elements of multidimensional arrays

Here is a graphical view of multidimensional array that we use to store salary and increment on salary. First column stores the salary element of the array and second column stores increment on salary. We could add another column to store the new salary which adds the increment to the salary.

Column 0 – Salary

Column 1 – Increment

Row 0

Row 2

Row 3

Row 4

Row 5

Row 6

Row 7

Row 8

Row 9

Row 10

Initialising multidimensional arrays

Multidimensional arrays can also be initialised in two ways just like one dimensional array. Two braces are used to surround the row element of arrays.

If you are initialising more than one dimension then you will have to use as many braces as the dimensions of the array are.

int Salary [5][2] = {

{2300, 460},

{3400, 680},

{3200, 640},

{1200, 240},

{3450, 690}

};

int Salary [5][2] ={0}; //This will initialise all the array elements to 0

int Salary [5][2];

Salary [0][0]=2300;

Salary [1][0]=3400;

int lCount=0,gCount=0,i=0;

for(i=0; i

#include

#define NUM_EMPLOYEE 10

using namespace std;

int main(int argc, char *argv[]){

//initialise Salary of each employee

int Salary[NUM_EMPLOYEE][2]={

{2300,0},

{3400,0},

{3200,0},

{1200,0},

{3450,0},

1.2 STACK DATA STRUCTURE

Two of the more common data objects found in computer algorithms are stacks and queues. Both of these objects are special cases of the more general data object, an ordered list.

A stack is an ordered list in which all insertions and deletions are made at one end, called the top. A queue is an ordered list in which all insertions take place at one end, the rear , while all deletions take place at the other end, the front. Given a stack S=(a[1],a[2],.......a[n]) then we say that a1 is the bottommost element and element a[i]) is on top of element a[i-1], 1

  1. Test whether the stack is empty

If the top = = 0

  1. Pushing items into the stack
  1. Increment top by 1

  2. Insert the item

  1. Poping items from the stack(Remove the item)

Decrease top by 1

1.2.2 The stack class

Class stack

{

Int top;

Int stackarray[50];

Public:

Stack( );

Int emptystack( );

Int fullstack( );

Void push(int item);

Void pop(int &item);

};

Example

Using stack structure write a program for displaying numbers in the reverse order

Solution

#Include < iostream.h>

Const int maxsize = 20;

{

Int top;

Int stackarray[maxsize];

Public:

Int emptystack( );

Int fullstack( );

Void push(int item);

Void pop(int & item);

};

Stack : : stack( )

{

Top = 0;

}

Int stack : : emptystack( )

{

Return top = = 0;

}

Int stack : : fullstack( )

{

Return top = = maxsize;

}

S. pop (x);

cout <

i)Compute remainder ii)Push remainder into the stack iii)Compute next number (the quotient become the next number)

  1. Display the content of stack((pop)

Example 2

Using stack, write a program for converting a number from base 10 to any other base (1-9)

Solution

include

Const int max size = 50;

{

class stack Int top; Int stackarray(maxsize)

Public:

Stack ( );

Int emptystack( );

Int fullstack( );

Void push(int item);

};

{

Top = 0;

}

int stack : : emptystack ( )

{

Return top = = 0;

}

Int stack: : fullstack ( )

{return top = = max size;

}

Void stock : : push (int item)

{

Top ++;

Stakarray (top) = item;

}

Void stock : : pop (int pitem)

{

Item = stackrray (top);

Top = - ;

}

Void main( )