SlideShare a Scribd company logo
1 of 30
Download to read offline
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

What's hot

OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)Kai-Feng Chou
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by ExampleOlve Maudal
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Ismar Silveira
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Gamindu Udayanga
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationGem WeBlog
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)VC Infotech
 
Function pointer
Function pointerFunction pointer
Function pointerGem WeBlog
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1Little Tukta Lita
 
李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义yiditushe
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and StructuresGem WeBlog
 
OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)Kai-Feng Chou
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, RecursionSreedhar Chowdam
 
C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)Olve Maudal
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c languageMomenMostafa
 
Fpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directivesFpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directivesMalik Tauqir Hasan
 

What's hot (20)

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

Similar to Data structure week 1

Basic construction of c
Basic construction of cBasic construction of c
Basic construction of ckinish kumar
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5Sowri Rajan
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1ReKruiTIn.com
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro headerhasan Mohammad
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++Amresh Raj
 
Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxCheriviralaNikhil
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
Book management system
Book management systemBook management system
Book management systemSHARDA SHARAN
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5Sowri Rajan
 

Similar to Data structure week 1 (20)

C++ Constructs.pptx
C++ Constructs.pptxC++ Constructs.pptx
C++ Constructs.pptx
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Basic construction of c
Basic construction of cBasic construction of c
Basic construction of c
 
Embedded C.pptx
Embedded C.pptxEmbedded C.pptx
Embedded C.pptx
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Cpp
CppCpp
Cpp
 
Structure of the C Program.docx
Structure of the C Program.docxStructure of the C Program.docx
Structure of the C Program.docx
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptx
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
C programming session9 -
C programming  session9 -C programming  session9 -
C programming session9 -
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Storage class
Storage classStorage class
Storage class
 
Book management system
Book management systemBook management system
Book management system
 
Memory management in c++
Memory management in c++Memory management in c++
Memory management in c++
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 

More from karmuhtam

Devre analizi deney malzeme listesi
Devre analizi deney malzeme listesiDevre analizi deney malzeme listesi
Devre analizi deney malzeme listesikarmuhtam
 
Deney 3 ve 4
Deney 3 ve 4Deney 3 ve 4
Deney 3 ve 4karmuhtam
 
Deney 1 ve 2
Deney 1 ve 2Deney 1 ve 2
Deney 1 ve 2karmuhtam
 
Data structure week y 5 1
Data structure week y 5 1Data structure week y 5 1
Data structure week y 5 1karmuhtam
 
Data structure week y 5
Data structure week y 5Data structure week y 5
Data structure week y 5karmuhtam
 
Data structure week y 4
Data structure week y 4Data structure week y 4
Data structure week y 4karmuhtam
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3karmuhtam
 
Data structure week 2
Data structure week 2Data structure week 2
Data structure week 2karmuhtam
 
13. sınıfları başlık dosyaları
13.  sınıfları başlık dosyaları13.  sınıfları başlık dosyaları
13. sınıfları başlık dosyalarıkarmuhtam
 
11. stl kütüphanesi
11. stl kütüphanesi11. stl kütüphanesi
11. stl kütüphanesikarmuhtam
 
10. istisna isleme
10. istisna isleme10. istisna isleme
10. istisna islemekarmuhtam
 
9. şablonlar
9. şablonlar9. şablonlar
9. şablonlarkarmuhtam
 
8. çok biçimlilik
8. çok biçimlilik8. çok biçimlilik
8. çok biçimlilikkarmuhtam
 
7. kalıtım
7. kalıtım7. kalıtım
7. kalıtımkarmuhtam
 
6. this işaretçisi ve arkadaşlık
6. this işaretçisi ve arkadaşlık6. this işaretçisi ve arkadaşlık
6. this işaretçisi ve arkadaşlıkkarmuhtam
 
5. kurucu, yok edici ve kopyalama fonksiyonları
5. kurucu, yok edici ve kopyalama fonksiyonları5. kurucu, yok edici ve kopyalama fonksiyonları
5. kurucu, yok edici ve kopyalama fonksiyonlarıkarmuhtam
 
4. nesneler ve sınıflar
4. nesneler ve sınıflar4. nesneler ve sınıflar
4. nesneler ve sınıflarkarmuhtam
 

More from karmuhtam (20)

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

Recently uploaded

MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 

Recently uploaded (20)

MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 

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