SlideShare a Scribd company logo
Class No 1 Data Structures http://ecomputernotes.com
Data Structures Prepares the students for (and is a prerequisite for) the more advanced material students will encounter in later courses.  Cover well-known data structures such as dynamic arrays, linked lists, stacks, queues, tree and graphs.  Implement data structures in C++ http://ecomputernotes.com
Data Structures Prepares the students for (and is a prerequisite for) the more advanced material students will encounter in later courses.   Cover well-known data structures such as dynamic arrays, linked lists, stacks, queues, tree and graphs.  Implement data structures in C++ http://ecomputernotes.com
Data Structures Prepares the students for (and is a prerequisite for) the more advanced material students will encounter in later courses.  Cover well-known data structures such as dynamic arrays, linked lists, stacks, queues, tree and graphs.  Implement data structures in C++ http://ecomputernotes.com
Need for Data Structures Data structures organize data     more efficient programs. More powerful computers     more complex applications. More complex applications demand more calculations. http://ecomputernotes.com
Need for Data Structures Data structures organize data     more efficient programs. More powerful computers     more complex applications. More complex applications demand more calculations. http://ecomputernotes.com
Need for Data Structures Data structures organize data     more efficient programs. More powerful computers     more complex applications. More complex applications demand more calculations. http://ecomputernotes.com
Organizing Data Any organization for a collection of records that can be searched, processed in any order, or modified. The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days. http://ecomputernotes.com
Organizing Data Any organization for a collection of records that can be searched, processed in any order, or modified. The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days. http://ecomputernotes.com
Efficiency A solution is said to be  efficient  if it solves the problem within its  resource constraints . Space Time The cost of a solution is the amount of resources that the solution consumes. http://ecomputernotes.com
Efficiency A solution is said to be efficient if it solves the problem within its resource constraints. Space Time The  cost  of a solution is the amount of resources that the solution consumes. http://ecomputernotes.com
Selecting a Data Structure Select a data structure as follows: Analyze the problem to determine the resource constraints a solution must meet. Determine the basic operations that must be supported.  Quantify the resource constraints for each operation. Select the data structure that best meets these requirements. http://ecomputernotes.com
Selecting a Data Structure Select a data structure as follows: Analyze the problem to determine the resource constraints a solution must meet. Determine the basic operations that must be supported.  Quantify the resource constraints for each operation. Select the data structure that best meets these requirements. http://ecomputernotes.com
Selecting a Data Structure Select a data structure as follows: Analyze the problem to determine the resource constraints a solution must meet. Determine the basic operations that must be supported.  Quantify the resource constraints for each operation. Select the data structure that best meets these requirements. http://ecomputernotes.com
Some Questions to Ask Are all data inserted into the data structure at the beginning, or are insertions interspersed with other operations? Can data be deleted? Are all data processed in some well-defined order, or is random access allowed? http://ecomputernotes.com
Some Questions to Ask Are all data inserted into the data structure at the beginning, or are insertions interspersed with other operations? Can data be deleted? Are all data processed in some well-defined order, or is random access allowed? http://ecomputernotes.com
Some Questions to Ask Are all data inserted into the data structure at the beginning, or are insertions interspersed with other operations? Can data be deleted? Are all data processed in some well-defined order, or is random access allowed? http://ecomputernotes.com
Data Structure Philosophy Each data structure has costs and benefits. Rarely is one data structure better than another in all situations. A data structure requires: space for each data item it stores, time to perform each basic operation, programming effort. http://ecomputernotes.com
Data Structure Philosophy Each data structure has costs and benefits. Rarely is one data structure better than another in all situations. A data structure requires: space for each data item it stores, time to perform each basic operation, programming effort. http://ecomputernotes.com
Data Structure Philosophy Each data structure has costs and benefits. Rarely is one data structure better than another in all situations. A data structure requires: space for each data item it stores, time to perform each basic operation, programming effort. http://ecomputernotes.com
Goals of this Course Reinforce the concept that costs and benefits exist for every data structure. Learn the commonly used data structures. These form a programmer's basic data structure “toolkit.” Understand how to measure the cost of a data structure or program. These techniques also allow you to judge the merits of new data structures that you or others might invent. http://ecomputernotes.com
Goals of this Course Reinforce the concept that costs and benefits exist for every data structure. Learn the commonly used data structures. These form a programmer's basic data structure “toolkit”. Understand how to measure the cost of a data structure or program. These techniques also allow you to judge the merits of new data structures that you or others might invent. http://ecomputernotes.com
Goals of this Course Reinforce the concept that costs and benefits exist for every data structure. Learn the commonly used data structures. These form a programmer's basic data structure “toolkit”. Understand how to measure the cost of a data structure or program. These techniques also allow you to judge the merits of new data structures that you or others might invent. http://ecomputernotes.com
Arrays Elementary data structure that exists as built-in in most programming languages. main(  int   argc , char**  argv  ) { 	 int  x[6]; 	 int  j; 	 for(j =0; j < 6; j++) 		 x[j ] = 2*j; } http://ecomputernotes.com
Arrays Array declaration:   int  x[6]; An array is collection of cells of the same type. The collection has the name ‘x’. The cells are numbered with consecutive integers. To access a cell, use the array name and an index:           x[0], x[1], x[2], x[3], x[4], x[5] http://ecomputernotes.com
Array Layout x[1] x[2] x[3] x[4] x[5] x[0] Array cells are contiguous in computer memory The memory can be thought of as an array http://ecomputernotes.com
What is Array Name? ‘x’ is an array name but there is no variable x. ‘x’ is not an  lvalue . For example, if we have the code		 int  a, b;then we can write		b = 2;		a = b;		a = 5;But we cannot write		2 = a; http://ecomputernotes.com
What is Array Name? ‘ x’ is an array name but there is no variable x. ‘x’ is not an  lvalue . For example, if we have the code		 int  a, b;then we can write		b = 2;		a = b;		a = 5; But we cannot write		2 = a; http://ecomputernotes.com
What is Array Name? ‘x’ is an array name but there is no variable x. ‘x’ is not an  lvalue . For example, if we have the code		 int  a, b;then we can write		b = 2;		a = b;		a = 5; But we cannot write		2 = a; http://ecomputernotes.com
Array Name ‘x’ is not an  lvalue 	 int  x[6];	 int  n;	x[0] = 5;	x[1] = 2; 	 x = 3;			// not allowed	x = a + b;		// not allowed	x = &n;		// not allowed http://ecomputernotes.com
Array Name ‘x’ is not an  lvalue 	 int  x[6];	 int  n;	x[0] = 5;	x[1] = 2; 	x = 3;			// not allowed	x = a + b;		// not allowed	x = &n;		// not allowed http://ecomputernotes.com
Dynamic Arrays You would like to use an array data structure but you do not know the size of the array at compile time. You find out when the program executes that you need an integer array of size n=20. Allocate an array using the new operator: int * y = new int[20];  // or  int * y = new  int[n ]y[0] = 10;y[1] = 15;		// use is the same http://ecomputernotes.com
Dynamic Arrays You would like to use an array data structure but you do not know the size of the array at compile time. You find out when the program executes that you need an integer array of size n=20. Allocate an array using the new operator: int * y = new int[20];  // or  int * y = new  int[n ]y[0] = 10;y[1] = 15;		// use is the same http://ecomputernotes.com
Dynamic Arrays You would like to use an array data structure but you do not know the size of the array at compile time. You find out when the program executes that you need an integer array of size n=20. Allocate an array using the new operator: int* y = new int[20];  // or int* y = new int[n] y[0] = 10; y[1] = 15; // use is the same http://ecomputernotes.com
Dynamic Arrays ‘ y’ is a lvalue; it is a pointer that holds the address of 20 consecutive cells in memory. It can be assigned a value. The new operator returns as address that is stored in y. We can write: y = &x[0]; y = x; // x can appear on the right // y gets the address of the // first cell of the x array http://ecomputernotes.com
Dynamic Arrays ‘ y’ is a lvalue; it is a pointer that holds the address of 20 consecutive cells in memory. It can be assigned a value. The new operator returns as address that is stored in y. We can write: y = &x[0]; y = x; // x can appear on the right // y gets the address of the // first cell of the x array http://ecomputernotes.com
Dynamic Arrays ‘ y’ is a lvalue; it is a pointer that holds the address of 20 consecutive cells in memory. It can be assigned a value. The new operator returns as address that is stored in y. We can write: y = &x[0]; y = x; // x can appear on the right // y gets the address of the // first cell of the x array http://ecomputernotes.com
Dynamic Arrays We must free the memory we got using the new operator once we are done with the  y  array. delete[ ] y; We would not do this to the  x  array because we did not use new to create it. http://ecomputernotes.com
The LIST Data Structure The List is among the most generic of data structures. Real life:  shopping list,  groceries list,  list of people to invite to dinner List of presents to get http://ecomputernotes.com
Lists A list is collection of items that are all of the same type (grocery items, integers, names) The items, or elements of the list, are stored in some particular order It is possible to insert new elements into various positions in the list and remove any element of the list http://ecomputernotes.com
Lists A list is collection of items that are all of the same type (grocery items, integers, names) The items, or elements of the list, are stored in some particular order It is possible to insert new elements into various positions in the list and remove any element of the list http://ecomputernotes.com
Lists A list is collection of items that are all of the same type (grocery items, integers, names) The items, or elements of the list, are stored in some particular order It is possible to insert new elements into various positions in the list and remove any element of the list http://ecomputernotes.com
Lists List is a set of elements in a linear order.  For example, data values a 1 , a 2 , a 3 , a 4  can be arranged in a list: (a 3 , a 1 , a 2 , a 4 ) In this list, a 3 , is the first element, a 1  is the second element, and so on The order is important here; this is not just a random collection of elements, it is an  ordered  collection http://ecomputernotes.com
Lists List is a set of elements in a linear order.  For example, data values a 1 , a 2 , a 3 , a 4  can be arranged in a list: (a 3 , a 1 , a 2 , a 4 ) In this list, a 3 , is the first element, a 1  is the second element, and so on The order is important here; this is not just a random collection of elements, it is an  ordered  collection http://ecomputernotes.com
List Operations Useful operations createList(): create a new list (presumably empty) copy(): set one list to be a copy of another clear(); clear a list (remove all elments) insert(X, ?): Insert element X at a particular position    in the list remove(?): Remove element at some position in    the list get(?): Get element at a given position update(X, ?): replace the element at a given position    with X find(X): determine if the element X is in the list length(): return the length of the list. http://ecomputernotes.com
List Operations We need to decide what is meant by “particular position”; we have used “?” for this. There are two possibilities: Use the actual index of element: insert after element 3, get element number 6. This approach is taken by arrays Use a “current” marker or pointer to refer to a particular position in the list. http://ecomputernotes.com
List Operations We need to decide what is meant by “particular position”; we have used “?” for this. There are two possibilities: Use the actual index of element: insert after element 3, get element number 6. This approach is taken by arrays Use a “current” marker or pointer to refer to a particular position in the list. http://ecomputernotes.com
List Operations If we use the “current” marker, the following four methods would be useful: start() : moves to “current” pointer to the very first   element. tail() : moves to “current” pointer to the very last  element. next (): move the current position forward one  element back (): move the current position backward one  element http://ecomputernotes.com

