Presentation is loading. Please wait.

Presentation is loading. Please wait.

Zhen Jiang West Chester University

Similar presentations


Presentation on theme: "Zhen Jiang West Chester University"— Presentation transcript:

1 Zhen Jiang West Chester University zjiang@wcupa.edu
List Zhen Jiang West Chester University

2 Introduction MyArray (another member function: sorting) 7_4.cc
int main() { MyArray a, b(4); cout << "Before copying"<<endl; a.printall(); b.printall(); a.array_copy(b); cout << "After copying"<<endl; a.sorting(); cout << "After sorting of a"<<endl; return 0; } MyArray (another member function: sorting) 7_4.cc

3 MyArray::MyArray() { size=0; ptr=NULL; } MyArray::MyArray(int t_size) if(t_size>0){ size=t_size; ptr=new int[t_size+1]; cout << "Please input "<< t_size<< " intergers:"; for(int i=0;i<t_size;i++) cin >> ptr[i]; else{ MyArray::~MyArray() delete [] ptr; MyArray::MyArray() { } MyArray::MyArray(int t_size) MyArray::~MyArray() // csc142_pointer7_4.cc // copyright zjiang 3/21/03 #include <iostream> class MyArray{ public: MyArray(); MyArray(int); ~MyArray(); void printall(); void array_copy(MyArray&); void sorting(); private: int size; int *ptr; }; int main() { MyArray a, b(4); cout << "Before copying"<<endl; a.printall(); b.printall(); a.array_copy(b); cout << "After copying"<<endl; a.sorting(); cout << "After sorting of a"<<endl; return 0; }

4 void MyArray::printall()
{ } void MyArray::array_copy(MyArray & b) void MyArray::printall() { if(size==0) { cout <<"NULL"<<endl; return; } for(int i=0;i<size;i++) cout<<ptr[i]<<" "; cout <<endl; void MyArray::array_copy(MyArray & b) size=b.size; delete [] ptr; ptr=new int [size]; for(int i=0; i<size;i++) ptr[i]=b.ptr[i]; void MyArray::sorting() { int temp; int pass; int search_index; int min; for(pass=0; pass<size-1; pass++){ min=pass; for(search_index=pass+1; search_index<size; search_index++){ if(ptr[search_index]<ptr[min]) min=search_index; } temp=ptr[min]; ptr[min]=ptr[pass]; ptr[pass]=temp;

5 Introduction Add (push) and delete (pop) 7_5.cc a.add(i); a.pop();
int main() { MyArray a, b(4); int i; cout << "Before copying"<<endl; a.printall(); b.printall(); a.array_copy(b); cout << "After copying"<<endl; a.sorting(); cout << "After sorting of a"<<endl; cout << "Please input an integer "; cin >> i; a.add(i); a.pop(); return 0; } Add (push) and delete (pop) 7_5.cc

6 int pop(); void add(int); // csc142_pointer7_5.cc
MyArray::MyArray() { size=0; ptr=NULL; } MyArray::MyArray(int t_size) if(t_size>0){ size=t_size; ptr=new int[t_size+1]; cout << "Please input "<< t_size<< " intergers:"; for(int i=0;i<t_size;i++) cin >> ptr[i]; else{ size=0; MyArray::~MyArray() delete [] ptr; // csc142_pointer7_5.cc // copyright zjiang 3/21/03 #include <iostream> class MyArray{ public: MyArray(); MyArray(int); ~MyArray(); void printall(); void array_copy(MyArray&); void sorting(); void add(int); int pop(); private: int size; int *ptr; }; void MyArray::printall() { if(size==0) { cout <<"NULL"<<endl; return; } for(int i=0;i<size;i++) cout<<ptr[i]<<" "; cout <<endl; void MyArray::array_copy(MyArray & b) size=b.size; delete [] ptr; ptr=new int [size+1]; for(int i=0; i<size;i++) ptr[i]=b.ptr[i]; ptr[size]=0;

7 void MyArray::add(int a) { int *tmp = new int[size+1], i;
void MyArray::sorting() { int temp; int pass; int search_index; int min; for(pass=0; pass<size-1; pass++){ min=pass; for(search_index=pass+1; search_index<size; search_index++) if(ptr[search_index]<ptr[min]) min=search_index; temp=ptr[min]; ptr[min]=ptr[pass]; ptr[pass]=temp; } void MyArray::add(int a) { int *tmp = new int[size+1], i; for(i=0; i<size; i++) tmp[i]=ptr[i]; tmp[i]=a; delete [] ptr; ptr=tmp; size = size+1; sorting(); } int MyArray::pop() int value = ptr[0]; size--; for(int i =0; i<size;i++) ptr[i]=ptr[i+1]; //Too much? void MyArray::add(int a) { } int MyArray::pop()

8 List Concept count 7 countPtr ... NULL

9 List *ptr ptr[i] or *(ptr+i) ptr-> or ptr-> data Ptr int Ptr Ptr
struct ptr-> or ptr->

10 List Ptr struct ptr-> node tmp1 tmp2 NULL tmp3

11 List Example 1 (insert) 8.cc // csc142_pointer8.cc
// copyright zjiang 3/21/03 #include <iostream> using namespace std; struct NodeType{ int component; NodeType * next; }; class List{ public: List(); void print(); void insert(int); private: NodeType * node; Example 1 (insert) 8.cc List::List() { node=NULL; }

12 NULL NULL void List::print() { NodeType * tmp=node; while(tmp){
cout<<" ("<<tmp->component<<" ) "; tmp=tmp->next; } cout <<endl; tmp tmp NULL tmp node node node NULL

13 ... ... current a NULL current a a+1 NULL pre tmp a-1 current a
void List::insert(int a) { NodeType *current = new NodeType; NodeType *tmp; NodeType *pre=NULL; if(!node || node->component >= a) { current->next = node; current->component = a; node=current; return; } tmp = node; while (tmp && tmp->component < a) pre=tmp; tmp = tmp-> next; pre->next = current; current->next = tmp; current a NULL current a a+1 NULL pre ... tmp a-1 ... current >=a or NULL a

14 List int main() { List list1, list2; list1.insert(-35);
list1.print(); list2.print(); }

15 List Example 2 (pop) 9.cc int isEmpty(); int pop();
// csc142_pointer9.cc // copyright zjiang 3/21/03 #include <iostream> using namespace std; struct NodeType{ int component; NodeType * next; }; class List{ public: List(); void print(); void insert(int); int isEmpty(); int pop(); private: NodeType * node; List Example 2 (pop) 9.cc List::List() { node=NULL; } void List::print() NodeType * tmp=node; while(tmp){ cout<<" ("<<tmp->component<<" ) "; tmp=tmp->next; cout <<endl;

16 List int List::isEmpty() { if(node)return 1; else return 0; }
void List::insert(int a) { NodeType *current = new NodeType; NodeType *tmp; NodeType *pre=NULL; if(!node || node->component >= a) { current->next = node; current->component = a; node=current; return; } tmp = node; while (tmp && tmp->component < a) pre=tmp; tmp = tmp-> next; pre->next = current; current->next = tmp; List int List::isEmpty() { if(node)return 1; else return 0; } int List::pop() int i; // create a new return object Nodetype * tmp = node->next; i=node->component; delete node; node = tmp; return i; tmp node

17 List list2.insert(list1.pop()); int main() { List list1, list2;
cout<<"List 1:"; list1.print(); cout<<"List 2:"; list2.print(); list2.insert(list1.pop()); return 0; }

18 Review Only ideas, the programs are all not complete
Insertion (p ) Deletion (p679) Traversing (i.e., printall, p )


Download ppt "Zhen Jiang West Chester University"

Similar presentations


Ads by Google