> count;           cout << "Enter " << count << " numbers \n";     for(i = 0; i < count; i++){         cin >> input[i];     }     // Copy numbers from inputArray to outputArray in     // reverse order         output[i] = input[count-i-1];     }        // Print Reversed array     cout << "Reversed Array\n";         cout << output[i] << " ";                return 0; } Exercise _ Solution"> > count;           cout << "Enter " << count << " numbers \n";     for(i = 0; i < count; i++){         cin >> input[i];     }     // Copy numbers from inputArray to outputArray in     // reverse order         output[i] = input[count-i-1];     }        // Print Reversed array     cout << "Reversed Array\n";         cout << output[i] << " ";                return 0; } Exercise _ Solution">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array-Based Lists & Pointers

Similar presentations


Presentation on theme: "Array-Based Lists & Pointers"— Presentation transcript:

1 Array-Based Lists & Pointers

2 Exercise _ Solution using namespace std; int main(){
#include <iostream> using namespace std;    int main(){     int input[500], output[500], count, i;            cout << "Enter number of elements in array\n";     cin >> count;           cout << "Enter " << count << " numbers \n";     for(i = 0; i < count; i++){         cin >> input[i];     }     // Copy numbers from inputArray to outputArray in     // reverse order         output[i] = input[count-i-1];     }        // Print Reversed array     cout << "Reversed Array\n";         cout << output[i] << " ";                return 0; } Exercise _ Solution

3 Enter number of elements in array 5 Enter 5 numbers 1 2 3 4 5
Output Enter number of elements in array 5 Enter 5 numbers Reversed Array using-loop.html

4 Pointer & Array-Based Lists Introduction
Array-Based Lists Everyone is familiar with the term list. You might have a list consisting of employee data, student data or sales data. One thing common to all lists is that all the elements of a list are of the same type. More formally, we can define a list as follows: List: A collection of elements of the same type. The length of a list is the number of elements in the list.

5 we need the following three variables
The array holding the list elements A variable to store the length of the list (that is, the number of list elements currently in the array) A variable to store the size of the array (that is, the maximum number of elements that can be stored in the array)

6 Following are some of the operations performed on a list:
1. Create the list. The list is initialized to an empty state. 2. Determine whether the list is empty. 3. Determine whether the list is full. 4. Find the size of the list. 5. Destroy, or clear, the list. 6. Determine whether an item is the same as a given list element. 7. Insert an item in the list at the specified location. 8. Remove an item from the list at the specified location. 9. Replace an item at the specified location with another item. 10. Retrieve an item from the list from the specified location Search the list for a given item.

7 Array-Based List functions
listSize -- O(1) listMaxSize -- O(1) insert -- O(1) print -- O(n) insertAt – O(n) retrieveAt -- O(1) removeAt -- O(n) replaceAt -- O(1) clearList -- O(1)

8 Pointer Basics A pointer is a reference to another variable (memory location) in a program Variables are allocated at addresses in computer memory Name of the variable is a reference to that memory address A pointer variable contains a representation of an address of another variable (P is a pointer variable in the following):

9 Pointer Variable Definition
Basic syntax: Type *Name Examples: int *P; /* P is var that can point to an int var */ float *Q; /* Q is a float pointer */ char *R; /* R is a char pointer */

10 Address (&) Operator The address (&) operator can be used in front of any variable object in C -- the result of the operation is the location in memory of the variable Syntax: &VariableReference Examples: int V; int *P; int A[5]; &V - memory location of integer variable V &(A[2]) - memory location of array element 2 in array A &P - memory location of pointer variable P

11 Pointer Variable Initialization/Assignment
NULL - pointer lit constant to non-existent address used to indicate pointer points to nothing Can initialize/assign pointer vars to NULL or use the address (&) op to get address of a variable variable in the address operator must be of the right type for the pointer (an integer pointer points only at integer variables) Examples: int V; int *P = &V;

12 Pointer Sample int A = 3; int B; int *P = &A; int *Q = P; int *R = &B;

13 Array and Pointers Example
float A[6] = {1.0,2.0,1.0,0.5,3.0,2.0}; float *theMin = &(A[0]); float *walker = &(A[1]); while (walker < &(A[6])) { if (*walker < *theMin) theMin = walker; walker = walker + 1; }

14 Declarations Examples
int A A is a int float B [5] B is a 1D array of size 5 of floats int * C C is a pointer to an int char D [6][3] D is a 2D array of size 6,3 of chars int * E [5] E is a 1D array of size 5 of pointers to ints

15 (Array (examples Each of the functions isEmpty, isFull, listSize, and maxListSize contain only one statement, which is either a comparison statement or a statement returning a value. It follows that each of these functions is of O(1).

16 Element Removal ( example)
In operation erase(i), we need to fill the hole left by the removed element by shifting backward the n - i - 1 elements A[i + 1], …, A[n - 1] In the worst case (i = 0), this takes O(n) time A 1 2 n o i A 1 2 n i A 1 2 n i Array Lists © 2010 Goodrich, Tamassia

17 Array-Based Lists (cont’d.)
Template insertAt Data Structures Using C++ 2E

18 Array-Based Lists (cont’d.)
Template insertEnd Data Structures Using C++ 2E

19 Array-Based Lists (cont’d.)
Template replaceAt and template clearList Data Structures Using C++ 2E

20 Array-Based Lists (cont’d.)
Searching for an element Linear search example: determining if 27 is in the list Definition of the function template FIGURE 3-32 List of seven elements Data Structures Using C++ 2E

21 Array-Based Lists (cont’d.)
Removing an element Data Structures Using C++ 2E

22 Array-Based Lists (cont’d.)
TABLE 3-1 Time complexity of list operations Data Structures Using C++ 2E

23 Array-Based Lists void print(){ for (int i = 0; i < length; i++)
#include <iostream> using namespace std; class List{ private: int * list; int length; int maxSize; public: List(int size){ list = new int[size]; length = 0; maxSize = size; } void insertAt(int location, int insertItem){ if (location < 0 || location >= maxSize) cout << "The position of the item to be inserted " << "is out of range" << endl; else if (length >= maxSize) //list is full cerr << "Cannot insert in a full list" << endl; { for (int i = length; i > location; i--) list[i] = list[i - 1]; //move the elements down list[location] = insertItem; //insert the item at the //specified position length++; //increment the length } //end insertAt void print(){ for (int i = 0; i < length; i++) cout << list[i] << " "; cout << endl; } }; void main(){ //cout<<"Hi\n"; List my_list(10); my_list.insertAt(0,15); my_list.insertAt(1,25); my_list.insertAt(2,35); my_list.insertAt(0,5); my_list.print(); system("Pause");

24 cout<< x <<endl; cout<< &x <<endl; int *p;
Example) ) Pointers #include <iostream> using namespace std; void main(){ int x; x = 3; cout<< x <<endl; cout<< &x <<endl; int *p; p = &x;

25 cout <<"--------------------------"<<endl;
cout << x << endl; cout << &x << endl; cout << p << endl; cout << *p << endl; cout << &p << endl; *p = 415;

26 b[0]=15; cout<<endl<<b[0]; cout <<" "<<endl; for(int i=0; i<10; i++) cin>>b[i]; cout<<b[i]<<" "; cout<<endl;


Download ppt "Array-Based Lists & Pointers"

Similar presentations


Ads by Google