More Related Content

PPT
computer notes - Data Structures - 1
PPT
Data structures cs301 power point slides lecture 01
PPT
Data Structure Lec #1
PDF
Jan vitek distributedrandomforest_5-2-2013
PPTX
Interacting with Accumulo and Graphulo using Julia/Python D4M
PDF
Random forest using apache mahout
PPTX
Data Science Academy Student Demo day--Michael blecher,the importance of clea...
computer notes - Data Structures - 1
Data structures cs301 power point slides lecture 01
Data Structure Lec #1
Jan vitek distributedrandomforest_5-2-2013
Interacting with Accumulo and Graphulo using Julia/Python D4M
Random forest using apache mahout
Data Science Academy Student Demo day--Michael blecher,the importance of clea...

What's hot (20)

DOC
Sas basis imp intrw ques
PDF
Reproducible, Open Data Science in the Life Sciences
PPT
Chapter 1( intro &amp; overview)
PPTX
weka data mining
PPT
Abstract data types
PPTX
4. R- files Reading and Writing
PPTX
Unit 2 - Data Manipulation with R.pptx
PPTX
Weka library, JAVA
PDF
Building Random Forest at Scale
PPS
Data Structure
PPT
Lecture 1 data structures and algorithms
PPTX
Association Rule Mining Using WEKA
PPTX
Exploratory data analysis with Python
PDF
PDF
Tutorial for Estimating Broad and Narrow Sense Heritability using R
PPT
Logistic Regression using Mahout
PPT
data mining with weka application
PDF
Lecture-05-DSA
PPTX
Triplestore and SPARQL
Sas basis imp intrw ques
Reproducible, Open Data Science in the Life Sciences
Chapter 1( intro &amp; overview)
weka data mining
Abstract data types
4. R- files Reading and Writing
Unit 2 - Data Manipulation with R.pptx
Weka library, JAVA
Building Random Forest at Scale
Data Structure
Lecture 1 data structures and algorithms
Association Rule Mining Using WEKA
Exploratory data analysis with Python
Tutorial for Estimating Broad and Narrow Sense Heritability using R
Logistic Regression using Mahout
data mining with weka application
Lecture-05-DSA
Triplestore and SPARQL
Ad

