linked lists definition struct Node int data Node

  • Slides: 16
Download presentation
linked lists: definition struct Node{ int data; Node* next; }; typedef Node* Node. Ptr;

linked lists: definition struct Node{ int data; Node* next; }; typedef Node* Node. Ptr; Node. Ptr head; bool empty(Node. Ptr head) { … } int get. Head(Node. Ptr head) { … } Node. Ptr get. Rest(Node. Ptr head) { … } Node. Ptr add. Head(Node. Ptr head, int newdata){ … } void del. Head(Node. Ptr& Head){ … } Unorganized ‘list’ …

Usage: void main(){ Node. Ptr Head 1=NULL, Head 2 = NULL, Head; add. Head(Head

Usage: void main(){ Node. Ptr Head 1=NULL, Head 2 = NULL, Head; add. Head(Head 1, 50); add. Head(Head 1, 40); add. Head(Head 1, 30); add. Head(Head 1, 20); cout << "List 1: " << endl; Display. List(Head 1); cout << "Length of Head 1 list: " << length(Head 1) << endl; cout << "Recursive length of Head 1 list: " << length. Rec(Head 1) << endl; if(is. Palindrome(Head 1)) cout << "Head 1 list is palindrome" << endl; else cout << "Head 1 list is not palindrome" << endl; add. Head(Head 2, 25); add. Head(Head 2, 35); add. Head(Head 2, 45); add. Head(Head 2, 35); add. Head(Head 2, 25); cout << "List 2: " << endl; Display. List(Head 2); cout << "Length of Head 2 list: " << length(Head 2) << endl; cout << "Recursive length of Head 2 list: " << length. Rec(Head 2) << endl; if(is. Palindrome(Head 2)) cout << "Head 2 list is palindrome" << endl; else cout << "Head 2 list is not palindrome" << endl; Head = merge. Lists(Head 1, Head 2); cout << "Merged List: " << endl; Display. List(Head); cout << "Length of Merged list: " << length(Head) << endl; cout << "Recursive length of Merged list: " << length. Rec(Head) << endl; if(is. Palindrome. Rec(Head)) cout << "Merged list is palindrome" << endl; else cout << "Merged list is not palindrome" << endl; cout << "check the list again: " << endl; Display. List(Head); }

Make It an Abstract Data Type l l One more example of ADT: l

Make It an Abstract Data Type l l One more example of ADT: l integer linked list using class A class with dynamic objects: l Copy constructor l Destructor

struct Node{ public: int data; Node* next; }; It’s all about re-organization, the implementation

struct Node{ public: int data; Node* next; }; It’s all about re-organization, the implementation is easy. class List { public: List(); List(const List& list 1); ~List(); bool empty() const; void add. Head(int newdata); void del. Head(); int head. Element() const; ‘new’ member functions // constructor // copy constructor // destructor // // boolean function add to the head delete the head get the head element // … insert. Node, delete. Node, add. End, delete. End, … int length() const; void print() const; // utility function // output private: Node* head; }; All ‘old’ operations

struct Node{ double data; Node* next; }; More complete list ADT class List {

struct Node{ double data; Node* next; }; More complete list ADT class List { public: List(); // constructor List(const List& list); // copy constructor ~List(); // destructor List& operator=(const List& list); // assignment operator bool empty() const; // boolean function void add. Head(double x); // add to the head double delete. Head(); // delete the head and get the head element // List& rest(); // get the rest of the list with the head removed // double head. Element() const; // get the head element void add. End(double x); double delete. End(); // double end. Element(); // add to the end // delete the end and get the end element // get the element at the end bool search. Node(double x); void insert. Node(double x); void delete. Node(double x); // search for a given x // insert x in a sorted list // delete x in a sorted list … void print() const; int length() const; private: Node* head; }; // output // count the number of elements

How to use it void main(){ List L; L. print(); L. add. Head(30); L.

How to use it void main(){ List L; L. print(); L. add. Head(30); L. print(); L. add. Head(13); L. print(); L. add. Head(40); L. print(); L. add. Head(50); L. print(); List N(L); N. print(); List R; R. print(); if(R. empty()) cout << L. del. Head(); L. print(); if(L. empty()) cout << else{ cout << } } // constructor called automatically here for L { } { 30 } { 13 30 } { 40 13 30 } { 50 40 13 30 } { 30 13 40 50 } { } "List R empty" << endl; { 40 13 30 } { 13 30 } "List L empty" << endl; "List L contains " << L. length() << " nodes" << endl; "Head element of list L is: " << L. head. Element() << endl; // destructor called automatically here for L

Implementation (check the previous linked list slides) Some simple member functions: List: : List(){

Implementation (check the previous linked list slides) Some simple member functions: List: : List(){ head = NULL; } bool List: : empty() const { if(head==NULL) return true; else return false; } Or {return(head==NULL); } int List: : head. Element() const { if(head != NULL) return head->data; else{ cout << "error: trying to find head of empty list" << endl; exit(1); } }

(explicitly defined) copy constructor: List: : List(const List& list 1) { head = NULL;

(explicitly defined) copy constructor: List: : List(const List& list 1) { head = NULL; Nodeptr cur = list 1. head; while(cur != NULL) { // add. End(cur->data); add. Head(cur->data); // inverse list order cur = cur->next; } }

Destructor: deallocation function List: : ~List(){ Nodeptr cur; while(head!=NULL){ cur = head; head =

Destructor: deallocation function List: : ~List(){ Nodeptr cur; while(head!=NULL){ cur = head; head = head->next; delete cur; } }

Adding an element to the head: void List: : add. Head(int newdata){ Nodeptr new.

Adding an element to the head: void List: : add. Head(int newdata){ Nodeptr new. Ptr = new Node; new. Ptr->data = newdata; new. Ptr->next = head; head = new. Ptr; }

Deleting the head: void List: : del. Head(){ if(head != NULL){ Nodeptr cur =

Deleting the head: void List: : del. Head(){ if(head != NULL){ Nodeptr cur = head; head = head->next; delete cur; } }

Print the list: void List: : print() const{ cout << "{"; Nodeptr cur =

Print the list: void List: : print() const{ cout << "{"; Nodeptr cur = head; while(cur != NULL){ cout << cur->data << " "; cur = cur->next; } cout << "}" << endl; }

Computing the number of elements of a given list: int List: : length() const{

Computing the number of elements of a given list: int List: : length() const{ int n=0; Nodeptr cur = head; while(cur != NULL){ n++; cur = cur->next; } return n; }

l One more example of ADT: l Really ‘abstract’, independent of implementation details.

l One more example of ADT: l Really ‘abstract’, independent of implementation details.

‘new’ member functions class List { public: List(); // constructor List(const List& list 1);

‘new’ member functions class List { public: List(); // constructor List(const List& list 1); // copy constructor ~List(); // destructor bool empty() const; void add. Head(int newdata); void del. Head(); int head. Element() const; // // boolean function add to the head delete the head access functions … int length() const; void print() const; // utility function // output ‘old’ operations private: Nodeptr head; private: int* h; int size; };

l l The usage on slide 5 is intact! We only need to redefine

l l The usage on slide 5 is intact! We only need to redefine the member functions.