Zhen Jiang West Chester University

Slides:



Advertisements
Similar presentations
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Advertisements

Doubly linked list concept Node structure Insertion sort Insertion sort program with a doubly linked list.
Circular Linked List. COMP104 Circular Linked List / Slide 2 Circular Linked Lists * A Circular Linked List is a special type of Linked List * It supports.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Class template Describing a generic class Instantiating classes that are type-specific version of this generic class Also are called parameterized types.
Introduction to C Programming CE Lecture 20 Insertion and Deletion with Linear Linked Lists.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compiling time * Dynamic Memory.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class List { public: List(); // constructor List(const.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Class Array template The array class defined in last week manipulate array of integer If we need to define class of array for float, double data type,
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class list { public: list(); // constructor list(const.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Reference: Vinu V Das, Principles of Data Structures using C and C++
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5: Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
  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.
Programming Circular Linked List.
What is a Queue? Queue is a linear data structure in which the insertion and deletion operations are performed at two different ends. In a queue data structure,
More on Linked Lists, Stacks, and Queues
Doubly Linked List Review - We are writing this code
Dr. Bernard Chen Ph.D. University of Central Arkansas
Traversing a Linked List
Linked lists.
Engineering Problem Solving with C++, Etter
LinkedList Class.
Binary Search Trees.
Dynamic Memory Allocation Reference Variables
Chapter 16-2 Linked Structures
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Zhen Jiang West Chester University
Chapter 4 Linked Lists.
Random Number Generation
Pointers and dynamic objects
Zhen Jiang West Chester University
Stack and Queues Stack implementation using Array
Stack and Queues Stack implementation using Array
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Programming Abstractions
Pass by Reference.
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
CS1201: Programming Language 2
Dynamic Objects.
Lists.
LAB#3 Stacks Nora Albabtin nora albabtin.
STL Библиотека стандартных шаблонов
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Array-Based Lists & Pointers
Pointers & Functions.
Object-Oriented Programming (OOP) Lecture No. 34
CS-161 Computer Programming Lecture 15 & 16: Arrays II
List as an Abstract Data Type
Pointers and dynamic objects
Linked lists.
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Dynamic Objects.
ICOM 4015 Advanced Programming
Presentation transcript:

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

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

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; }

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;

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

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;

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()

List Concept count 7 countPtr ... NULL

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

List Ptr struct ptr-> node tmp1 tmp2 NULL tmp3

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; }

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

... ... 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

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

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;

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

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; }

Review Only ideas, the programs are all not complete Insertion (p676-678) Deletion (p679) Traversing (i.e., printall, p679-681)