SlideShare a Scribd company logo
Chapter 1
Data Structures
Assoc. Prof. Dr. Oğuz FINDIK
2016-2017
KBUZEM
KARABUK UNIVERSITY
1
 Referencess Books
 A book on C All KELLEY, İra POHL
 Data Structures and Program Design in C++ By Robert L.
Kruse and Alexander J Ryba
Referencess Book
2
 You can not do anything without data on world . İf you want to
proceed add operator, you should have at least two number.
 Computer sciences deal with storing, organizing and retrieving
effectively of data.
 Computer programmer should get data from user or another
sources and use them.
 for this purpose you have to use data structure for every software
program or system.
Why data structures is important for
computer engineering?
3
 WE will use eclipse IDE for developing program in this course.
 Steps to install Eclipse and run
 Download eclipse Neon from
http://www.eclipse.org/downloads/
 Choose Eclipse IDE for C/C++ Developers and install
 You should download and install MinGW GCC
http://www.mingw.org/
Eclipse
4
 Functions break large computing tasks into smaller
ones.
 Taking a problem and breaking into small,
manageable pieces is ritical to writing large programs.
 So important definition for function,
 Functions return values to where is invoked.
C programming
5
 Function definition
 type function_name(parameter list ) {declerations
statements}
 The parameter list is a comma-seperated list of declarations.
Functions
6
Example
7
Scope of Variables
8
 return; // return ++a; // return (a*b)
 When a return statement is encountered, execution of the
function is terminated and control is passed back to calling
environment. İf the return statement contains an expression,
then the value of the expressions is passed to the calling
environment as well.
Return Statement
9
Example
10
 Every variable and function in C has two attributes.
Type and storage class. Four storage classes are
automatics, external, register and static with
corresponding
 auto extern register static
Storage Classes
11
 Storage class auto:
 Variables declared within function are automatic by default.
These variables can be used in scope of the function.
 Storage class extern:
 One methods of transmitting information across blocks and
functions is to use external variables. When a variable is
declared outside a function, storage is permanently assigned
to it, and its storage class is extern.
Storage Classes
12
 #include <stdio.h>
 extern int a = 1, b = 2;
 c = 3;
 int f(void);
 int main(void) {
 printf("%3dn", f());
 printf("%3d%3d%3dn", a, b, c);
 return 0;
 }
 int f(void) {
 auto int b, c;
 a = b = c = 4;
 return (a + b + c);
 }
Example
13
file2.c
int f(void) {
extern a;
int b, c;
b = c = a;
return (a + b +
c);
}
Extern keyword
 This use of extern is used to tell the
compiler to ‘’look for it elsewhere’’ either
in this file or in some other file.
Örnek1.c
#include <stdio.h>
int a = 1, b = 2;
c = 3;
int f(void);
int main(void) {
printf("%3dn", f());
printf("%3d%3d%3dn", a, b, c);
return 0;
} 14
 Storage class register tells the compiler that the association
variables should be stored in high-speed memory registers.
#include <stdio.h>
#include <time.h>
int a =1;
#define N 10000
int main(void) {
clock_t start, end;
double cpu_time_used;
register double i;
start = clock();
for(i=0;i<N;i=i+0.0001);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Running time is %f",cpu_time_used);
return 0;
}
Storage Class Register
 Running
time is
0,163
second
with
register
variable.
 Running
time is
0,419
second
without
register
variable.
15
 Static declarations have two important and distinct
uses. One of them is to allow a local variable to retain its
previous value when the block is reentered.
Storage class static
16
 The second and more subtle use of static is in connection with
external declarations. İt is use to restriction of the scope of
the variable.
Storage class static
17
 A typical memory
representation of C program
consists of following sections.
 1. Text segment
2. Initialized data segment
3. Uninitialized data segment
4. Stack
5. Heap
18
Memory Layout of C Programs
 1. Text Segment:
 A text segment , also known as a code segment or simply as
text, is one of the sections of a program in an object file or in
memory, which contains executable instructions.
 Usually, the text segment is sharable so that only a single
