linklist1

Embed Size (px)

Citation preview

  • 7/29/2019 linklist1

    1/29

    Linked Lists

  • 7/29/2019 linklist1

    2/29

    Structure for Linked List:

    We have already used a simple node definition:

    struct Node

    {

    int data;Node *link;

    };

  • 7/29/2019 linklist1

    3/29

    Insert at end

    Inserts an element at the end of the list

    void insertend(int x)

    { Node *temp = head;

    while (temp -> link != NULL)

    temp = temp->link;

    Node *newer = new Node;

    newer->data = x;

    newer->link = NULL;

    temp->link = newer;

    } 10 12 14 head

    newer

    16

    temp

  • 7/29/2019 linklist1

    4/29

    Delete a Node after a Node

    Delete an element after the specific node

    int delafter(Node *temp)

    { Node *temp1 = temp->next;

    int x = temp->data;

    temp = temp1->link;

    delete temp1;

    return x;

    }

    10 12 14 head 16

    temp

    temp1

  • 7/29/2019 linklist1

    5/29

    Assignment

    Merge two lists. Delete all occurrences of a number from the list.

  • 7/29/2019 linklist1

    6/29

    Stacks As Singly Linked List

    We can implement a stack with a singly linked list

    The head pointer points to the first node.

    Both insertion and deletion are done from head side.

    We had already done.Stacks

    head

    nodes

    elements

  • 7/29/2019 linklist1

    7/29

    Queue with a Singly Linked List

    We can implement a queue with a singly linked list

    The front element is stored at the first node

    The rear element is stored at the last node

    Stacks

    7

    front

    rear

    nodes

    elements

  • 7/29/2019 linklist1

    8/29

    struct Queue{

    int info;

    Queue *link;

    };

    Queue *front, *rear;rear = front = NULL;

    Queue Operations

    frontrear

  • 7/29/2019 linklist1

    9/29

    bool empty()

    {

    if ( front==NULL)

    return TRUE;

    else

    return FALSE;

    }

    Queue Operations

  • 7/29/2019 linklist1

    10/29

    int remove()

    {

    if (empty()){

    cout link;

    if (front== NULL)

    rear = NULL;delete temp;return x;

    }

    Queue Operations

    front

    rear

    temp642 8

  • 7/29/2019 linklist1

    11/29

    Void insert(int x)

    {

    Queue *temp = new Queue;temp->info=x;temp->link = NULL;

    if (rear== NULL)front = temp;

    elserear->link = temp;

    rear = temp;}

    Queue Operations

    front

    rear

    temp

    642 8 10

  • 7/29/2019 linklist1

    12/29

    We can not reach any of the nodethat precede a node p.

    If a list is traversed the externalpointer is needed to be preserved tobe able to refer the list again.

    Solution is to use a Circular List

    Shortcomings of linked linear list

  • 7/29/2019 linklist1

    13/29

    Circular List

    Circular List: The tail of the list points back to the head

    There is no NULL pointer to end the list.62 84

  • 7/29/2019 linklist1

    14/29

    Circular List

    There is no first and last node in circular list so we establish a first, last

    node by convention First Node

    Last Node Last

    An external pointer is used to point to last node.62 84

  • 7/29/2019 linklist1

    15/29

    Stacks as a Circular List

    Let last be the pointer pointing to the last node in the stackand first node be considered as top of the stack.

    Implementation of Stackstruct Node{

    int info;Node *link;

    };Node *last;last = NULL;

  • 7/29/2019 linklist1

    16/29

    bool empty()

    {

    if (last == NULL)

    return TRUE;else

    return FALSE:

    }

    Stack Operations

  • 7/29/2019 linklist1

    17/29

    void push(int x)

    {

    Node * temp = new Node;

    temp->info = x;

    if (last == NULL)last = temp;

    else

    temp->link = last->link;

    }

    last->link = temp;

    }

    PUSH

    642

    last

    1

    temp

  • 7/29/2019 linklist1

    18/29

    int pop()

    {

    int x; Node *temp;

    if (empty()) {coutlink;x = temp->info;

    if (temp == last)

    last = NULL;

    else

    last->link = temp->link;

    delete temp;return x;

    }

    POP

    642

    last

    1

    temp

  • 7/29/2019 linklist1

    19/29

    void insert(int x)

    {

    int x; Node *temp= new node;

    temp->info = x;

    if (empty())last = temp;

    else

    temp->link = last->link;

    last->link = temp->info;

    last = temp;

    }

    Queue As Circular List

    642

    last

    1

    temp

    8

  • 7/29/2019 linklist1

    20/29

    void delafter(Node *p)

    {

    int x; Node *temp;

    if (p==NULL || p==p->link)

    {coutlink;

    x = p->info;

    p->link = temp->next;

    delete temp;}

    DELAFTER()

    642

    last

    1

    temp

    8

    p

  • 7/29/2019 linklist1

    21/29

    read n;

    read(name);

    while (name != END)

    {insert name on the circular list;

    read(name);

    }

    while (there is more than one node on the list)

    {count through n-1 nodes on the list;

    print the name in the nth node;

    delete nth node

    }Print the name of the only node on the list

    Josephus Problem

  • 7/29/2019 linklist1

    22/29

    List can not be traversed backward.

    A node can not be deleted from it,

    given only a pointer to that node.Solution is to use a Doubly LinkedList

    Shortcomings of circular list

  • 7/29/2019 linklist1

    23/29

    Doubly-Linked Lists

    Each node contains two pointers, one to its successorand one to its predecessor.

    Doubly linked list can be linear or circular.

    88NULL 42 109 NULL

    head

    left info right

    88 42 109

  • 7/29/2019 linklist1

    24/29

    Same basic functions operate on list

    Insertleft()

    Insertright()

    Delete()

    deleteleft()

    deleteright()

    We have

    left(right(p)) =p = right(left(p))

    Doubly-Linked Lists

  • 7/29/2019 linklist1

    25/29

    Doubly link list can be implemented using lists.

    Each node contains left, right pointers and info

    part.

    struct node{

    int info;

    node *left, *right;};

    Implementation of Doubly-

    Linked Lists

  • 7/29/2019 linklist1

    26/29

    Delete

    int Delete(Node *p)

    {

    Node *q, *r; int x;

    if (p==NULL){ cout info;

    q = p->left;

    r = p->right;

    q->right = r;

    r->left = q;

    delete p;

  • 7/29/2019 linklist1

    27/29

    Insertright

    void insertright(Node *p, int x)

    {

    Node *q, *r;

    if (p==NULL)

    { cout info = x;

    r = p->right;

    r->left = q;

    q->right = r;

    q->left = p;

    p->right = q;

    }

  • 7/29/2019 linklist1

    28/29

    Insert on head side

    void insert(Node *head, int x)

    {

    Node *q, *r;

    r = head;q = new node;

    q->info = x;

    q->right = r;

    q->left = NULL;r->left = q;

    head = q;

    }

  • 7/29/2019 linklist1

    29/29

    Deletion on head side

    int insert(Node *head)

    {

    Node *q, *r;

    int x;r = head;

    q= r->right;

    q->left = NULL;

    r->right = NULL;x = r->info;

    head = q;

    delete r;

    }