SlideShare a Scribd company logo

Data structure and algorithm.

Data structure and algorithm in computer science| data structure| dsa | DSA| data structure ppt|

1 of 24
Download to read offline
Course Title: Data Structures and
Algorithms
Course code: CS211
Credit Hours: 4(3+1)
Prerequisite: Introduction to Computer
Programming
Course objectives:
Detailed study of the basic notions of the design of
algorithms and the underlying data structures.
Several measures of complexity are introduced.
Emphasis on the Algorithms development, Testing
complexity, and efficiency of algorithms.
Learning outcomes:
Fundamental algorithms design principles such as
Divide and Conquer, Different sorting and searching
algorithms are introduced and explored through case
studies to demonstrate the design of efficient
solutions to computational problems. On completion
of the module a student should be able to:
 Identify the fundamental strategies in algorithmic
design
 Distinguish which strategy is appropriate to solve
a given problem
 Classify different algorithmic strategies
 Analyze a given algorithm and assess its
efficiency
 Apply techniques of proof by induction to verify
certain properties of algorithms.
 Coursework will enhance a student's algorithm
design and program implementation skills.
Course Outlines :
Introduction to Data Structures and Algorithms;
Complexity Analysis; Arrays; Sorting Algorithms:
Insertion Sort, Selection Sort, Bubble Sort, Shell
Sort, Heap Sort, Quick Sort, Merge Sort, Radix Sort,
Bucket Sort; Linked Lists: Singly Linked Lists,
Doubly Linked Lists, Circular List; Stacks, Queues,
and Priority Queue; Recursion: Function call and
Recursion Implementation, Tail Recursion, Non-tail
Recursion, Indirect Recursion, Nested Recursion,
Backtracking. Trees: Binary Trees, Binary Heap,
Binary Search. Tree Traversal, Insertion, Deletion,
and Balancing a Tree; Heap; B-Tree; Spanning Tree,
Splay Trees; Graphs: Representation, Traversal,
Shortest Path, and Cycle Detection; Isomorphic
Graphs; Graph Traversal Algorithms; Hashing;
Memory Management and Garbage Collection.
LAB Work:
1 Write a program to search an element in a two
dimensional array
2 Using iteration and recursion concepts write
programs for finding the element in the array
using the Binary search method.
3 Write a program to perform following
operations on tables by user defined
functions: Addition, Subtraction,
Multiplication, and Transpose.
4 Write a program using iteration and recursion
concepts for quick sort.
5 Write a program to implement various
operations on strings.
6 Write a program for swapping two numbers
using call by value and call by reference
strategies.
7 Write a program to implement Binary search
tree.
8 Write a program to create a Linked List and
perform operations such as insert, delete,
update and reverse.
9 Write a program to simulate various sorting
and searching algorithms.
10 Write a program to simulate various Graph
traversing techniques.
11 Write a program to simulate various tree
traversal techniques.
12 Implement data structures in C++
Reference Materials (Latest Editions of the
following books):
1. Data Structures and Algorithm Analysis, Mark
Allen Weiss, Florida International University,
Addison-Wesley
2. Algorithms, Robert Sedgewick, Princeton
University Publisher: Addison- Wesley Professional
3. Data Structures: Abstraction and Design Using
Java, Koffman and Wolfgang, Wiley;
4. Data Structures and Algorithms in C++, Adam
Drozdek, Course Technology;
Grading Policy:
1.Assignment /Presentation/ Quizzes /Group
Discussions 15%
2.Attendance (Minimum class attendance 75% of
the total lectures ) 05%
3.Mid Term Examination
30%
4.Final Examination
50%
Data Structure:
Data :
• The collection of raw material or facts and
figures about any object is called data.
• e.g
• Student data
• Name, Age, Rno, Class, Section, Gender ,
Address, Phone no, Blood group,
Data Structure
• Organization of data into computer memory is
called data structure.
• Data structure deals with “How data is organized
in memory”.
• Data may be organized in many different ways:
• The logical or mathematical model of particular
organization of data is called a data structure.
• The choice of a particular data model depends
on two considerations.
• First it must be rich enough in structure to mirror
the actual relationships of the data in real world.
• The structure should be simple enough that one
.can effectively process the data when necessary.
Classification of Data Structure
Ad

