Stack and Queue
1
Stack
Data structure with Last-In First
In First-Out (LIFO) behavior
In
C
B
A
Out
B
C
2
Typical Operations
on Stack
Pop
Push
isempty: determines if the stack has no elements
isfull: determines if the stack is full in case
of a bounded si
Functions
1
Function
A program segment that carries out some
specific, well-defined task
defined
Example
A function to add two numbers
A function to find the largest of n numbers
A function will carry out its intended task
whenever it is called or invoked
Expressions
1
Expressions
Variables and constants linked with operators
Arithmetic expressions
Uses arithmetic operators
Can evaluate to any value
Logical expressions
Uses relational and logical operators
Evaluates to 1 or 0 (true or false) only
Assignmen
CS11001:
Programming & Data Structures
Dept. of Computer Science & Engineering
1
Course Materials
Slides available at http:/cse.iitkgp.ac.in/~agupta/pds
More materials available at http:/cse.iitkgp.ac.in/~pds
Books:
1.
2.
3.
Programming with C (Second Edi
Number
Representation
1
Number System : The Basics
We are accustomed to using the so-called
decimal number system
Ten digits : 0,1,2,3,4,5,6,7,8,9
Every digit position has a weight which is a
power of 10
Base or radix is 10
Example:
234 = 2 x 102 + 3 x
Recursion
1
Recursion
A process by which a function calls itself
repeatedly
Either directly.
Or
X calls X
cyclically in a chain.
X calls Y, and Y calls X
Used for repetitive computations in which each
action is stated in terms of a previous result
fact(
Looping
1
Loops
Group of statements that are executed
repeatedly while some condition
remains true
Each execution of the group of
statements is called an iteration of the
loop
2
Example
counter 1, sum 0
counter < 6
Read 5 integers
and display the
their s
Expressions
1
Expressions
Variables and constants linked with operators
Arithmetic
Uses arithmetic operators
Can evaluate to any value
Logical
expressions
expressions
Uses relational and logical operators
Evaluates to 1 or 0 (true or false) only
Assign
1-d Arrays
1
Array
Many applications require multiple data
items that have common characteristics
In
mathematics, we often express such groups
of data items in indexed form:
x1, x2, x3, , xn
Array is a data structure which can
represent a collection of d
Conditional Statement
1
Conditional Statements
Allow different sets of instructions to be
executed depending on truth or falsity of a
logical condition
Also called Branching
How do we specify conditions?
Using expressions
non-zero value means condition i
Structures
1
What is a Structure?
Used for handling a group of logically
related data items
Examples:
Student name, roll number, and marks
Real part and complex part of a complex number
Helps in organizing complex data in a more
meaningful way
The ind
1-d Arrays
d
1
Array
Many applications require multiple data
items that have common characteristics
In mathematics, we often express such
groups of data items in indexed form:
x1, x2, x3, , xn
Array is a data structure which can
represent a collection of
Looping
1
Loops
Group of statements that are executed
repeatedly while some condition
remains true
Each execution of the group of
statements is called an iteration of the
loop
2
Example
counter 1, sum 0
false
counter < 6
Read 5 integers
and display the
th
Conditional Statement
1
Conditional Statements
Allow different sets of instructions to be
executed depending on truth or falsity of a
logical condition
Also called Branching
How do we specify conditions?
Using expressions
non-zero value means condition is
File Handling
1
Storage seen so far
All variables stored in memory
Problem: the contents of memory are wiped out
when the computer is powered off
Example: Consider keeping students records
100 students records are added in array of
structures
Machine is t
Linked List
1
List
A list refers to a sequence of data items
Example: An array
The array index is used for accessing and
manipulation of array elements
Problems with arrays
The array size has to be specified at the
beginning (at least during dynamic alloc
Sorting
1
Sorting Data Items
Consider a set of data items
Each item may have more than one field
Example: a student record with name, roll no, CGPA,
Sort the set in ascending/descending order of
some key value (some value of the data)
Sort a set of intege
2-d
2-d Arrays
1
Two Dimensional Arrays
We have seen that an array variable can store
a list of values
Many applications require us to store a table
of values
Subject 1 Subject 2
Student 1
Student 2
Student 3
Student 4
75
68
88
50
82
75
74
65
Subject 3
Su
Number
Representation
1
Number System : The Basics
We are accustomed to using the so-called
decimal number system
Ten digits : 0,1,2,3,4,5,6,7,8,9
Every digit position has a weight which is a
power of 10
Base or radix is 10
Example:
234 = 2 x 102 + 3 x 10
Dynamic Memory
Allocation
1
Problem with Arrays
Sometimes
Amount of data cannot be predicted beforehand
Number of data items keeps changing during program
execution
Example: Seach for an element in an array of N elements
One solution: find the maximum pos
Recursion
1
Recursion
A process by which a function calls itself
repeatedly
Either directly.
X calls X
Or cyclically in a chain.
X calls Y, and Y calls X
Used for repetitive computations in which each
action is stated in terms of a previous result
fact(n)
Pointers: Parameter
Passing and Return
1
Passing Pointers to a Function
Pointers are often passed to a function as
arguments
Allows data items within the calling function to be
accessed by the called function, altered, and then
returned to the calling fun
CS11001:
Programming & Data Structures
Dept. of Computer Science & Engineering
1
Course Materials
Slides available at http:/cse.iitkgp.ac.in/~agupta/pds
More materials available at http:/cse.iitkgp.ac.in/~pds
Books:
1.
2.
3.
Programming with C
Byron Gottf
Pointers and Arrays
1
Pointers and Arrays
When an array is declared,
The compiler allocates sufficient amount of
storage to contain all the elements of the
array in contiguous memory locations
The base address is the location of the first
element (index 0
Structures
1
What is a Structure?
Used for handling a group of logically
related data items
Examples:
Student name, roll number, and marks
Real part and complex part of a complex number
Helps in organizing complex data in a
more meaningful way
The individ
Pointers: Basics
What is a pointer?
First of all, it is a variable, just like other
variables you studied
So it has type, storage etc.
Difference: it can only store the address
(rather than the value) of a data item
Type of a pointer variable pointer to t
DS: CS 11002
9
Computer Sc & Engg: IIT Kharagpur
6
1
Structure of a C Program
A program in C language is a collection of functions and declarations. It also have C preprocessor (cpp) directives. A function named main() is mandatory.
ect 2
8
Goutam Biswas
DS: CS 11002
'
Computer Sc & Engg: IIT Kharagpur
$
1
A Few More C Programs
ect 2a
&
Goutam Biswas
%
DS: CS 11002
'
Computer Sc & Engg: IIT Kharagpur
$
2
rst.c /* * This is the first Program first.c * Compile: cc -Wall first.c */ #include <stdio.h> int mai