copy needs to be in memory for frequently executed
programs, such as text editors, the C compiler, the shells,
and so on. Also, the text segment is often read-only, to
prevent a program from accidentally modifying its
instructions. 19
Memory Layout of C Programs
 2. Initialized Data Segment: A data segment is a portion of
virtual address space of a program, which contains the global
variables and static variables that are initialized by the
programmer.
 3. Uninitialized Data Segment:Data in this segment is
initialized by the kernel to arithmetic 0 before the program
starts executing uninitialized data starts at the end of the
data segment and contains all global variables and static
variables that are initialized to zero or do not have explicit
initialization in source code.
20
Memory Layout of C Programs
 4. Stack:
 Stack, where automatic variables are stored, along with
information that is saved each time a function is called. Each
time a function is called, the address of where to return to
and certain information about the caller’s environment, such
as some of the machine registers, are saved on the stack. The
newly called function then allocates room on the stack for its
automatic and temporary variables.
 This is how recursive functions in C can work. Each time a
recursive function calls itself, a new stack frame is used, so
one set of variables doesn’t interfere with the variables from
another instance of the function.
21
Memory Layout of C Programs
 5. Heap:
 Heap is the segment where dynamic memory allocation
usually takes place.
 Heap area is managed by malloc, realloc, and free.
22
Memory Layout of C Programs
#include <stdio.h>
int global; /* Uninitialized variable stored in bss*/
int main(void)
{
int *ptr_one;
ptr_one = (int *)malloc(sizeof(int));
int c;//stack
static int i = 100; /* Initialized static variable stored in DS*/
return 0;
}
23
Example
 A function is said to be recursive if it class itself
directly or indirectly. in C, all functions can be used
recursively, including main function.
Recursion
24
Examples
25
 Typically, a large program is written in a separate
directory as a collection of .h and .c file, with each .c
file contains one or more functions definition
 When preprocessor encounters #include "pgm.h«
directive search this file in the same directory or
system-dependent places. İf it cannot be found,
preprocessor issues an error message and
compilations stops.
Developing Large Program
26
 .h files contain #includes, #defines, templates of enumaration
types, templates of structure and union types, and list of
function prototype at the bottom.
 pgm.h file:
 #include <stdio.h>
 #include <stdlib.h>
 #define N 3
 void fct1(int k);
 void fct2(void);
 void wrt_info(char *);
Developing Large Program
27
 fct.c file
 #include "pgm.h"
 void fct1(int n)
 {
 int i;
 printf("Hello from fct1()n");
 for(i=0;i<n;++i)
 fct2();
 }
 void fct2(void){
 printf("Hello from fct2()n");
 }