Recommended

Data structures cs301 power point slides lecture 01
Data structures   cs301 power point slides lecture 01Data structures   cs301 power point slides lecture 01
Data structures cs301 power point slides lecture 01shaziabibi5
 
Algorithms and Data Structures
Algorithms and Data StructuresAlgorithms and Data Structures
Algorithms and Data Structuressonykhan3
 
Data Structure and Algorithms
Data Structure and AlgorithmsData Structure and Algorithms
Data Structure and Algorithmsiqbalphy1
 

More Related Content

Similar to Data structure and algorithm.

CS301-lec01.ppt
CS301-lec01.pptCS301-lec01.ppt
CS301-lec01.pptomair31
 
DSA Introduction To Data Structure Algorithm Analysis Big O.pptx
DSA Introduction To Data Structure Algorithm Analysis Big O.pptxDSA Introduction To Data Structure Algorithm Analysis Big O.pptx
DSA Introduction To Data Structure Algorithm Analysis Big O.pptxcilasov907
 
Introduction to Data Structures & Algorithms
Introduction to Data Structures & AlgorithmsIntroduction to Data Structures & Algorithms
Introduction to Data Structures & AlgorithmsAfaq Mansoor Khan
 
data structures and its importance
 data structures and its importance  data structures and its importance
data structures and its importance Anaya Zafar
 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsAkhil Kaushik
 
DSA 1- Introduction.pdf
DSA 1- Introduction.pdfDSA 1- Introduction.pdf
DSA 1- Introduction.pdfAliyanAbbas1
 
UNIT_5_Data Wrangling.pptx
UNIT_5_Data Wrangling.pptxUNIT_5_Data Wrangling.pptx
UNIT_5_Data Wrangling.pptxBhagyasriPatel2
 
computer notes - Data Structures - 1
computer notes - Data Structures - 1computer notes - Data Structures - 1
computer notes - Data Structures - 1ecomputernotes
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsRajendran
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptxclassall
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptxclassall
 
b,Sc it data structure.ppt
b,Sc it data structure.pptb,Sc it data structure.ppt
b,Sc it data structure.pptclassall
 
Data Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptxData Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptxEllenGrace9
 

Similar to Data structure and algorithm. (20)

Data Structure Lec #1
Data Structure Lec #1Data Structure Lec #1
Data Structure Lec #1
 
algo 1.ppt
algo 1.pptalgo 1.ppt
algo 1.ppt
 
CS301-lec01.ppt
CS301-lec01.pptCS301-lec01.ppt
CS301-lec01.ppt
 
DSA Introduction To Data Structure Algorithm Analysis Big O.pptx
DSA Introduction To Data Structure Algorithm Analysis Big O.pptxDSA Introduction To Data Structure Algorithm Analysis Big O.pptx
DSA Introduction To Data Structure Algorithm Analysis Big O.pptx
 
Introduction to Data Structures & Algorithms
Introduction to Data Structures & AlgorithmsIntroduction to Data Structures & Algorithms
Introduction to Data Structures & Algorithms
 
Unit 1 dsa
Unit 1 dsaUnit 1 dsa
Unit 1 dsa
 
data structures and its importance
 data structures and its importance  data structures and its importance
data structures and its importance
 
Data structure
Data structureData structure
Data structure
 
RAJAT PROJECT.pptx
RAJAT PROJECT.pptxRAJAT PROJECT.pptx
RAJAT PROJECT.pptx
 
UNIT 1.pptx
UNIT 1.pptxUNIT 1.pptx
UNIT 1.pptx
 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & Algorithms
 
DSA 1- Introduction.pdf
DSA 1- Introduction.pdfDSA 1- Introduction.pdf
DSA 1- Introduction.pdf
 
UNIT_5_Data Wrangling.pptx
UNIT_5_Data Wrangling.pptxUNIT_5_Data Wrangling.pptx
UNIT_5_Data Wrangling.pptx
 
computer notes - Data Structures - 1
computer notes - Data Structures - 1computer notes - Data Structures - 1
computer notes - Data Structures - 1
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notations
 
Lect1.pptx
Lect1.pptxLect1.pptx
Lect1.pptx
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptx
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptx
 