Similar to Computer notes - data structures (20)

PPT
CS301-lec01.ppt
PPT
Data structure and algorithm with java by shikra
DOCX
Data structure and algorithm.
PDF
DSA 1- Introduction.pdf
PPT
Lec1
PPTX
Introduction to Data Structures, Data Structures using C.pptx
PPTX
PPTX
EE-232-LEC-01 Data_structures.pptx
PDF
Chapter 1 Introduction to Data Structures and Algorithms.pdf
PDF
This is the official tutorial from Oracle.httpdocs.oracle.comj.pdf
PDF
Data structures and algorithms short note (version 14).pd
PPTX
DSA(Lec-1,2,3) For C++ Introduction for basics
PPTX
DS-UNIT 1 FINAL (2).pptx
PPTX
Data Structures_Introduction
PDF
PDF
Analysis using r
PDF
EXECUTION OF ASSOCIATION RULE MINING WITH DATA GRIDS IN WEKA 3.8
PPTX
Chapter 1 Data structure _Algorithms.pptx
PPT
Accurately and Reliably Extracting Data from the Web:
CS301-lec01.ppt
Data structure and algorithm with java by shikra
Data structure and algorithm.
DSA 1- Introduction.pdf
Lec1
Introduction to Data Structures, Data Structures using C.pptx
EE-232-LEC-01 Data_structures.pptx
Chapter 1 Introduction to Data Structures and Algorithms.pdf
This is the official tutorial from Oracle.httpdocs.oracle.comj.pdf
Data structures and algorithms short note (version 14).pd
DSA(Lec-1,2,3) For C++ Introduction for basics
DS-UNIT 1 FINAL (2).pptx
Data Structures_Introduction
Analysis using r
EXECUTION OF ASSOCIATION RULE MINING WITH DATA GRIDS IN WEKA 3.8
Chapter 1 Data structure _Algorithms.pptx
Accurately and Reliably Extracting Data from the Web:
Ad

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 15
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 13
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 36
computer notes - Data Structures - 30
computer notes - Data Structures - 39
computer notes - Data Structures - 11
computer notes - Data Structures - 20
computer notes - Data Structures - 15
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 28
computer notes - Data Structures - 19
computer notes - Data Structures - 31
computer notes - Data Structures - 4
computer notes - Data Structures - 13
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 16
computer notes - Data Structures - 22
computer notes - Data Structures - 35
computer notes - Data Structures - 36

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Basic Mud Logging Guide for educational purpose
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Cell Structure & Organelles in detailed.
PDF
Insiders guide to clinical Medicine.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
01-Introduction-to-Information-Management.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
Module 4: Burden of Disease Tutorial Slides S2 2025
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Basic Mud Logging Guide for educational purpose
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Renaissance Architecture: A Journey from Faith to Humanism
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPH.pptx obstetrics and gynecology in nursing
Sports Quiz easy sports quiz sports quiz
Microbial disease of the cardiovascular and lymphatic systems
O5-L3 Freight Transport Ops (International) V1.pdf
Pre independence Education in Inndia.pdf
GDM (1) (1).pptx small presentation for students
Cell Structure & Organelles in detailed.
Insiders guide to clinical Medicine.pdf
O7-L3 Supply Chain Operations - ICLT Program
01-Introduction-to-Information-Management.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Complications of Minimal Access Surgery at WLH

