Object oriented programming
Download
1 / 43

Object Oriented Programming - PowerPoint PPT Presentation


  • 55 Views
  • Uploaded on

Object Oriented Programming . Chapter 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques may include features such as Data abstraction. Encapsulation. Polymorphism. Inheritance.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Object Oriented Programming ' - ghalib


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  • OOP revolves around the concept of an objects.

  • Objects are created using the class definition.

  • Programming techniques may include features such as

    • Data abstraction.

    • Encapsulation.

    • Polymorphism.

    • Inheritance.

  • Many modern programming languages now support OOP.


Abstract data types
Abstract data types

  • An outline of the program containing its requirements should precede the coding process for a project. Then, in the later stage, the implementation may start with specific data structure.

    • First, specify each task in terms of inputs and outputs.

    • Be concerned with what the program should do. For example, if an item is needed to accomplish some tasks, the item is specified by the operations performed on it not by its structure.

    • When the implementation starts, it decides which data structure to use to make the execution more efficient.


  • An item specified in terms of operations is called an abstract data type.

  • ADT- An abstract data type can be defined by the operations that are done on the data regardless of its type.

  • ADT- A set of data values and associated operations that are precisely specified independent of any particular implementation.

  • ADT- Mathematical description of an object with set of operations on the object


Some information before starting

  • You already learned that addresses of variables can be assigned to pointers.

  • However, pointers can refer to unknown locations that are accessible only through their addresses not by names.

  • These locations must be set by the memory manager dynamically during the run of the program.

  • To dynamically allocate and de-allocate memory, two functions are used:

    • new – takes from memory as much space as needed to store an object.

      • Ex : p = new int;

    • delete – return the space that is accessible from p and is no longer needed.

      • Ex : delete p;

    • Note : an address has to be assigned to a pointer, if it can’t be the address of any location, it should be a null address, which is simply 0.


Linked lists

Linked Lists

Chapter 3