b,Sc it data structure.ppt
b,Sc it data structure.pptb,Sc it data structure.ppt
b,Sc it data structure.ppt
 
Data Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptxData Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptx
 

Recently uploaded

IIBA Adl - Being Effective on Day 1 - Slide Deck.pdf
IIBA Adl - Being Effective on Day 1 - Slide Deck.pdfIIBA Adl - Being Effective on Day 1 - Slide Deck.pdf
IIBA Adl - Being Effective on Day 1 - Slide Deck.pdfAustraliaChapterIIBA
 
ppt penjualan berbasis online omset.pptx
ppt penjualan berbasis online omset.pptxppt penjualan berbasis online omset.pptx
ppt penjualan berbasis online omset.pptxHizkiaJastis
 
data analytics and tools from in2inglobal.pdf
data analytics  and tools from in2inglobal.pdfdata analytics  and tools from in2inglobal.pdf
data analytics and tools from in2inglobal.pdfdigimartfamily
 
Web 3.0 in Data Privacy and Security | Data Privacy |Blockchain Security| Cyb...
Web 3.0 in Data Privacy and Security | Data Privacy |Blockchain Security| Cyb...Web 3.0 in Data Privacy and Security | Data Privacy |Blockchain Security| Cyb...
Web 3.0 in Data Privacy and Security | Data Privacy |Blockchain Security| Cyb...Cyber Security Experts
 
SABARI PRIYAN's self introduction as reference
SABARI PRIYAN's self introduction as referenceSABARI PRIYAN's self introduction as reference
SABARI PRIYAN's self introduction as referencepriyansabari355
 
Big Data - large Scale data (Amazon, FB)
Big Data - large Scale data (Amazon, FB)Big Data - large Scale data (Amazon, FB)
Big Data - large Scale data (Amazon, FB)CUO VEERANAN VEERANAN
 
Industry 4.0 in IoT Transforming the Future.pptx
Industry 4.0 in IoT Transforming the Future.pptxIndustry 4.0 in IoT Transforming the Future.pptx
Industry 4.0 in IoT Transforming the Future.pptxMdRafiqulIslam403212
 
SABARI PRIYAN's self introduction as a reference
SABARI PRIYAN's self introduction as a referenceSABARI PRIYAN's self introduction as a reference
SABARI PRIYAN's self introduction as a referencepriyansabari355
 
AWS Identity and access management for users
AWS Identity and access management for usersAWS Identity and access management for users
AWS Identity and access management for usersStephenEfange3
 
Generative AI Rennes Meetup with OVHcloud - WAICF highlights & how to deploy ...
Generative AI Rennes Meetup with OVHcloud - WAICF highlights & how to deploy ...Generative AI Rennes Meetup with OVHcloud - WAICF highlights & how to deploy ...
Generative AI Rennes Meetup with OVHcloud - WAICF highlights & how to deploy ...Thibaud Le Douarin
 
Lies and Myths in InfoSec - 2023 Usenix Enigma
Lies and Myths in InfoSec - 2023 Usenix EnigmaLies and Myths in InfoSec - 2023 Usenix Enigma
Lies and Myths in InfoSec - 2023 Usenix EnigmaAdrian Sanabria
 
A Gentle Introduction to Text Analysis :)
A Gentle Introduction to Text Analysis :)A Gentle Introduction to Text Analysis :)
A Gentle Introduction to Text Analysis :)UNCResearchHub
 
Tips to Align with Your Salesforce Data Goals
Tips to Align with Your Salesforce Data GoalsTips to Align with Your Salesforce Data Goals
Tips to Align with Your Salesforce Data GoalsDataArchiva
 
Artificial Intelligence and its Impact on Society.pptx
Artificial Intelligence and its Impact on Society.pptxArtificial Intelligence and its Impact on Society.pptx
Artificial Intelligence and its Impact on Society.pptxVighnesh Shashtri
 
Soil Health Policy Map Years 2020 to 2023
Soil Health Policy Map Years 2020 to 2023Soil Health Policy Map Years 2020 to 2023
Soil Health Policy Map Years 2020 to 2023stephizcoolio
 