Example
28
 wrt.c file:
 #include "pgm.h"
 void wrt_info(char
*pgm_name){
 printf("Usage:
%snn",pgm_name);
 printf("%sn",
 "Hello from wrt_info-1n"
 "Hello from wrt_info-2n"
 "Hello from wrt_info-3n");
 }
Example
 main.c file:
 #include "pgm.h"
 int main(void) {
 char ans;
 int i, n = N;
 printf("%s", "This program does not do very much.n
"
 "Do you want to more
information?");
 fflush(stdout);
 scanf("%c", &ans);
 if (ans == 'y' || ans == 'Y')
 wrt_info("pgm");
 for (i = 0; i < n; ++i)
 fct1(i);
 printf("Bye!n");
 return 0;
 }
 }
29
Tower of Hanoi
 Write c code that
solves this problem.
You have to use
recursive function.
30

More Related Content

DOC
12. stl örnekler
PDF
Virtual Functions
PDF
C++aptitude questions and answers
PDF
Cbse marking scheme 2006 2011
PDF
C Prog - Functions
DOCX
Virtual function
PDF
C++ aptitude
PPT
Functions in c
12. stl örnekler
Virtual Functions
C++aptitude questions and answers
Cbse marking scheme 2006 2011
C Prog - Functions
Virtual function
C++ aptitude
Functions in c

What's hot (20)

PDF
OOP in C - Inherit (Chinese Version)
PDF
Solid C++ by Example
PPT
Paradigmas de Linguagens de Programacao - Aula #4
PPTX
Pointers Refrences & dynamic memory allocation in C++
PDF
Dynamic memory allocation
PPTX
Function in c language(defination and declaration)
PPTX
Function pointer
PPTX
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
PDF
李建忠、侯捷设计模式讲义
PDF
Pointers and Structures
PDF
C Programming Tutorial - www.infomtec.com
PDF
Unit 3 (1)
ODP
OOP in C - Before GObject (Chinese Version)
PPT
An imperative study of c
PDF
C Programming Storage classes, Recursion
PDF
Python Programming
PDF
C++ idioms by example (Nov 2008)
PDF
1 introducing c language
PPT
Fpga 06-data-types-system-tasks-compiler-directives
PPT
Lecture 8- Data Input and Output
OOP in C - Inherit (Chinese Version)
Solid C++ by Example
Paradigmas de Linguagens de Programacao - Aula #4
Pointers Refrences & dynamic memory allocation in C++
Dynamic memory allocation
Function in c language(defination and declaration)
Function pointer
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
李建忠、侯捷设计模式讲义
Pointers and Structures
C Programming Tutorial - www.infomtec.com
Unit 3 (1)
OOP in C - Before GObject (Chinese Version)
An imperative study of c
C Programming Storage classes, Recursion
Python Programming
C++ idioms by example (Nov 2008)
1 introducing c language
Fpga 06-data-types-system-tasks-compiler-directives
Lecture 8- Data Input and Output
Ad

Similar to Data structure week 1 (20)

PPTX
C++ Constructs.pptx
PPTX
Storage classes in C
PPTX
Functions and Header files ver very useful
DOC
Basic construction of c
PPTX
Embedded C.pptx
PDF
Unit 5 quesn b ans5
PPT
C, C++ Interview Questions Part - 1
PDF
CP MATERIAL.pdf Computer Programming Material
PPTX
Presentation on Function in C Programming
DOCX
Structure of the C Program.docx
PDF
Introduction To Programming With C 1st Edition Nhce
PDF
6 preprocessor macro header
PPTX
Interoduction to c++
PPTX
Storage_classes_and_Scope_rules.pptx
DOCX
Basic structure of c programming
DOCX
Basic structure of c programming
PDF
C programming session9 -
PPTX
Programming in C
DOCX
Storage class
C++ Constructs.pptx
Storage classes in C
Functions and Header files ver very useful
Basic construction of c
Embedded C.pptx
Unit 5 quesn b ans5
C, C++ Interview Questions Part - 1
CP MATERIAL.pdf Computer Programming Material
Presentation on Function in C Programming
Structure of the C Program.docx
Introduction To Programming With C 1st Edition Nhce
6 preprocessor macro header
Interoduction to c++
Storage_classes_and_Scope_rules.pptx
Basic structure of c programming
Basic structure of c programming
C programming session9 -
Programming in C
Storage class
Ad

More from karmuhtam (20)

PDF
Devre analizi deney malzeme listesi
PDF
Deney 6
PDF
Deney 5
PDF
Deney 3 ve 4
PDF
Deney 1 ve 2
PDF
Data structure week y 5 1
PDF
Data structure week y 5
PPTX
Data structure week y 4
PDF
Data structure week 3
PDF
Data structure week 2
DOC
13. sınıfları başlık dosyaları
DOC
11. stl kütüphanesi
DOC
10. istisna isleme
DOC
9. şablonlar
DOC
8. çok biçimlilik
DOC
7. kalıtım
DOC
6. this işaretçisi ve arkadaşlık
DOC
5. kurucu, yok edici ve kopyalama fonksiyonları
DOCX
4. yapılar
DOC
4. nesneler ve sınıflar
Devre analizi deney malzeme listesi
Deney 6
Deney 5
Deney 3 ve 4
Deney 1 ve 2
Data structure week y 5 1
Data structure week y 5
Data structure week y 4
Data structure week 3
Data structure week 2
13. sınıfları başlık dosyaları
11. stl kütüphanesi
10. istisna isleme
9. şablonlar
8. çok biçimlilik
7. kalıtım
6. this işaretçisi ve arkadaşlık
5. kurucu, yok edici ve kopyalama fonksiyonları
4. yapılar
4. nesneler ve sınıflar

Recently uploaded (20)

PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Classroom Observation Tools for Teachers
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Pre independence Education in Inndia.pdf
PPTX
Lesson notes of climatology university.
PPTX
Cell Structure & Organelles in detailed.
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Pharma ospi slides which help in ospi learning
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Institutional Correction lecture only . . .
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPH.pptx obstetrics and gynecology in nursing
Classroom Observation Tools for Teachers
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Module 4: Burden of Disease Tutorial Slides S2 2025
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Pre independence Education in Inndia.pdf
Lesson notes of climatology university.
Cell Structure & Organelles in detailed.
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Renaissance Architecture: A Journey from Faith to Humanism
O7-L3 Supply Chain Operations - ICLT Program
Supply Chain Operations Speaking Notes -ICLT Program
Pharma ospi slides which help in ospi learning
human mycosis Human fungal infections are called human mycosis..pptx
Cell Types and Its function , kingdom of life
STATICS OF THE RIGID BODIES Hibbelers.pdf
Institutional Correction lecture only . . .
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

Data structure week 1

  • 1. Chapter 1 Data Structures Assoc. Prof. Dr. Oğuz FINDIK 2016-2017 KBUZEM KARABUK UNIVERSITY 1
  • 2.  Referencess Books  A book on C All KELLEY, İra POHL  Data Structures and Program Design in C++ By Robert L. Kruse and Alexander J Ryba Referencess Book 2
  • 3.  You can not do anything without data on world . İf you want to proceed add operator, you should have at least two number.  Computer sciences deal with storing, organizing and retrieving effectively of data.  Computer programmer should get data from user or another sources and use them.  for this purpose you have to use data structure for every software program or system. Why data structures is important for computer engineering? 3
  • 4.  WE will use eclipse IDE for developing program in this course.  Steps to install Eclipse and run  Download eclipse Neon from http://www.eclipse.org/downloads/  Choose Eclipse IDE for C/C++ Developers and install  You should download and install MinGW GCC http://www.mingw.org/ Eclipse 4
  • 5.  Functions break large computing tasks into smaller ones.  Taking a problem and breaking into small, manageable pieces is ritical to writing large programs.  So important definition for function,  Functions return values to where is invoked. C programming 5
  • 6.  Function definition  type function_name(parameter list ) {declerations statements}  The parameter list is a comma-seperated list of declarations. Functions 6
  • 9.  return; // return ++a; // return (a*b)  When a return statement is encountered, execution of the function is terminated and control is passed back to calling environment. İf the return statement contains an expression, then the value of the expressions is passed to the calling environment as well. Return Statement 9
  • 11.  Every variable and function in C has two attributes. Type and storage class. Four storage classes are automatics, external, register and static with corresponding  auto extern register static Storage Classes 11
  • 12.  Storage class auto:  Variables declared within function are automatic by default. These variables can be used in scope of the function.  Storage class extern:  One methods of transmitting information across blocks and functions is to use external variables. When a variable is declared outside a function, storage is permanently assigned to it, and its storage class is extern. Storage Classes 12
  • 13.  #include <stdio.h>  extern int a = 1, b = 2;  c = 3;  int f(void);  int main(void) {  printf("%3dn", f());  printf("%3d%3d%3dn", a, b, c);  return 0;  }  int f(void) {  auto int b, c;  a = b = c = 4;  return (a + b + c);  } Example 13
  • 14. file2.c int f(void) { extern a; int b, c; b = c = a; return (a + b + c); } Extern keyword  This use of extern is used to tell the compiler to ‘’look for it elsewhere’’ either in this file or in some other file. Örnek1.c #include <stdio.h> int a = 1, b = 2; c = 3; int f(void); int main(void) { printf("%3dn", f()); printf("%3d%3d%3dn", a, b, c); return 0; } 14
  • 15.  Storage class register tells the compiler that the association variables should be stored in high-speed memory registers. #include <stdio.h> #include <time.h> int a =1; #define N 10000 int main(void) { clock_t start, end; double cpu_time_used; register double i; start = clock(); for(i=0;i<N;i=i+0.0001); end = clock(); cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; printf("Running time is %f",cpu_time_used); return 0; } Storage Class Register  Running time is 0,163 second with register variable.  Running time is 0,419 second without register variable. 15
  • 16.  Static declarations have two important and distinct uses. One of them is to allow a local variable to retain its previous value when the block is reentered. Storage class static 16
  • 17.  The second and more subtle use of static is in connection with external declarations. İt is use to restriction of the scope of the variable. Storage class static 17
  • 18.  A typical memory representation of C program consists of following sections.  1. Text segment 2. Initialized data segment 3. Uninitialized data segment 4. Stack 5. Heap 18 Memory Layout of C Programs
  • 19.  1. Text Segment:  A text segment , also known as a code segment or simply as text, is one of the sections of a program in an object file or in memory, which contains executable instructions.  Usually, the text segment is sharable so that only a single copy needs to be in memory for frequently executed programs, such as text editors, the C compiler, the shells, and so on. Also, the text segment is often read-only, to prevent a program from accidentally modifying its instructions. 19 Memory Layout of C Programs
  • 20.  2. Initialized Data Segment: A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer.  3. Uninitialized Data Segment:Data in this segment is initialized by the kernel to arithmetic 0 before the program starts executing uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code. 20 Memory Layout of C Programs
  • 21.  4. Stack:  Stack, where automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller’s environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables.  This is how recursive functions in C can work. Each time a recursive function calls itself, a new stack frame is used, so one set of variables doesn’t interfere with the variables from another instance of the function. 21 Memory Layout of C Programs
  • 22.  5. Heap:  Heap is the segment where dynamic memory allocation usually takes place.  Heap area is managed by malloc, realloc, and free. 22 Memory Layout of C Programs
  • 23. #include <stdio.h> int global; /* Uninitialized variable stored in bss*/ int main(void) { int *ptr_one; ptr_one = (int *)malloc(sizeof(int)); int c;//stack static int i = 100; /* Initialized static variable stored in DS*/ return 0; } 23 Example
  • 24.  A function is said to be recursive if it class itself directly or indirectly. in C, all functions can be used recursively, including main function. Recursion 24
  • 26.  Typically, a large program is written in a separate directory as a collection of .h and .c file, with each .c file contains one or more functions definition  When preprocessor encounters #include "pgm.h« directive search this file in the same directory or system-dependent places. İf it cannot be found, preprocessor issues an error message and compilations stops. Developing Large Program 26
  • 27.  .h files contain #includes, #defines, templates of enumaration types, templates of structure and union types, and list of function prototype at the bottom.  pgm.h file:  #include <stdio.h>  #include <stdlib.h>  #define N 3  void fct1(int k);  void fct2(void);  void wrt_info(char *); Developing Large Program 27
  • 28.  fct.c file  #include "pgm.h"  void fct1(int n)  {  int i;  printf("Hello from fct1()n");  for(i=0;i<n;++i)  fct2();  }  void fct2(void){  printf("Hello from fct2()n");  } Example 28
  • 29.  wrt.c file:  #include "pgm.h"  void wrt_info(char *pgm_name){  printf("Usage: %snn",pgm_name);  printf("%sn",  "Hello from wrt_info-1n"  "Hello from wrt_info-2n"  "Hello from wrt_info-3n");  } Example  main.c file:  #include "pgm.h"  int main(void) {  char ans;  int i, n = N;  printf("%s", "This program does not do very much.n "  "Do you want to more information?");  fflush(stdout);  scanf("%c", &ans);  if (ans == 'y' || ans == 'Y')  wrt_info("pgm");  for (i = 0; i < n; ++i)  fct1(i);  printf("Bye!n");  return 0;  }  } 29
  • 30. Tower of Hanoi  Write c code that solves this problem. You have to use recursive function. 30