Pointer Review. Node { int value; Node * next; Node( int val =-1, Node * n = NULL) {value = val; next = n;} } Given a ordered linked list pointed to.

Slides:



Advertisements
Similar presentations
Data Structures ADT List
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
C’ POINTERS Basic&Examples. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++));
Dynamic memory allocation
Ceng-112 Data Structures I Chapter 5 Queues.
Data Structures: A Pseudocode Approach with C
Ceng-112 Data Structures I 1 Chapter 3 Linear Lists.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Data Structures (3rd Exam). 1. [5] Determine whether or not the arrays A = [62, 40, 58, 26, 30, 57, 50, 16, 15], B = [56, 38, 55, 46, 16, 53, 48, 39,
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Class 3: Linked Lists. cis 335 Fall 2001 Barry Cohen What is a linked list? n A linked list is an ordered series of ‘nodes’ n Each node contains some.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 C++ Pointers Gordon College. 2 Regular variables Regular variables declared –Memory allocated for value of specified type –Variable name associated.
Lecture 25 Self-Referential Structures Linked Lists
Implementation of Linked List For more notes and topics visit: eITnotes.com.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Lecture 14 Linked Lists 14-1 Richard Gesick. Linked Lists Dynamic data structures can grow and shrink at execution time. A linked list is a linear collection.
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :
Review 1 List Data Structure List operations List Implementation Array Linked List.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Announcements Remember what we talked on Tuesday in terms of Makefiles and phony targets. Don’t lose points for this! BTW, the.PHONY target can appear.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
UNIT – I Linked Lists.
Review Deleting an Element from a Linked List Deletion involves:
CISC181 Introduction to Computer Science Dr
Linked lists.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Dynamic Memory.
Data Structures & Algorithms
Lecture No.02 Data Structures Dr. Sohail Aslam
Linked lists.
Presentation transcript:

Pointer Review

Node { int value; Node * next; Node( int val =-1, Node * n = NULL) {value = val; next = n;} } Given a ordered linked list pointed to by head, write the code to insert an element with value x.

Node { int value; Node * next; Node( int val =-1, Node * n = NULL) {value = val; next = n;} } Given a ordered linked list pointed to by head, write the code to delete an element with value x.

What type is a *a **a

What type is a *a **a Hint: I read the declaration backwards, a is a pointer to a pointer to an int. When I look at *a, I mentally glue the *a together and think int * (*a) *a is a pointer to an int.

Fill in the blanks //find and return the maximum value in an array int maxEntry(int* data, int size) { if ( data == NULL || size <= 0 ) return INT_MIN; int *highSoFar =________________ ; for (int count = ______; count <size;______________ ) { if ( __________________________) highSoFar = __________________; } return _____________________; }

int * p; int * q; What comparison would be true iff p and q have targets with the same value? 1) &p == &q 2) *p == *q 3) p == q

Student * p; Student * q; What comparison would be true iff p and q have the same target? 1) &p == &q 2) *p == *q 3) p == q

int foo = 17; int *ptr = &foo; Which of the following statements will change the value of foo to 18? 1) ptr++; 2) foo++; 3) (*foo)++; 4) (*ptr)++;

Both code fragments below will compile but the one on the right will (on most systems) cause a runtime error. Why? Draw a picture to show what is happening. int x = 5; int *p = new int(x); delete p; int x = 5; int *p = &x; delete p;

const int size = 5; int *a = new int[size]; What code fragment could be inserted in the blank in order to safely initialize each element of A to zero? int* p = &a[0]; for (int i = 0; i< size; i++, p++) { _____________________; } 1) *a = 0; 2) a[i] = 0; 3) *p = 0; 4) *i= 0;

Definitions 1) A dangling pointer is a pointer whose value is the address of memory that the program does not own. 2) A memory leak is when the program owns memory that it can no longer access.

const int size = 5; int *a = new int[size]; What logical error(s) would result if the following statement were executed? a= new int[2*size]; 1) A dangling pointer would result 2) A memory leak would result 3) Both a dangling pointer and a memory leak would result. 4) None of the above

const int size = 5; int *a = new int[size]; int* p = &a[0]; What logical error(s) would result if the following statement were executed: delete [] p; 1) A dangling pointer would result 2) A memory leak would result 3) Both a dangling pointer and a memory leak would result. 4) None of the above

Show the picture after executing p->next = q;

Show the picture after executing q->next = p->next; p->next = NULL;

Show the picture after executing q->next = p->next; q->next->next = p; p->next = NULL;

Show the picture after executing q->next = NULL; delete p;

Show the picture after executing delete (p->next);

Show the picture after executing q->next = head; delete p;

Show the picture after executing delete Head; head = q;

Given the following, explain the effect of the following on the contents of ptr and i: int i = 10,; int *ptr = &i int j = 4; *ptr += j;

Given the following declarations int a[] = {5, 15, 34, 54, 14, 2, 52, 72}; int *p = &a[1]; int *q = &a[5]; What is the value of *(p+3)?

Given the following declarations int a[] = {5, 15, 34, 54, 14, 2, 52, 72}; int *p = &a[1]; int *q = &a[5]; What is the value of *(q-3)?

Given the following declarations int a[] = {5, 15, 34, 54, 14, 2, 52, 72}; int *p = &a[1]; int *q = &a[5]; What is the value of q – p ?

Given the following declarations int a[] = {5, 15, 34, 54, 14, 2, 52, 72}; int *p = &a[1]; int *q = &a[5]; Is the condition p < q true or false?

Given the following declarations int a[] = {5, 15, 34, 54, 14, 2, 52, 72}; int *p = &a[1]; int *q = &a[5]; Is the condition *p < *q true or false?