Operations Data On Mobile - inSis Mobile App - Sample Screens
Operations Data On Mobile - inSis Mobile App - Sample ScreensOperations Data On Mobile - inSis Mobile App - Sample Screens
Operations Data On Mobile - inSis Mobile App - Sample ScreensKondapi V Siva Rama Brahmam
 
What is the value of your Data v3.0.pptx
What is the value of your Data v3.0.pptxWhat is the value of your Data v3.0.pptx
What is the value of your Data v3.0.pptxJose Briones
 

Recently uploaded (18)

IIBA Adl - Being Effective on Day 1 - Slide Deck.pdf
IIBA Adl - Being Effective on Day 1 - Slide Deck.pdfIIBA Adl - Being Effective on Day 1 - Slide Deck.pdf
IIBA Adl - Being Effective on Day 1 - Slide Deck.pdf
 
Electricity Year 2023_updated_22022024.pptx
Electricity Year 2023_updated_22022024.pptxElectricity Year 2023_updated_22022024.pptx
Electricity Year 2023_updated_22022024.pptx
 
ppt penjualan berbasis online omset.pptx
ppt penjualan berbasis online omset.pptxppt penjualan berbasis online omset.pptx
ppt penjualan berbasis online omset.pptx
 
data analytics and tools from in2inglobal.pdf
data analytics  and tools from in2inglobal.pdfdata analytics  and tools from in2inglobal.pdf
data analytics and tools from in2inglobal.pdf
 
Web 3.0 in Data Privacy and Security | Data Privacy |Blockchain Security| Cyb...
Web 3.0 in Data Privacy and Security | Data Privacy |Blockchain Security| Cyb...Web 3.0 in Data Privacy and Security | Data Privacy |Blockchain Security| Cyb...
Web 3.0 in Data Privacy and Security | Data Privacy |Blockchain Security| Cyb...
 
SABARI PRIYAN's self introduction as reference
SABARI PRIYAN's self introduction as referenceSABARI PRIYAN's self introduction as reference
SABARI PRIYAN's self introduction as reference
 
Big Data - large Scale data (Amazon, FB)
Big Data - large Scale data (Amazon, FB)Big Data - large Scale data (Amazon, FB)
Big Data - large Scale data (Amazon, FB)
 
Industry 4.0 in IoT Transforming the Future.pptx
Industry 4.0 in IoT Transforming the Future.pptxIndustry 4.0 in IoT Transforming the Future.pptx
Industry 4.0 in IoT Transforming the Future.pptx
 
SABARI PRIYAN's self introduction as a reference
SABARI PRIYAN's self introduction as a referenceSABARI PRIYAN's self introduction as a reference
SABARI PRIYAN's self introduction as a reference
 
AWS Identity and access management for users
AWS Identity and access management for usersAWS Identity and access management for users
AWS Identity and access management for users
 
Generative AI Rennes Meetup with OVHcloud - WAICF highlights & how to deploy ...
Generative AI Rennes Meetup with OVHcloud - WAICF highlights & how to deploy ...Generative AI Rennes Meetup with OVHcloud - WAICF highlights & how to deploy ...
Generative AI Rennes Meetup with OVHcloud - WAICF highlights & how to deploy ...
 
Lies and Myths in InfoSec - 2023 Usenix Enigma
Lies and Myths in InfoSec - 2023 Usenix EnigmaLies and Myths in InfoSec - 2023 Usenix Enigma
Lies and Myths in InfoSec - 2023 Usenix Enigma
 
A Gentle Introduction to Text Analysis :)
A Gentle Introduction to Text Analysis :)A Gentle Introduction to Text Analysis :)
A Gentle Introduction to Text Analysis :)
 
Tips to Align with Your Salesforce Data Goals
Tips to Align with Your Salesforce Data GoalsTips to Align with Your Salesforce Data Goals
Tips to Align with Your Salesforce Data Goals
 
Artificial Intelligence and its Impact on Society.pptx
Artificial Intelligence and its Impact on Society.pptxArtificial Intelligence and its Impact on Society.pptx
Artificial Intelligence and its Impact on Society.pptx
 
Soil Health Policy Map Years 2020 to 2023
Soil Health Policy Map Years 2020 to 2023Soil Health Policy Map Years 2020 to 2023
Soil Health Policy Map Years 2020 to 2023
 