Linked list
Linked list

  • An array is a very useful data structure in programming languages . However, it has at least two limitations:

    • Fixed size: the size has to be known at compile time

    • data is separated in the computer by the same distance. Insertion is a problem (shifting is required)

  • that can be overcome by using linked structure.

    • Linked List (LL):

      is a collection of nodes storing data and links (using addresses) to next nodes.


  • Linked list1
    Linked list

    • A linked list is a data structure that consists of a sequence of data records such that in each record there is a field that contains a reference (i.e., a link) to the next record in the sequence.

    • The most flexible linked structure’s implementation is by using pointers.

    • Linked lists are among the simplest and most common data structures; they provide an easy implementation for several important abstract data structures, including stacks, queues and arrays.


    3 1 singly linked lists
    3.1 Singly Linked Lists

    • Linked list – a data structure that composed of nodes, each node holding some information and a pointer to another node in the list.

    • Singly linked list – a node has a link only to its successor in the sequence of nodes in the list.

    • Note :

      • Only one variable is used to access any node in the list.

      • The last node on the list can be recognized by the null pointer.


    • Example :

      class IntSLLNode

      { public:

      IntSLLNode()

      { next = 0; }

      IntSLLNode(inti, IntSLLNode *ptr = 0)

      { info = i;

      next = ptr; }

      int info;

      IntSLLNode *next;

      };


    Now how to create the linked list
    Now, how to create the linked list

    • Create a new node by executing the declaration and assignment :

      IntSLLNode *p = new IntSLLNode(10);

      • This statement create a node on the list and make p points to it.

      • The constructor assigns the number 10 to theinfomember of this node.

      • The constructor assigns null to its next member.

    • Then any new node is included in the list by making the next member of the first node a pointer to the new node.

    • The second node is created by :

      p -> next = new IntSLLNode (8);


    Where p -> next is the next member of the node pointed to p (Figure 3.1d). As before four steps are executed:

    • New node is created (Figure 3.1e)

    • The constructor assign the number 8 to the info member of this node (Figure 3.1f)

    • The constructor assign null to the next member. (Figure 3.1g).

    • The new node is included in the list by making the next member of the first node a pointer to the node (Figure 3.1h).

      p -> next -> next = new IntSLLNode (50);



    • Problem :

      • The longer the linked list, the longer the chain of nexts to access the nodes at the end of the list when using pointers.

      • In this example p -> next -> next -> next allow us to access the next member of the 3rd node on the list.

        • If you missed a node in the chain, then a wrong assignment is made

        • The flexibility of using linked lists is diminished .


    Figure 3.3 a singly list of integers


    Operations
    Operations :

    • Insert

      • At the Beginning (before Head)

      • At End (after Tail)

      • middle

    • Delete

      • At the Beginning (before Head)

      • At End (after Tail)

      • middle

      • Special cases: Empty List, single-node list

    • Search


    3 1 1 insertion
    3.1.1 Insertion :

    • inserting a new node at the beginning of a singly linked list

    Figure 3.4


    3 1 1 insertion cont
    3.1.1 Insertion : (cont’)

    • inserting a new node at the end of a singly linked list

    Figure 3.5


    Insert At the beginning :

    Insert at the end

    addToTail(int el)

    {

    if (tail != 0)

    { // if list is not empty;

    tail->next = new IntSLLNode(el);

    tail = tail->next; }

    else { // if list is empty;

    head = tail = new IntSLLNode(el); }

    }

    Complexity is O(1)

    addToHead(int el)

    {

    head = new IntSLLNode(el,head);

    if (tail == 0) // if list is empty

    tail = head;

    }

    Complexity is O(1)


    3 1 2 deletion
    3.1.2 Deletion :

    • Deleting a node at the beginning of the list and returning the value stored in it.

    Figure 3.6


    • There are : two special cases to consider :

    • Attempt to remove a node from an empty linked list .

      • One way is to use assert statement( with the function isEmpty() ).

      • Or throw an exception by using throw clause( and having a matching try-catch clause).

    • The list has one node to remove

      • The list becomes empty and tail + head set to null.


    3 1 2 deletion cont
    3.1.2 Deletion : (cont’)

    • Deleting a node from the end of the list and returning the value stored in it.

    • Problem :

      • Tail has to be moved backward by one node?

      • The predecessor has to be found, how?

        • Use a temporary variable tmp that scans the list within for loop.





    Delete from Head same as in

    Delete from Tail

    intIntSLList::deleteFromTail() {

    inte1 = tail->info;

    if (head==tail) {// one node

    delete head;

    head = tail = 0; }

    else {

    IntSLLNode*tmp;

    for (tmp=head; tmp->next != tail; )

    tmp= tmp->next;

    delete tail; tail=tmp;

    tmp->next=0; }

    return e1; }

    Complexity is O(n)

    intIntSLList::deleteFromHead() {

    inte1 = head->info;

    IntSLLNode*tmp= head;

    if (head==tail) // 1node only

    head = tail = 0;

    else head = head->next;

    delete tmp;

    return e1; }

    Complexity is O(1)


    Finding and deleting a node with certain integer
    Finding and deleting a node with certain integer: same as in

    • In this case, a node is removed from the list by linking its predecessor to its successor. So,

      • Either scan the list and then scan it again to find the node’s predecessor.

      • Or use two pointers variable(ex, pred and tmp) using for loop so that they point to first and second nodes of the list.


    Figure 3.8 same as in


    Complexity of deleting a node with certain integer
    Complexity of deleting same as in a node with certain integer:

    • Best Case : O(1)

    • Worst Case: O(1)

    • Average Case:


    Other deletion cases
    Other deletion cases : same as in

    • Remove a node from an empty list – function is exited.

    • Delete a node from a one-node linked list -head =tail =null.

    • Remove the first node of the list – updating head.

    • Remove the last node of the list – updating tail.

    • Delete a node with a number that is not in the list – do nothing .

    • Last , the one we discussed, delete a node with a number that is in the list


    3 1 3 search
    3.1.3 Search same as in

    • The searching operation does not modify linked lists, it just scans the list to find a number.

    If tmp is not null, el was found and true is returned .

    If tmp is null, the search was unsuccessful and false is returned.


    3 2 doubly linked lists dll
    3.2 Doubly Linked Lists (DLL) same as in

    • To avoid singly linked list problem, the linked list is redefined so that each node in the list has twopointers, one to the successor and one to the predecessor. This list is called a doubly linked list .

    Figure 3.9


    • Insert a node to the end of Doubly linked list : same as in

      • A new node is created, and then its three data members are initialized :

        • info member to a number,

        • next member to null,

        • prevmember to value of tail

      • tail is set to point to the new node

      • The next member of the predecessor is set to point to the new node

        Complexity is O(1)


    Figure 3.11 same as in


    • Delete a same as in node to the end of Doubly linked list:

      • A temporary variable (ex. el) is set to the value in the node.

      • tail is set to its predecessor.

      • The last node is deleted.

      • The next member of the tail node is set to null.

      • Return the copy of the object stored in the removed node.

        Complexity is O(1)

      • Note : before deleting, check if the list is empty.


    el = 10; same as in

    Figure 3.12


    3 3 circular lists cll
    3.3 Circular Lists (CLL) same as in

    • A list where nodes form a ring ; each node has a successor.

    • For example:

      • Several processes are using the same resources for the same amount of time, and each process need a fair share of the resources.

    Figure 3.13


    Complexity is O(1)


    Insert a node at the front of CSLL though operations on the list require access to the tail and its successor, the head.

    Insert a node at the end of CSLL

    Figure 3.14


  • A circular doubly linked list can avoid this problems in deletion and insertion to circular singly linked list and complexity becomes O(1)

  • Figure 3.15


    Complexity Analysis Tail -

    Complete the missing cells


    ad