Computer notes - data structures

  • 1. Class No 1 Data Structures http://ecomputernotes.com
  • 2. Data Structures Prepares the students for (and is a prerequisite for) the more advanced material students will encounter in later courses. Cover well-known data structures such as dynamic arrays, linked lists, stacks, queues, tree and graphs. Implement data structures in C++ http://ecomputernotes.com
  • 3. Data Structures Prepares the students for (and is a prerequisite for) the more advanced material students will encounter in later courses. Cover well-known data structures such as dynamic arrays, linked lists, stacks, queues, tree and graphs. Implement data structures in C++ http://ecomputernotes.com
  • 4. Data Structures Prepares the students for (and is a prerequisite for) the more advanced material students will encounter in later courses. Cover well-known data structures such as dynamic arrays, linked lists, stacks, queues, tree and graphs. Implement data structures in C++ http://ecomputernotes.com
  • 5. Need for Data Structures Data structures organize data  more efficient programs. More powerful computers  more complex applications. More complex applications demand more calculations. http://ecomputernotes.com
  • 6. Need for Data Structures Data structures organize data  more efficient programs. More powerful computers  more complex applications. More complex applications demand more calculations. http://ecomputernotes.com
  • 7. Need for Data Structures Data structures organize data  more efficient programs. More powerful computers  more complex applications. More complex applications demand more calculations. http://ecomputernotes.com
  • 8. Organizing Data Any organization for a collection of records that can be searched, processed in any order, or modified. The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days. http://ecomputernotes.com
  • 9. Organizing Data Any organization for a collection of records that can be searched, processed in any order, or modified. The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days. http://ecomputernotes.com
  • 10. Efficiency A solution is said to be efficient if it solves the problem within its resource constraints . Space Time The cost of a solution is the amount of resources that the solution consumes. http://ecomputernotes.com
  • 11. Efficiency A solution is said to be efficient if it solves the problem within its resource constraints. Space Time The cost of a solution is the amount of resources that the solution consumes. http://ecomputernotes.com
  • 12. Selecting a Data Structure Select a data structure as follows: Analyze the problem to determine the resource constraints a solution must meet. Determine the basic operations that must be supported. Quantify the resource constraints for each operation. Select the data structure that best meets these requirements. http://ecomputernotes.com
  • 13. Selecting a Data Structure Select a data structure as follows: Analyze the problem to determine the resource constraints a solution must meet. Determine the basic operations that must be supported. Quantify the resource constraints for each operation. Select the data structure that best meets these requirements. http://ecomputernotes.com
  • 14. Selecting a Data Structure Select a data structure as follows: Analyze the problem to determine the resource constraints a solution must meet. Determine the basic operations that must be supported. Quantify the resource constraints for each operation. Select the data structure that best meets these requirements. http://ecomputernotes.com
  • 15. Some Questions to Ask Are all data inserted into the data structure at the beginning, or are insertions interspersed with other operations? Can data be deleted? Are all data processed in some well-defined order, or is random access allowed? http://ecomputernotes.com
  • 16. Some Questions to Ask Are all data inserted into the data structure at the beginning, or are insertions interspersed with other operations? Can data be deleted? Are all data processed in some well-defined order, or is random access allowed? http://ecomputernotes.com
  • 17. Some Questions to Ask Are all data inserted into the data structure at the beginning, or are insertions interspersed with other operations? Can data be deleted? Are all data processed in some well-defined order, or is random access allowed? http://ecomputernotes.com
  • 18. Data Structure Philosophy Each data structure has costs and benefits. Rarely is one data structure better than another in all situations. A data structure requires: space for each data item it stores, time to perform each basic operation, programming effort. http://ecomputernotes.com
  • 19. Data Structure Philosophy Each data structure has costs and benefits. Rarely is one data structure better than another in all situations. A data structure requires: space for each data item it stores, time to perform each basic operation, programming effort. http://ecomputernotes.com
  • 20. Data Structure Philosophy Each data structure has costs and benefits. Rarely is one data structure better than another in all situations. A data structure requires: space for each data item it stores, time to perform each basic operation, programming effort. http://ecomputernotes.com
  • 21. Goals of this Course Reinforce the concept that costs and benefits exist for every data structure. Learn the commonly used data structures. These form a programmer's basic data structure “toolkit.” Understand how to measure the cost of a data structure or program. These techniques also allow you to judge the merits of new data structures that you or others might invent. http://ecomputernotes.com
  • 22. Goals of this Course Reinforce the concept that costs and benefits exist for every data structure. Learn the commonly used data structures. These form a programmer's basic data structure “toolkit”. Understand how to measure the cost of a data structure or program. These techniques also allow you to judge the merits of new data structures that you or others might invent. http://ecomputernotes.com
  • 23. Goals of this Course Reinforce the concept that costs and benefits exist for every data structure. Learn the commonly used data structures. These form a programmer's basic data structure “toolkit”. Understand how to measure the cost of a data structure or program. These techniques also allow you to judge the merits of new data structures that you or others might invent. http://ecomputernotes.com
  • 24. Arrays Elementary data structure that exists as built-in in most programming languages. main( int argc , char** argv ) { int x[6]; int j; for(j =0; j < 6; j++) x[j ] = 2*j; } http://ecomputernotes.com
  • 25. Arrays Array declaration: int x[6]; An array is collection of cells of the same type. The collection has the name ‘x’. The cells are numbered with consecutive integers. To access a cell, use the array name and an index: x[0], x[1], x[2], x[3], x[4], x[5] http://ecomputernotes.com
  • 26. Array Layout x[1] x[2] x[3] x[4] x[5] x[0] Array cells are contiguous in computer memory The memory can be thought of as an array http://ecomputernotes.com
  • 27. What is Array Name? ‘x’ is an array name but there is no variable x. ‘x’ is not an lvalue . For example, if we have the code int a, b;then we can write b = 2; a = b; a = 5;But we cannot write 2 = a; http://ecomputernotes.com
  • 28. What is Array Name? ‘ x’ is an array name but there is no variable x. ‘x’ is not an lvalue . For example, if we have the code int a, b;then we can write b = 2; a = b; a = 5; But we cannot write 2 = a; http://ecomputernotes.com
  • 29. What is Array Name? ‘x’ is an array name but there is no variable x. ‘x’ is not an lvalue . For example, if we have the code int a, b;then we can write b = 2; a = b; a = 5; But we cannot write 2 = a; http://ecomputernotes.com
  • 30. Array Name ‘x’ is not an lvalue int x[6]; int n; x[0] = 5; x[1] = 2; x = 3; // not allowed x = a + b; // not allowed x = &n; // not allowed http://ecomputernotes.com
  • 31. Array Name ‘x’ is not an lvalue int x[6]; int n; x[0] = 5; x[1] = 2; x = 3; // not allowed x = a + b; // not allowed x = &n; // not allowed http://ecomputernotes.com
  • 32. Dynamic Arrays You would like to use an array data structure but you do not know the size of the array at compile time. You find out when the program executes that you need an integer array of size n=20. Allocate an array using the new operator: int * y = new int[20]; // or int * y = new int[n ]y[0] = 10;y[1] = 15; // use is the same http://ecomputernotes.com
  • 33. Dynamic Arrays You would like to use an array data structure but you do not know the size of the array at compile time. You find out when the program executes that you need an integer array of size n=20. Allocate an array using the new operator: int * y = new int[20]; // or int * y = new int[n ]y[0] = 10;y[1] = 15; // use is the same http://ecomputernotes.com
  • 34. Dynamic Arrays You would like to use an array data structure but you do not know the size of the array at compile time. You find out when the program executes that you need an integer array of size n=20. Allocate an array using the new operator: int* y = new int[20]; // or int* y = new int[n] y[0] = 10; y[1] = 15; // use is the same http://ecomputernotes.com
  • 35. Dynamic Arrays ‘ y’ is a lvalue; it is a pointer that holds the address of 20 consecutive cells in memory. It can be assigned a value. The new operator returns as address that is stored in y. We can write: y = &x[0]; y = x; // x can appear on the right // y gets the address of the // first cell of the x array http://ecomputernotes.com
  • 36. Dynamic Arrays ‘ y’ is a lvalue; it is a pointer that holds the address of 20 consecutive cells in memory. It can be assigned a value. The new operator returns as address that is stored in y. We can write: y = &x[0]; y = x; // x can appear on the right // y gets the address of the // first cell of the x array http://ecomputernotes.com
  • 37. Dynamic Arrays ‘ y’ is a lvalue; it is a pointer that holds the address of 20 consecutive cells in memory. It can be assigned a value. The new operator returns as address that is stored in y. We can write: y = &x[0]; y = x; // x can appear on the right // y gets the address of the // first cell of the x array http://ecomputernotes.com
  • 38. Dynamic Arrays We must free the memory we got using the new operator once we are done with the y array. delete[ ] y; We would not do this to the x array because we did not use new to create it. http://ecomputernotes.com
  • 39. The LIST Data Structure The List is among the most generic of data structures. Real life: shopping list, groceries list, list of people to invite to dinner List of presents to get http://ecomputernotes.com
  • 40. Lists A list is collection of items that are all of the same type (grocery items, integers, names) The items, or elements of the list, are stored in some particular order It is possible to insert new elements into various positions in the list and remove any element of the list http://ecomputernotes.com
  • 41. Lists A list is collection of items that are all of the same type (grocery items, integers, names) The items, or elements of the list, are stored in some particular order It is possible to insert new elements into various positions in the list and remove any element of the list http://ecomputernotes.com
  • 42. Lists A list is collection of items that are all of the same type (grocery items, integers, names) The items, or elements of the list, are stored in some particular order It is possible to insert new elements into various positions in the list and remove any element of the list http://ecomputernotes.com
  • 43. Lists List is a set of elements in a linear order. For example, data values a 1 , a 2 , a 3 , a 4 can be arranged in a list: (a 3 , a 1 , a 2 , a 4 ) In this list, a 3 , is the first element, a 1 is the second element, and so on The order is important here; this is not just a random collection of elements, it is an ordered collection http://ecomputernotes.com
  • 44. Lists List is a set of elements in a linear order. For example, data values a 1 , a 2 , a 3 , a 4 can be arranged in a list: (a 3 , a 1 , a 2 , a 4 ) In this list, a 3 , is the first element, a 1 is the second element, and so on The order is important here; this is not just a random collection of elements, it is an ordered collection http://ecomputernotes.com
  • 45. List Operations Useful operations createList(): create a new list (presumably empty) copy(): set one list to be a copy of another clear(); clear a list (remove all elments) insert(X, ?): Insert element X at a particular position in the list remove(?): Remove element at some position in the list get(?): Get element at a given position update(X, ?): replace the element at a given position with X find(X): determine if the element X is in the list length(): return the length of the list. http://ecomputernotes.com
  • 46. List Operations We need to decide what is meant by “particular position”; we have used “?” for this. There are two possibilities: Use the actual index of element: insert after element 3, get element number 6. This approach is taken by arrays Use a “current” marker or pointer to refer to a particular position in the list. http://ecomputernotes.com
  • 47. List Operations We need to decide what is meant by “particular position”; we have used “?” for this. There are two possibilities: Use the actual index of element: insert after element 3, get element number 6. This approach is taken by arrays Use a “current” marker or pointer to refer to a particular position in the list. http://ecomputernotes.com
  • 48. List Operations If we use the “current” marker, the following four methods would be useful: start() : moves to “current” pointer to the very first element. tail() : moves to “current” pointer to the very last element. next (): move the current position forward one element back (): move the current position backward one element http://ecomputernotes.com