Operations Data On Mobile - inSis Mobile App - Sample Screens
Operations Data On Mobile - inSis Mobile App - Sample ScreensOperations Data On Mobile - inSis Mobile App - Sample Screens
Operations Data On Mobile - inSis Mobile App - Sample Screens
 
What is the value of your Data v3.0.pptx
What is the value of your Data v3.0.pptxWhat is the value of your Data v3.0.pptx
What is the value of your Data v3.0.pptx
 

Data structure and algorithm.

  • 1. Course Title: Data Structures and Algorithms Course code: CS211 Credit Hours: 4(3+1) Prerequisite: Introduction to Computer Programming Course objectives: Detailed study of the basic notions of the design of algorithms and the underlying data structures. Several measures of complexity are introduced. Emphasis on the Algorithms development, Testing complexity, and efficiency of algorithms. Learning outcomes: Fundamental algorithms design principles such as Divide and Conquer, Different sorting and searching algorithms are introduced and explored through case studies to demonstrate the design of efficient solutions to computational problems. On completion of the module a student should be able to:
  • 2.  Identify the fundamental strategies in algorithmic design  Distinguish which strategy is appropriate to solve a given problem  Classify different algorithmic strategies  Analyze a given algorithm and assess its efficiency  Apply techniques of proof by induction to verify certain properties of algorithms.  Coursework will enhance a student's algorithm design and program implementation skills. Course Outlines : Introduction to Data Structures and Algorithms; Complexity Analysis; Arrays; Sorting Algorithms: Insertion Sort, Selection Sort, Bubble Sort, Shell Sort, Heap Sort, Quick Sort, Merge Sort, Radix Sort, Bucket Sort; Linked Lists: Singly Linked Lists, Doubly Linked Lists, Circular List; Stacks, Queues, and Priority Queue; Recursion: Function call and Recursion Implementation, Tail Recursion, Non-tail Recursion, Indirect Recursion, Nested Recursion,
  • 3. Backtracking. Trees: Binary Trees, Binary Heap, Binary Search. Tree Traversal, Insertion, Deletion, and Balancing a Tree; Heap; B-Tree; Spanning Tree, Splay Trees; Graphs: Representation, Traversal, Shortest Path, and Cycle Detection; Isomorphic Graphs; Graph Traversal Algorithms; Hashing; Memory Management and Garbage Collection. LAB Work: 1 Write a program to search an element in a two dimensional array 2 Using iteration and recursion concepts write programs for finding the element in the array using the Binary search method. 3 Write a program to perform following operations on tables by user defined functions: Addition, Subtraction, Multiplication, and Transpose. 4 Write a program using iteration and recursion concepts for quick sort. 5 Write a program to implement various operations on strings. 6 Write a program for swapping two numbers using call by value and call by reference strategies. 7 Write a program to implement Binary search tree.
  • 4. 8 Write a program to create a Linked List and perform operations such as insert, delete, update and reverse. 9 Write a program to simulate various sorting and searching algorithms. 10 Write a program to simulate various Graph traversing techniques. 11 Write a program to simulate various tree traversal techniques. 12 Implement data structures in C++ Reference Materials (Latest Editions of the following books): 1. Data Structures and Algorithm Analysis, Mark Allen Weiss, Florida International University, Addison-Wesley 2. Algorithms, Robert Sedgewick, Princeton University Publisher: Addison- Wesley Professional 3. Data Structures: Abstraction and Design Using Java, Koffman and Wolfgang, Wiley; 4. Data Structures and Algorithms in C++, Adam Drozdek, Course Technology;
  • 5. Grading Policy: 1.Assignment /Presentation/ Quizzes /Group Discussions 15% 2.Attendance (Minimum class attendance 75% of the total lectures ) 05% 3.Mid Term Examination 30% 4.Final Examination 50% Data Structure: Data : • The collection of raw material or facts and figures about any object is called data. • e.g
  • 6. • Student data • Name, Age, Rno, Class, Section, Gender , Address, Phone no, Blood group, Data Structure • Organization of data into computer memory is called data structure. • Data structure deals with “How data is organized in memory”. • Data may be organized in many different ways: • The logical or mathematical model of particular organization of data is called a data structure. • The choice of a particular data model depends on two considerations. • First it must be rich enough in structure to mirror the actual relationships of the data in real world. • The structure should be simple enough that one .can effectively process the data when necessary. Classification of Data Structure
  • 7. • Linear • Non linear Linear • The organization of data in which each node except first and last have only one proceeding and succeeding node is called linear data structure • • Arrays • Stacks • Queues • Lists, etc Non Linear • When the data is stored in the computer memory in hierarchical way or in non linear form, then it is called non linear data structure • e.g • Trees
  • 8. • Files • Graphs Algorithms: • An algorithm is any well-defined computational procedure that takes some values, or set of values, as input and produces some value, or set of values, as output. An algorithm is thus a sequence of computational steps that transform the input into output. • It can be described in a natural language, pseudocode, a flow- chart, or even a programming language.
  • 9. Criteria/Characteristics of Algorithms • Finiteness • Definiteness - no ambiguity /unambiguous • Input • Output • Effectiveness • Efficient • Concise and Compact • Sequence No. • Correctness Finiteness • An algorithm must terminate after a finite number of steps and further each step must be executable in finite amount of time. • In order to establish a sequence of steps as an algorithm, it should be established that it terminates (in finite number of steps) on all allowed inputs.
  • 10. Definiteness (no ambiguity , clarity ) • Each steps of an algorithm must be precisely defined and it should be unambiguous. Inputs • An algorithm must have input values from a specified set. • An algorithm has zero or more but only finite number of inputs. Output: • An algorithm must produce some values called output. • An algorithm has one or more outputs. • The requirement of at least one output is obviously essential, because, otherwise we cannot know the answer/ solution provided by the algorithm. • The outputs have specific relation to the inputs, where the relation is defined by the algorithm.
  • 11. Effectiveness • Effectiveness is the capability of producing a desired result. • An algorithm should be effective/useful. Efficient • An algorithm should not use unnecessary memory locations. Concise and compact • An algorithm should be concise and compact. Sequence No. • Each step of algorithm must have a sequence number. Correctness • Each step of an algorithm must be correctly defined.
  • 12. Need for Data Structures  Data structures organize data  more efficient programs.  More powerful computers  more complex applications.  More complex applications demand more calculations. 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.
  • 13. Selecting a Data Structure Select a data structure as follows: 1.Analyze the problem to determine the resource constraints a solution must meet. 2.Determine the basic operations that must be supported. Quantify the resource constraints for each operation. 3.Select the data structure that best meets these requirements 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?
  • 14. 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.
  • 15. Arrays:  Elementary data structure that exists as built-in in most programming languages. An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). main( int argc, char** argv ) { int x[6]; int j; for(j=0; j < 6; j++) x[j] = 2*j; }
  • 16.  Array declaration: int x[8];  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] Array Layout Array cells are contiguous in computer memory. The memory can be thought of as an array. X[0] X[1] X[2] X[3]
  • 17. X[4] X[5] X[6] X[7] 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;
  • 18.  But we cannot write 2 = a;  ‘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
  • 19. 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
  • 20.  ‘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  We must free the memory we got using the new operator once we are done with the y array. delete[ ] y; 
  • 21.  We would not do this to the x array because we did not use new to create it. There is a substantial difference between declaring a normal array and allocating dynamic memory for a block of memory using new. The most important difference is that the size of a regular array needs to be a constant expression, and thus its size has to be determined at the moment of designing the program, before it is run, whereas the dynamic memory allocation performed by new allows to assign memory during runtime using any variable value as size.
  • 22. The LIST Data Structure  The List is among the most generic of data structures.  Real life: a.shopping list, b.groceries list, c.list of people to invite to dinner d.List of presents to get  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  List is a set of elements in a linear order. For example, data values a1, a2, a3, a4 can be arranged in a list: (a3, a1, a2, a4)
  • 23. In this list, a3, is the first element, a1 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 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. List Operations
  • 24.  We need to decide what is meant by “particular position”; we have used “?” for this. There are two possibilities: 1.Use the actual index of element: insert after element 3, get element number 6. This approach is taken by arrays 2.Use a “current” marker or pointer to refer to a particular position in the list. 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