LINKED LIST
PRESENTED BY:

                Javeria
             (11-arid-3303)
                 MIT-3
   University Institute of Information
               Technology,
       Rawalpindi (UIIT, UAAR)
INTRODUTION
   Link List is an ordered collection of elements
    called nodes. The nodes are connected by
    pointer.

   Each node has two parts
    1) Data Field – Stores data of the node. Same data
      type
    2) Link Field – Store address of the next node
                    ( i.e. Link to the next node)
                                 NEXT

         NODE :

                      DATA
ARRAY VS. LINK LIST
       Aspect              Array                        Link List

Size             Fixed size.                   Grow and contract
                                                 according to insertions
                                                 and deletions.

Storage          Static: It’s location is      Dynamic: It’s node is
capacity        allocated during compile         located during run time.
                time.
Accessing the    Direct or random access       Sequential access
element           method.                        method.
                 Specify the array index or    Traverse starting from the
                  subscript.                     first node in the list by
                                                 pointer.



                                                                  4
SINGLY LINK LIST
IMPLEMENTATION IN
STACKS:

            A          B            C   top


IMPLEMENTATION IN LINEAR
QUEUE:
     front A            B           C   rear



IMPLEMENTATION IN CIRCULAR QUEUE:

            A          B            C   rear




                DOUBLY LINK LIST

        A                   B             C    rear
LINKED LIST
IMPLEMENTATION

   Stacks
   Linear Queue
   Circular Queue
Functions

◦ We will discuss the following functions in
above three data structures:
    Insertion
    Deletion
    Display
    Search
LINK LIST IMPLEMENTATION
        OF STACKS
Defining the linked list data
structure:
  struct node
  {
       int data;
       node *next;
  };


                     Data   next

                        node
STACK CLASS

class stack
{
 public:
  node * top;
  stack( )
  {               top
      top=NULL;
  }
Push method
void push(int x)
{
    node * ptr=new node;
                           top
    ptr->data=x;
    ptr->next=top;
    top=ptr;
}
Push method
void push(int x)           ptr
{
    node * ptr=new node;
                             top
    ptr->data=x;
    ptr->next=top;
    top=ptr;
}
Push method
void push(int x)               ptr
{                          x
    node * ptr=new node;
                                 top
    ptr->data=x;
    ptr->next=top;
    top=ptr;
}
Push method
void push(int x)               ptr
{                          x
    node * ptr=new node;
                                 top
    ptr->data=x;
    ptr->next=top;
    top=ptr;
}
Push method
void push(int x)               ptr
{                          x
    node * ptr=new node;
                                 top
    ptr->data=x;
    ptr->next=top;
    top=ptr;
}
Push method
void push(int x)
{                              top
                               ptr
    node * ptr=new node;
    ptr->data=x;           x
    ptr->next=top;
    top=ptr;
}
-   All nodes are connected to each other through
    pointers
-   Link of the first node is NULL pointer denoted by X
-   Null pointer indicates the start of the stack list
                               top

                           D


                           C


                           B



                           A
Pop Method
int pop()
{
                               top
    if(top==NULL)
     {
     cout<<"nlist emptyn";   D
     return;
     }
                               C
    else
     {
     int temp=top->data;
     node* ptr=top;            B
     top=top->next;
     delete ptr;
     return temp;              A
     }
}
Pop Method
int pop()
{                              top   temp=D
    if(top==NULL)
     {
     cout<<"nlist emptyn";   D
     return ;
     }
                               C
    else
     {
     int temp=top->data;
     node* ptr=top;            B
     top=top->next;
     delete ptr;
     return temp;              A
     }
}
Pop Method
int pop()
                                   ptr
{                              top       temp=D
    if(top==NULL)
     {
     cout<<"nlist emptyn";   D
     return ;
     }
                               C
    else
     {
     int temp=top->data;
     node* ptr=top;            B
     top=top->next;
     delete ptr;
     return temp;              A
     }
}
Pop Method
int pop()
{                              ptr
                                         temp=D
    if(top==NULL)
     {                         D
     cout<<"nlist emptyn";
     return ;                      top
     }
                               C
    else
     {
     int temp=top->data;
     node* ptr=top;            B
     top=top->next;
     delete ptr;
     return temp;              A
     }
}
Pop Method
int pop()
{                              ptr
                                         temp=D
    if(top==NULL)
     {                         D
     cout<<"nlist emptyn";
     return ;                      top
     }
                               C
    else
     {
     int temp=top->data;
     node* ptr=top;            B
     top=top->next;
     delete ptr;
     return temp;              A
     }
}
Display Method
void display()
{
    node * temp;
                                     top
    temp=top;
    if(top==NULL)
                                 D
    {
        cout<<"stack empty";
        return;                  C
    }
    while(temp!=NULL)
    {                            B
        cout<<temp->data<<" ";
        temp=temp->next;
    }                            A
}
Display Method
void display()
{
    node * temp;                     tem
                                     top
                                     p
    temp=top;
    if(top==NULL)
                                 D
    {
        cout<<"stack empty";
        return;                  C
    }
    while(temp!=NULL)
    {                            B
        cout<<temp->data<<" ";
        temp=temp->next;
    }                            A
}
Display Method
void display()
{
    node * temp;                     tem
                                     top
                                     p
    temp=top;                              display D
    if(top==NULL)
                                 D
    {
        cout<<"stack empty";
        return;                  C
    }
    while(temp!=NULL)
    {                            B
        cout<<temp->data<<" ";
        temp=temp->next;
    }                            A
}
Display Method
void display()
{
    node * temp;
                                     top
    temp=top;
    if(top==NULL)
                                 D
    {
        cout<<"stack empty";          tem
                                      p
        return;                  C
    }
    while(temp!=NULL)
    {                            B
        cout<<temp->data<<" ";
        temp=temp->next;
    }                            A
}
Display Method
void display()
{
    node * temp;
                                     top
    temp=top;
    if(top==NULL)
                                 D
    {
        cout<<"stack empty";          tem
                                      p
        return;                  C          display C
    }
    while(temp!=NULL)
    {                            B
        cout<<temp->data<<" ";
        temp=temp->next;
    }                            A
}
Display Method
void display()
{
    node * temp;
                                     top
    temp=top;
    if(top==NULL)
                                 D
    {
        cout<<"stack empty";
        return;                  C
    }
                                      tem
    while(temp!=NULL)                 p
    {                            B
        cout<<temp->data<<" ";
        temp=temp->next;
    }                            A
}
Display Method
void display()
{
    node * temp;
                                     top
    temp=top;
    if(top==NULL)
                                 D
    {
        cout<<"stack empty";
        return;                  C
    }
                                      tem
    while(temp!=NULL)                 p     display B
    {                            B
        cout<<temp->data<<" ";
        temp=temp->next;
    }                            A
}
Display Method
void display()
{
    node * temp;
                                     top
    temp=top;
    if(top==NULL)
                                 D
    {
        cout<<"stack empty";
        return;                  C
    }
    while(temp!=NULL)
    {                            B
        cout<<temp->data<<" ";
                                       tem
        temp=temp->next;
                                       p
    }                            A
}
Display Method
void display()
{
    node * temp;
                                     top
    temp=top;
    if(top==NULL)
                                 D
    {
        cout<<"stack empty";
        return;                  C
    }
    while(temp!=NULL)
    {                            B
        cout<<temp->data<<" ";
                                       tem
        temp=temp->next;
                                       p     display A
    }                            A
}
Display Method
void display()
{
    node * temp;
                                      top
    temp=top;
    if(top==NULL)
                                  D
    {
        cout<<"stack empty";
        return;                   C
    }
    while(temp!=NULL)
    {                             B
        cout<<temp->data<<" ";
        temp=temp->next;
    }                             A
}
                                 temp = NULL
Search Method
void search(int x)
{
   int a=0; //index calculated from top
   node * temp=top;                                 x=B
   while(temp!=NULL)                                a=0
   {                                          top
        if(temp->data==x)
        {                                 D
           cout<<"found at index "<<a;
           break;
         }
        else                              C
        {
         temp=temp->next;
         a++;                             B
        }
    }
   if(temp==NULL)
   cout<<“value not found";               A
}
};
Search Method
void search(int x)
{
   int a=0; //index calculated from top
   node * temp=top;                                 x=B
   while(temp!=NULL)                          tem
                                                    a=0
   {                                          top
                                              p
        if(temp->data==x)
        {                                 D
           cout<<"found at index "<<a;
           break;
         }
        else                              C
        {
         temp=temp->next;
         a++;                             B
        }
    }
   if(temp==NULL)
   cout<<“value not found";               A
}
};
Search Method
void search(int x)
{
   int a=0; //index calculated from top
   node * temp=top;                                 x=B
   while(temp!=NULL)                                a=1
   {                                          top
        if(temp->data==x)
        {                                 D
           cout<<"found at index "<<a;
           break;                             tem
         }                                    p
        else                              C
        {
         temp=temp->next;
         a++;                             B
        }
    }
   if(temp==NULL)
   cout<<“value not found";               A
}
};
Search Method
void search(int x)
{
   int a=0; //index calculated from top
   node * temp=top;                                 x=B
   while(temp!=NULL)                                a=2
   {                                          top
        if(temp->data==x)
        {                                 D
           cout<<"found at index "<<a;
           break;
         }
        else                              C
        {
                                              tem
         temp=temp->next;
                                              p
         a++;                             B
        }
    }
   if(temp==NULL)
   cout<<“value not found";               A
}
};
Search Method
void search(int x)
{
   int a=0; //index calculated from top
   node * temp=top;                                            x=B
   while(temp!=NULL)                                           a=2
   {                                                     top
        if(temp->data==x)
        {                                            D
           cout<<"found at index "<<a;
           break;
         }
        else                                         C
        {
                                                         tem
         temp=temp->next;
                                                         p
         a++;                             found at   B
        }                                  index 2
    }
   if(temp==NULL)
   cout<<“value not found";                          A
}
};
Main Method
                                          case 2:
void main()                               y=s.pop();
{clrscr();                                if(y!=NULL)
int n, a, y, z;                           cout<<"nvalue popped is: "<<y;
int e=0;                                  break;
stack s;                                  case 3:
   do                                     s.display();
   {cout<<"npress 1 to push";            break;
   cout<<"npress 2 to pop";              case 4:
   cout<<"npress 3 to display";          cout<<“enter value to search?”;
   cout<<“n press 4 to search”;          cin>>z;
   cout<<"npress 5to exitn";            s.search(z);
   cin>>n;                                break;
   switch(n)                              case 5:
    {                                     e=1;
   case 1:                                break;
   cout<<"enter a number";                }
   cin>>a;                             } while(e==0);
   s.push(a);                      }
   break;
TIME COMPLEXITY
    “Time Complexity is a measure of the amount
    of time required to execute an algorithm.”

   Push
    ◦ Best Case: O(1)
    ◦ Worst Case: O(1)
   Pop
    ◦ Best Case: O(1)
    ◦ Worst Case: O(1)
   Search
    ◦ Best Case: O(1)
    ◦ Worst Case: O(n)
LINK LIST
IMPLEMENTATION OF
   LINEAR QUEUE
Defining the linked list data
structure:
 struct node
 {
      int data;
      node *next;
 };


                    Data   next

                       node
Class Linear Queue
class linearQueue
{
 public:
   node *rear;
   node *front;
   linearQueue()
                     rear
   {                 front
       rear=NULL;
       front=NULL;
   }
Insert Method
void insert(int x)      rear
                        front
{
    node * ptr=new
       node;
    ptr->data=x;
    if(rear==NULL)
    {
      front=ptr;
    }
    else
    {
      rear->next=ptr;
    }
    ptr->next=NULL;
    rear=ptr;
}
Insert Method
void insert(int x)      rear
                        front   ptr
{
    node * ptr=new
       node;
    ptr->data=x;
    if(rear==NULL)
    {
      front=ptr;
    }
    else
    {
      rear->next=ptr;
    }
    ptr->next=NULL;
    rear=ptr;
}
Insert Method
void insert(int x)      rear
                        front   ptr
{
    node * ptr=new              x
       node;
    ptr->data=x;
    if(rear==NULL)
    {
      front=ptr;
    }
    else
    {
      rear->next=ptr;
    }
    ptr->next=NULL;
    rear=ptr;
}
Insert Method
void insert(int x)      rear   front
                               ptr
{
    node * ptr=new             x
       node;
    ptr->data=x;
    if(rear==NULL)
    {
      front=ptr;
    }
    else
    {
      rear->next=ptr;
    }
    ptr->next=NULL;
    rear=ptr;
}
Insert Method
void insert(int x)      rear   front
                               ptr
{
    node * ptr=new             x
       node;
    ptr->data=x;
    if(rear==NULL)
    {
      front=ptr;
    }
    else
    {
      rear->next=ptr;
    }
    ptr->next=NULL;
    rear=ptr;
}
Insert Method
                        rear
void insert(int x)      front
{                       ptr
    node * ptr=new      x
       node;
    ptr->data=x;
    if(rear==NULL)
    {
      front=ptr;
    }
    else
    {
      rear->next=ptr;
    }
    ptr->next=NULL;
    rear=ptr;
}
Insert Method
void insert(int x)
{
    node * ptr=new
       node;
    ptr->data=x;
    if(rear==NULL)
    {
      front=ptr;
    }
    else
                        rear
    {                   front   ptr
      rear->next=ptr;
    }                   A ptr   x
    ptr->next=NULL;
    rear=ptr;
}
Insert Method
void insert(int x)
{
    node * ptr=new
       node;
    ptr->data=x;
    if(rear==NULL)
    {
      front=ptr;
    }
    else
                        rear
    {                   front   ptr
      rear->next=ptr;
    }                   A       x
    ptr->next=NULL;
    rear=ptr;
}
Insert Method
void insert(int x)
{
    node * ptr=new
       node;
    ptr->data=x;
    if(rear==NULL)
    {
      front=ptr;
    }
    else
                        rear
    {                   front   ptr
      rear->next=ptr;
    }                   A ptr   x
    ptr->next=NULL;
    rear=ptr;
}
Insert Method
void insert(int x)
{
    node * ptr=new
       node;
    ptr->data=x;
    if(rear==NULL)
    {
      front=ptr;
    }
    else                        rear
    {                   front   ptr
      rear->next=ptr;
    }                   A ptr   x
    ptr->next=NULL;
    rear=ptr;
}
Remove Method
int remove()
{   if(rear==NULL)
    {                                rear    temp= A
                                     front
        cout<<"nlist emptyn";
        return 0;                    A
    }
    else
    {
        int temp=front->data;
        node* ptr=front;
        if(front->next==NULL)
             { rear=NULL; }
        else
              { front=ptr->next; }
        delete ptr;
        return temp;
    }
}
Remove Method
int remove()
                                             temp= A
{   if(rear==NULL)                   ptr
    {                                rear
                                     front
        cout<<"nlist emptyn";
        return 0;                    A
    }
    else
    {
        int temp=front->data;
        node* ptr=front;
        if(front->next==NULL)
             { rear=NULL; }
        else
              { front=ptr->next; }
        delete ptr;
        return temp;
    }
}
Remove Method
int remove()
                                             temp= A
{   if(rear==NULL)
                                     ptr
    {
                                     front     rear
        cout<<"nlist emptyn";
        return 0;                    A
    }
    else
    {
        int temp=front->data;
        node* ptr=front;
        if(front->next==NULL)
             { rear=NULL; }
        else
              { front=ptr->next; }
        delete ptr;
        return temp;
    }
}
Remove Method
int remove()
                                             temp= A
{   if(rear==NULL)
                                     ptr
    {
                                     front     rear
        cout<<"nlist emptyn";
        return 0;                    A
    }
    else
    {
        int temp=front->data;
        node* ptr=front;
        if(front->next==NULL)
             { rear=NULL; }
        else
              { front=ptr->next; }
        delete ptr;
        return temp;
    }
}
Remove Method
int remove()
{   if(rear==NULL)
    {
        cout<<"nlist emptyn";
        return 0;
    }
    else
    {
        int temp=front->data;        temp=A
        node* ptr=front;
        if(front->next==NULL)
                                      ptr
             { rear=NULL; }           front   rear
        else
                                     A        B
              { front=ptr->next; }
        delete ptr;
        return temp;
    }
}
Remove Method
int remove()
{   if(rear==NULL)
    {
        cout<<"nlist emptyn";
        return 0;
    }
    else
    {
        int temp=front->data;        temp=A
        node* ptr=front;
        if(front->next==NULL)                 front
                                      ptr
             { rear=NULL; }                   rear
        else
                                     A        B
              { front=ptr->next; }
        delete ptr;
        return temp;
    }
}
Remove Method
int remove()
{   if(rear==NULL)
    {
        cout<<"nlist emptyn";
        return 0;
    }
    else
    {
        int temp=front->data;        temp=A
        node* ptr=front;
        if(front->next==NULL)                 front
                                      ptr
             { rear=NULL; }                   rear
        else
                                     A        B
              { front=ptr->next; }
        delete ptr;
        return temp;
    }
}
Display Method
void display()
{
    if(rear==NULL)
    {
        cout<<"list empty";
        return;
    }
    cout<<"data in nodes of link
      list:t";
    node *temp=front;
    while(temp!=NULL)
    {
        cout<<temp->data<<" ";
        temp=temp->next;
                                 front       rear
    }
                                 A       B   C
}
Display Method
void display()
{
    if(rear==NULL)
    {
        cout<<"list empty";
        return;
    }
    cout<<"data in nodes of link
      list:t";
    node *temp=front;
    while(temp!=NULL)
    {
        cout<<temp->data<<" ";
        temp=temp->next;       temp
                               front       rear
    }
                               A       B   C
}
Display Method
void display()
{
    if(rear==NULL)
    {
        cout<<"list empty";
        return;
    }
    cout<<"data in nodes of link
      list:t";
    node *temp=front;
    while(temp!=NULL)                      display A
    {
        cout<<temp->data<<" ";
        temp=temp->next;       temp
                               front             rear
    }
                               A       B        C
}
Display Method
void display()
{
    if(rear==NULL)
    {
        cout<<"list empty";
        return;
    }
    cout<<"data in nodes of link
      list:t";
    node *temp=front;
    while(temp!=NULL)
    {
        cout<<temp->data<<" ";
        temp=temp->next;                     temp
                                 front              rear
    }
                                 A       B          C
}
Display Method
void display()
{
    if(rear==NULL)
    {
        cout<<"list empty";
        return;
    }
    cout<<"data in nodes of link
      list:t";
    node *temp=front;
    while(temp!=NULL)                               display B
    {
        cout<<temp->data<<" ";
        temp=temp->next;                     temp
                                 front                   rear
    }
                                 A       B               C
}
Display Method
void display()
{
    if(rear==NULL)
    {
        cout<<"list empty";
        return;
    }
    cout<<"data in nodes of link
      list:t";
    node *temp=front;
    while(temp!=NULL)
    {
        cout<<temp->data<<" ";
        temp=temp->next;                     temp
                                 front        rear
    }
                                 A       B   C
}
Display Method
void display()
{
    if(rear==NULL)
    {
        cout<<"list empty";
        return;
    }
    cout<<"data in nodes of link
      list:t";
    node *temp=front;
    while(temp!=NULL)                        display C
    {
        cout<<temp->data<<" ";
        temp=temp->next;                          temp
                                 front             rear
    }
                                 A       B        C
}
Search Method
void search(int x)
{
   int a=0;
   node * temp=front;
   while(temp!=NULL)
   {
        if(temp->data==x)
        {
           cout<<"found at index "<<a;
           break;                              x=C
         }                                     a=0
        else
        {
         temp=temp->next;          front       rear
         a++;
        }                          A       B   C
    }
   if(temp==NULL)
   cout<<“value not found";
}
};
Search Method
void search(int x)
{
   int a=0;
   node * temp=front;
   while(temp!=NULL)
   {
        if(temp->data==x)
        {
           cout<<"found at index "<<a;
           break;                              x=C
         }                                     a=0
        else
        {                          temp
         temp=temp->next;          front       rear
         a++;
        }                          A       B   C
    }
   if(temp==NULL)
   cout<<“value not found";
}
};
Search Method
void search(int x)
{
   int a=0;
   node * temp=front;
   while(temp!=NULL)
   {
        if(temp->data==x)
        {
           cout<<"found at index "<<a;
           break;                                 x=C
         }                                        a=1
        else
        {
                                           temp
         temp=temp->next;          front          rear
         a++;
        }                          A       B      C
    }
   if(temp==NULL)
   cout<<“value not found";
}
};
Search Method
void search(int x)
{
   int a=0;
   node * temp=front;
   while(temp!=NULL)
   {
        if(temp->data==x)
        {
           cout<<"found at index "<<a;
           break;                              x=C
         }                                     a=2
        else
        {                                      temp
         temp=temp->next;          front       rear
         a++;
        }                          A       B   C
    }
   if(temp==NULL)
   cout<<“value not found";
}
};
Search Method
void search(int x)
{
   int a=0;
   node * temp=front;
   while(temp!=NULL)
   {
        if(temp->data==x)
        {
           cout<<"found at index "<<a;
           break;                                x=C
         }                                       a=2
        else
        {                                        temp
         temp=temp->next;          front         rear
         a++;
        }                          A       B    C
    }
                                               found at
   if(temp==NULL)
                                                index 2
   cout<<“value not found";
}
};
Main Method
                                          case 2:
void main()                               y=q.pop();
{clrscr();                                if(y!=NULL)
int n, a, y, z;                           cout<<"nvalue popped is: "<<y;
int e=0;                                  break;
   linearQueue q;                         case 3:
   do                                     q.display();
   {cout<<"npress 1 to push";            break;
   cout<<"npress 2 to pop";              case 4:
   cout<<"npress 3 to display";          cout<<“enter value to search?”;
   cout<<“n press 4 to search”;          cin>>z;
   cout<<"npress 5 to exitn";           q.search(z);
   cin>>n;                                break;
   switch(n)                              case 5:
     {                                    e=1;
   case 1:                                break;
   cout<<"enter a number";                }
   cin>>a;                             } while(e==0);
   q.push(a);                      }
   break;
TIME COMPLEXITY
   Insert
    ◦ Best Case: O(1)
    ◦ Worst Case: O(1)
   Remove
    ◦ Best Case: O(1)
    ◦ Worst Case: O(1)
   Search
    ◦ Best Case: O(1)
    ◦ Worst Case: O(n)
LINK LIST IMPLEMENTATION
   OF CIRCULAR QUEUE
Defining the linked list data
structure:
  struct node
  {
       int data;
       node *next;
  };


                     Data   next

                        node
Class circularQueue

class
  circularQueue
{
 public:
   node *rear;
                      rear
   circularQueue()
   {
       rear=NULL;
   }
Insert Method                rear

void insert(int x)
{
  node * ptr = new node;
  ptr -> data=x;
  if(rear==NULL)
     ptr -> next=ptr;
    else
    {
     ptr->next=rear->next;
     rear->next=ptr;
    }
    rear=ptr;
}
Insert Method                rear   ptr

void insert(int x)
{
  node * ptr = new node;
  ptr -> data=x;
  if(rear==NULL)
     ptr -> next=ptr;
    else
    {
     ptr->next=rear->next;
     rear->next=ptr;
    }
    rear=ptr;
}
Insert Method                rear   ptr

void insert(int x)                  x

{
  node * ptr = new node;
  ptr -> data=x;
  if(rear==NULL)
     ptr -> next=ptr;
    else
    {
     ptr->next=rear->next;
     rear->next=ptr;
    }
    rear=ptr;
}
Insert Method                rear   ptr
                                          Address
void insert(int x)                  x     of itself

{
  node * ptr = new node;
  ptr -> data=x;
  if(rear==NULL)
     ptr -> next=ptr;
    else
    {
     ptr->next=rear->next;
     rear->next=ptr;
    }
    rear=ptr;
}
Insert Method                rear
                             ptr
                                 Address
void insert(int x)           x   of itself

{
  node * ptr = new node;
  ptr -> data=x;
  if(rear==NULL)
     ptr -> next=ptr;
    else
    {
     ptr->next=rear->next;
     rear->next=ptr;
    }
    rear=ptr;
}
Insert Method
void insert(int x)
{
  node * ptr = new node;
  ptr -> data=x;
  if(rear==NULL)
     ptr -> next=ptr;
    else                     rear                ptr

    {                        A
                                 Address
                                 of itself
                                             x
     ptr->next=rear->next;
     rear->next=ptr;
    }
    rear=ptr;
}
Insert Method
void insert(int x)
{
  node * ptr = new node;
  ptr -> data=x;
  if(rear==NULL)
     ptr -> next=ptr;
    else                     rear                ptr

    {                        A
                                 Address
                                 of itself
                                             x
     ptr->next=rear->next;
     rear->next=ptr;
    }
    rear=ptr;
}
Insert Method
void insert(int x)
{
  node * ptr = new node;
  ptr -> data=x;
  if(rear==NULL)
     ptr -> next=ptr;
    else                     rear       ptr

    {                        A      x
     ptr->next=rear->next;
     rear->next=ptr;
    }
    rear=ptr;
}
Insert Method
void insert(int x)
{
  node * ptr = new node;
  ptr -> data=x;
  if(rear==NULL)
     ptr -> next=ptr;
                                 rear
    else                          ptr

    {                        A   x
     ptr->next=rear->next;
     rear->next=ptr;
    }
    rear=ptr;
}
Remove
     Method
int remove()
{
    if(rear==NULL)
    {
        cout<<"nlist emptyn";
        return 0;                       rear
    }
    else                                A   Address
                                            of itself

    {
        int temp=rear->next->data;
        node* ptr=rear->next;
        if(rear==rear->next)
        { rear=NULL;
        }
        else
        {rear->next=rear->next->next;
        }
        delete ptr;
        return temp;
    }
}
Remove
     Method
int remove()
{
    if(rear==NULL)                              temp=A
    {
        cout<<"nlist emptyn";
        return 0;                       rear
    }
    else                                A   Address
                                            of itself

    {
        int temp=rear->next->data;
        node* ptr=rear->next;
        if(rear==rear->next)
        { rear=NULL;
        }
        else
        {rear->next=rear->next->next;
        }
        delete ptr;
        return temp;
    }
}
Remove
     Method
int remove()
{
    if(rear==NULL)                              temp=A
    {
        cout<<"nlist emptyn";          ptr
        return 0;                       rear
    }
    else                                A   Address
                                            of itself

    {
        int temp=rear->next->data;
        node* ptr=rear->next;
        if(rear==rear->next)
        { rear=NULL;
        }
        else
        {rear->next=rear->next->next;
        }
        delete ptr;
        return temp;
    }
}
Remove
     Method
int remove()
{
    if(rear==NULL)                              temp=A
    {
        cout<<"nlist emptyn";         ptr
        return 0;
    }
    else                                A   Address
                                            of itself

    {
        int temp=rear->next->data;
        node* ptr=rear->next;           rear
        if(rear==rear->next)
        { rear=NULL;
        }
        else
        {rear->next=rear->next->next;
        }
        delete ptr;
        return temp;
    }
}
Remove
     Method
int remove()
{
    if(rear==NULL)                              temp=A
    {
        cout<<"nlist emptyn";         ptr
        return 0;
    }
    else                                A   Address
                                            of itself

    {
        int temp=rear->next->data;
        node* ptr=rear->next;           rear
        if(rear==rear->next)
        { rear=NULL;
        }
        else
        {rear->next=rear->next->next;
        }
        delete ptr;
        return temp;
    }
}
Remove
     Method
int remove()
{
    if(rear==NULL)
    {
        cout<<"nlist emptyn";
        return 0;
    }
    else
    {
        int temp=rear->next->data;
        node* ptr=rear->next;                   rear
        if(rear==rear->next)
        { rear=NULL;                    A   B   C
        }
        else
        {rear->next=rear->next->next;
        }
        delete ptr;
        return temp;
    }
}
Remove
     Method
int remove()
{
    if(rear==NULL)
    {
        cout<<"nlist emptyn";
        return 0;
    }
    else
                                        temp=A
    {
        int temp=rear->next->data;
        node* ptr=rear->next;                        rear
        if(rear==rear->next)
        { rear=NULL;                    A        B   C
        }
        else
        {rear->next=rear->next->next;
        }
        delete ptr;
        return temp;
    }
}
Remove
     Method
int remove()
{
    if(rear==NULL)
    {
        cout<<"nlist emptyn";
        return 0;
    }
    else
                                        temp=A
    {
        int temp=rear->next->data;
        node* ptr=rear->next;               ptr       rear
        if(rear==rear->next)
        { rear=NULL;                    A         B   C
        }
        else
        {rear->next=rear->next->next;
        }
        delete ptr;
        return temp;
    }
}
Remove
     Method
int remove()
{
    if(rear==NULL)
    {
        cout<<"nlist emptyn";
        return 0;
    }
    else
                                        temp=A
    {
        int temp=rear->next->data;
        node* ptr=rear->next;               ptr       rear
        if(rear==rear->next)
        { rear=NULL;                    A         B   C
        }
        else
        {rear->next=rear->next->next;
        }
        delete ptr;
        return temp;
    }
}
Remove
     Method
int remove()
{
    if(rear==NULL)
    {
        cout<<"nlist emptyn";
        return 0;
    }
    else
                                        temp=A
    {
        int temp=rear->next->data;
        node* ptr=rear->next;               ptr       rear
        if(rear==rear->next)
        { rear=NULL;                    A         B   C
        }
        else
        {rear->next=rear->next->next;
        }
        delete ptr;
        return temp;
    }
}
Display Method
void display()
{
        if(rear==NULL)
        {
          cout<<"nlist emptyn";
          return;
        }
        node * temp=rear->next;
        do
        {
          cout<<temp->data<<" ";
          temp=temp->next;
        }while(temp!=rear->next);           rear
    }
                                    A   B   C
.
Display Method
void display()
{
        if(rear==NULL)
        {
          cout<<"nlist emptyn";
          return;
        }
        node * temp=rear->next;
        do
        {
          cout<<temp->data<<" ";
          temp=temp->next;
        }while(temp!=rear->next);
                                    temp       rear
    }
.                                   A      B   C
Display Method
void display()
{
        if(rear==NULL)
        {
          cout<<"nlist emptyn";
          return;
        }
        node * temp=rear->next;
        do
        {                                      display A
          cout<<temp->data<<" ";
          temp=temp->next;
        }while(temp!=rear->next);
                                    temp             rear
    }
.                                   A      B         C
Display Method
void display()
{
        if(rear==NULL)
        {
          cout<<"nlist emptyn";
          return;
        }
        node * temp=rear->next;
        do
        {
          cout<<temp->data<<" ";
          temp=temp->next;
        }while(temp!=rear->next);       temp   rear
    }
                                    A   B      C
.
Display Method
void display()
{
        if(rear==NULL)
        {
          cout<<"nlist emptyn";
          return;
        }
        node * temp=rear->next;
        do
        {                                      display B
          cout<<temp->data<<" ";
          temp=temp->next;
        }while(temp!=rear->next);       temp         rear
    }
                                    A   B           C
.
Display Method
void display()
{
        if(rear==NULL)
        {
          cout<<"nlist emptyn";
          return;
        }
        node * temp=rear->next;
        do
        {
          cout<<temp->data<<" ";
          temp=temp->next;                   temp
        }while(temp!=rear->next);           rear
    }
                                    A   B   C
.
Display Method
void display()
{
        if(rear==NULL)
        {
          cout<<"nlist emptyn";
          return;
        }
        node * temp=rear->next;
        do
        {                                   display C
          cout<<temp->data<<" ";
          temp=temp->next;                         temp
        }while(temp!=rear->next);                 rear
    }
                                    A   B        C
.
Search Method
void search(int x)
{
  int a=0;
  node * temp= rear->next
  while(temp!=NULL)
  {
       if(temp->data==x)
       {
          cout<<"found at index "<<a;
          break;                            x=C
        }                                   a=0
       else
       {
        temp=temp->next;
        a++;                                rear
       }                                    C
   }                              A     B
  if(temp==NULL)
  cout<<“value not found";
}
};
Search Method
void search(int x)
{
  int a=0;
  node * temp=rear->next;
  while(temp!=NULL)
  {
       if(temp->data==x)
       {
          cout<<"found at index "<<a;
          break;                             x=C
        }                                    a=0
       else
       {
        temp=temp->next;          temp
        a++;                                 rear
       }                                     C
   }                              A      B
  if(temp==NULL)
  cout<<“value not found";
}
};
Search Method
void search(int x)
{
  int a=0;
  node * temp=rear->next;
  while(temp!=NULL)
  {
       if(temp->data==x)
       {
          cout<<"found at index "<<a;
          break;                                   x=C
        }                                          a=1
       else
       {
        temp=temp->next;                    temp
        a++;                                       rear
       }                                           C
   }                              A     B
  if(temp==NULL)
  cout<<“value not found";
}
};
Search Method
void search(int x)
{
  int a=0;
  node * temp=rear->next;
  while(temp!=NULL)
  {
       if(temp->data==x)
       {
          cout<<"found at index "<<a;
          break;                            x=C
        }                                   a=2
       else
       {
        temp=temp->next;                    temp
        a++;                                 rear
       }                                    C
   }                              A     B
  if(temp==NULL)
  cout<<“value not found";
}
};
Search Method
void search(int x)
{
  int a=0;
  node * temp=rear->next;
  while(temp!=NULL)
  {
       if(temp->data==x)
       {
          cout<<"found at index "<<a;
          break;                              x=C
        }                                     a=2
       else
       {
        temp=temp->next;                     temp
        a++;                                  rear
       }                                     C
   }                              A     B
                                            found at
  if(temp==NULL)
                                             index 2
  cout<<“value not found";
}
};
Main Method
                                          case 2:
void main()                               y=q.pop();
{clrscr();                                if(y!=NULL)
int n, a, y, z;                           cout<<"nvalue popped is: "<<y;
int e=0;                                  break;
   circularQueue q;                       case 3:
   do                                     q.display();
   {cout<<"npress 1 to push";            break;
   cout<<"npress 2 to pop";              case 4:
   cout<<"npress 3 to display";          cout<<“enter value to search?”;
   cout<<“n press 4 to search”;          cin>>z;
   cout<<"npress 5 to exitn";           q.search(z);
   cin>>n;                                break;
   switch(n)                              case 5:
    {                                     e=1;
   case 1:                                break;
   cout<<"enter a number";                }
   cin>>a;                             } while(e==0);
   q.push(a);                      }
   break;
TIME COMPLEXITY
   Insert
    ◦ Best Case: O(1)
    ◦ Worst Case: O(1)
   Remove
    ◦ Best Case: O(1)
    ◦ Worst Case: O(1)
   Search
    ◦ Best Case: O(1)
    ◦ Worst Case: O(n)
THANK YOU!

Link list

  • 2.
    LINKED LIST PRESENTED BY: Javeria (11-arid-3303) MIT-3 University Institute of Information Technology, Rawalpindi (UIIT, UAAR)
  • 3.
    INTRODUTION  Link List is an ordered collection of elements called nodes. The nodes are connected by pointer.  Each node has two parts 1) Data Field – Stores data of the node. Same data type 2) Link Field – Store address of the next node ( i.e. Link to the next node) NEXT NODE : DATA
  • 4.
    ARRAY VS. LINKLIST Aspect Array Link List Size  Fixed size.  Grow and contract according to insertions and deletions. Storage  Static: It’s location is  Dynamic: It’s node is capacity allocated during compile located during run time. time. Accessing the  Direct or random access  Sequential access element method. method.  Specify the array index or  Traverse starting from the subscript. first node in the list by pointer. 4
  • 5.
    SINGLY LINK LIST IMPLEMENTATIONIN STACKS: A B C top IMPLEMENTATION IN LINEAR QUEUE: front A B C rear IMPLEMENTATION IN CIRCULAR QUEUE: A B C rear DOUBLY LINK LIST A B C rear
  • 6.
    LINKED LIST IMPLEMENTATION  Stacks  Linear Queue  Circular Queue
  • 7.
    Functions ◦ We willdiscuss the following functions in above three data structures:  Insertion  Deletion  Display  Search
  • 8.
  • 9.
    Defining the linkedlist data structure: struct node { int data; node *next; }; Data next node
  • 10.
    STACK CLASS class stack { public: node * top; stack( ) { top top=NULL; }
  • 11.
    Push method void push(intx) { node * ptr=new node; top ptr->data=x; ptr->next=top; top=ptr; }
  • 12.
    Push method void push(intx) ptr { node * ptr=new node; top ptr->data=x; ptr->next=top; top=ptr; }
  • 13.
    Push method void push(intx) ptr { x node * ptr=new node; top ptr->data=x; ptr->next=top; top=ptr; }
  • 14.
    Push method void push(intx) ptr { x node * ptr=new node; top ptr->data=x; ptr->next=top; top=ptr; }
  • 15.
    Push method void push(intx) ptr { x node * ptr=new node; top ptr->data=x; ptr->next=top; top=ptr; }
  • 16.
    Push method void push(intx) { top ptr node * ptr=new node; ptr->data=x; x ptr->next=top; top=ptr; }
  • 17.
    - All nodes are connected to each other through pointers - Link of the first node is NULL pointer denoted by X - Null pointer indicates the start of the stack list top D C B A
  • 18.
    Pop Method int pop() { top if(top==NULL) { cout<<"nlist emptyn"; D return; } C else { int temp=top->data; node* ptr=top; B top=top->next; delete ptr; return temp; A } }
  • 19.
    Pop Method int pop() { top temp=D if(top==NULL) { cout<<"nlist emptyn"; D return ; } C else { int temp=top->data; node* ptr=top; B top=top->next; delete ptr; return temp; A } }
  • 20.
    Pop Method int pop() ptr { top temp=D if(top==NULL) { cout<<"nlist emptyn"; D return ; } C else { int temp=top->data; node* ptr=top; B top=top->next; delete ptr; return temp; A } }
  • 21.
    Pop Method int pop() { ptr temp=D if(top==NULL) { D cout<<"nlist emptyn"; return ; top } C else { int temp=top->data; node* ptr=top; B top=top->next; delete ptr; return temp; A } }
  • 22.
    Pop Method int pop() { ptr temp=D if(top==NULL) { D cout<<"nlist emptyn"; return ; top } C else { int temp=top->data; node* ptr=top; B top=top->next; delete ptr; return temp; A } }
  • 23.
    Display Method void display() { node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; return; C } while(temp!=NULL) { B cout<<temp->data<<" "; temp=temp->next; } A }
  • 24.
    Display Method void display() { node * temp; tem top p temp=top; if(top==NULL) D { cout<<"stack empty"; return; C } while(temp!=NULL) { B cout<<temp->data<<" "; temp=temp->next; } A }
  • 25.
    Display Method void display() { node * temp; tem top p temp=top; display D if(top==NULL) D { cout<<"stack empty"; return; C } while(temp!=NULL) { B cout<<temp->data<<" "; temp=temp->next; } A }
  • 26.
    Display Method void display() { node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; tem p return; C } while(temp!=NULL) { B cout<<temp->data<<" "; temp=temp->next; } A }
  • 27.
    Display Method void display() { node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; tem p return; C display C } while(temp!=NULL) { B cout<<temp->data<<" "; temp=temp->next; } A }
  • 28.
    Display Method void display() { node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; return; C } tem while(temp!=NULL) p { B cout<<temp->data<<" "; temp=temp->next; } A }
  • 29.
    Display Method void display() { node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; return; C } tem while(temp!=NULL) p display B { B cout<<temp->data<<" "; temp=temp->next; } A }
  • 30.
    Display Method void display() { node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; return; C } while(temp!=NULL) { B cout<<temp->data<<" "; tem temp=temp->next; p } A }
  • 31.
    Display Method void display() { node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; return; C } while(temp!=NULL) { B cout<<temp->data<<" "; tem temp=temp->next; p display A } A }
  • 32.
    Display Method void display() { node * temp; top temp=top; if(top==NULL) D { cout<<"stack empty"; return; C } while(temp!=NULL) { B cout<<temp->data<<" "; temp=temp->next; } A } temp = NULL
  • 33.
    Search Method void search(intx) { int a=0; //index calculated from top node * temp=top; x=B while(temp!=NULL) a=0 { top if(temp->data==x) { D cout<<"found at index "<<a; break; } else C { temp=temp->next; a++; B } } if(temp==NULL) cout<<“value not found"; A } };
  • 34.
    Search Method void search(intx) { int a=0; //index calculated from top node * temp=top; x=B while(temp!=NULL) tem a=0 { top p if(temp->data==x) { D cout<<"found at index "<<a; break; } else C { temp=temp->next; a++; B } } if(temp==NULL) cout<<“value not found"; A } };
  • 35.
    Search Method void search(intx) { int a=0; //index calculated from top node * temp=top; x=B while(temp!=NULL) a=1 { top if(temp->data==x) { D cout<<"found at index "<<a; break; tem } p else C { temp=temp->next; a++; B } } if(temp==NULL) cout<<“value not found"; A } };
  • 36.
    Search Method void search(intx) { int a=0; //index calculated from top node * temp=top; x=B while(temp!=NULL) a=2 { top if(temp->data==x) { D cout<<"found at index "<<a; break; } else C { tem temp=temp->next; p a++; B } } if(temp==NULL) cout<<“value not found"; A } };
  • 37.
    Search Method void search(intx) { int a=0; //index calculated from top node * temp=top; x=B while(temp!=NULL) a=2 { top if(temp->data==x) { D cout<<"found at index "<<a; break; } else C { tem temp=temp->next; p a++; found at B } index 2 } if(temp==NULL) cout<<“value not found"; A } };
  • 38.
    Main Method case 2: void main() y=s.pop(); {clrscr(); if(y!=NULL) int n, a, y, z; cout<<"nvalue popped is: "<<y; int e=0; break; stack s; case 3: do s.display(); {cout<<"npress 1 to push"; break; cout<<"npress 2 to pop"; case 4: cout<<"npress 3 to display"; cout<<“enter value to search?”; cout<<“n press 4 to search”; cin>>z; cout<<"npress 5to exitn"; s.search(z); cin>>n; break; switch(n) case 5: { e=1; case 1: break; cout<<"enter a number"; } cin>>a; } while(e==0); s.push(a); } break;
  • 39.
    TIME COMPLEXITY “Time Complexity is a measure of the amount of time required to execute an algorithm.”  Push ◦ Best Case: O(1) ◦ Worst Case: O(1)  Pop ◦ Best Case: O(1) ◦ Worst Case: O(1)  Search ◦ Best Case: O(1) ◦ Worst Case: O(n)
  • 40.
  • 41.
    Defining the linkedlist data structure: struct node { int data; node *next; }; Data next node
  • 42.
    Class Linear Queue classlinearQueue { public: node *rear; node *front; linearQueue() rear { front rear=NULL; front=NULL; }
  • 43.
    Insert Method void insert(intx) rear front { node * ptr=new node; ptr->data=x; if(rear==NULL) { front=ptr; } else { rear->next=ptr; } ptr->next=NULL; rear=ptr; }
  • 44.
    Insert Method void insert(intx) rear front ptr { node * ptr=new node; ptr->data=x; if(rear==NULL) { front=ptr; } else { rear->next=ptr; } ptr->next=NULL; rear=ptr; }
  • 45.
    Insert Method void insert(intx) rear front ptr { node * ptr=new x node; ptr->data=x; if(rear==NULL) { front=ptr; } else { rear->next=ptr; } ptr->next=NULL; rear=ptr; }
  • 46.
    Insert Method void insert(intx) rear front ptr { node * ptr=new x node; ptr->data=x; if(rear==NULL) { front=ptr; } else { rear->next=ptr; } ptr->next=NULL; rear=ptr; }
  • 47.
    Insert Method void insert(intx) rear front ptr { node * ptr=new x node; ptr->data=x; if(rear==NULL) { front=ptr; } else { rear->next=ptr; } ptr->next=NULL; rear=ptr; }
  • 48.
    Insert Method rear void insert(int x) front { ptr node * ptr=new x node; ptr->data=x; if(rear==NULL) { front=ptr; } else { rear->next=ptr; } ptr->next=NULL; rear=ptr; }
  • 49.
    Insert Method void insert(intx) { node * ptr=new node; ptr->data=x; if(rear==NULL) { front=ptr; } else rear { front ptr rear->next=ptr; } A ptr x ptr->next=NULL; rear=ptr; }
  • 50.
    Insert Method void insert(intx) { node * ptr=new node; ptr->data=x; if(rear==NULL) { front=ptr; } else rear { front ptr rear->next=ptr; } A x ptr->next=NULL; rear=ptr; }
  • 51.
    Insert Method void insert(intx) { node * ptr=new node; ptr->data=x; if(rear==NULL) { front=ptr; } else rear { front ptr rear->next=ptr; } A ptr x ptr->next=NULL; rear=ptr; }
  • 52.
    Insert Method void insert(intx) { node * ptr=new node; ptr->data=x; if(rear==NULL) { front=ptr; } else rear { front ptr rear->next=ptr; } A ptr x ptr->next=NULL; rear=ptr; }
  • 53.
    Remove Method int remove() { if(rear==NULL) { rear temp= A front cout<<"nlist emptyn"; return 0; A } else { int temp=front->data; node* ptr=front; if(front->next==NULL) { rear=NULL; } else { front=ptr->next; } delete ptr; return temp; } }
  • 54.
    Remove Method int remove() temp= A { if(rear==NULL) ptr { rear front cout<<"nlist emptyn"; return 0; A } else { int temp=front->data; node* ptr=front; if(front->next==NULL) { rear=NULL; } else { front=ptr->next; } delete ptr; return temp; } }
  • 55.
    Remove Method int remove() temp= A { if(rear==NULL) ptr { front rear cout<<"nlist emptyn"; return 0; A } else { int temp=front->data; node* ptr=front; if(front->next==NULL) { rear=NULL; } else { front=ptr->next; } delete ptr; return temp; } }
  • 56.
    Remove Method int remove() temp= A { if(rear==NULL) ptr { front rear cout<<"nlist emptyn"; return 0; A } else { int temp=front->data; node* ptr=front; if(front->next==NULL) { rear=NULL; } else { front=ptr->next; } delete ptr; return temp; } }
  • 57.
    Remove Method int remove() { if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else { int temp=front->data; temp=A node* ptr=front; if(front->next==NULL) ptr { rear=NULL; } front rear else A B { front=ptr->next; } delete ptr; return temp; } }
  • 58.
    Remove Method int remove() { if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else { int temp=front->data; temp=A node* ptr=front; if(front->next==NULL) front ptr { rear=NULL; } rear else A B { front=ptr->next; } delete ptr; return temp; } }
  • 59.
    Remove Method int remove() { if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else { int temp=front->data; temp=A node* ptr=front; if(front->next==NULL) front ptr { rear=NULL; } rear else A B { front=ptr->next; } delete ptr; return temp; } }
  • 60.
    Display Method void display() { if(rear==NULL) { cout<<"list empty"; return; } cout<<"data in nodes of link list:t"; node *temp=front; while(temp!=NULL) { cout<<temp->data<<" "; temp=temp->next; front rear } A B C }
  • 61.
    Display Method void display() { if(rear==NULL) { cout<<"list empty"; return; } cout<<"data in nodes of link list:t"; node *temp=front; while(temp!=NULL) { cout<<temp->data<<" "; temp=temp->next; temp front rear } A B C }
  • 62.
    Display Method void display() { if(rear==NULL) { cout<<"list empty"; return; } cout<<"data in nodes of link list:t"; node *temp=front; while(temp!=NULL) display A { cout<<temp->data<<" "; temp=temp->next; temp front rear } A B C }
  • 63.
    Display Method void display() { if(rear==NULL) { cout<<"list empty"; return; } cout<<"data in nodes of link list:t"; node *temp=front; while(temp!=NULL) { cout<<temp->data<<" "; temp=temp->next; temp front rear } A B C }
  • 64.
    Display Method void display() { if(rear==NULL) { cout<<"list empty"; return; } cout<<"data in nodes of link list:t"; node *temp=front; while(temp!=NULL) display B { cout<<temp->data<<" "; temp=temp->next; temp front rear } A B C }
  • 65.
    Display Method void display() { if(rear==NULL) { cout<<"list empty"; return; } cout<<"data in nodes of link list:t"; node *temp=front; while(temp!=NULL) { cout<<temp->data<<" "; temp=temp->next; temp front rear } A B C }
  • 66.
    Display Method void display() { if(rear==NULL) { cout<<"list empty"; return; } cout<<"data in nodes of link list:t"; node *temp=front; while(temp!=NULL) display C { cout<<temp->data<<" "; temp=temp->next; temp front rear } A B C }
  • 67.
    Search Method void search(intx) { int a=0; node * temp=front; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=0 else { temp=temp->next; front rear a++; } A B C } if(temp==NULL) cout<<“value not found"; } };
  • 68.
    Search Method void search(intx) { int a=0; node * temp=front; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=0 else { temp temp=temp->next; front rear a++; } A B C } if(temp==NULL) cout<<“value not found"; } };
  • 69.
    Search Method void search(intx) { int a=0; node * temp=front; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=1 else { temp temp=temp->next; front rear a++; } A B C } if(temp==NULL) cout<<“value not found"; } };
  • 70.
    Search Method void search(intx) { int a=0; node * temp=front; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=2 else { temp temp=temp->next; front rear a++; } A B C } if(temp==NULL) cout<<“value not found"; } };
  • 71.
    Search Method void search(intx) { int a=0; node * temp=front; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=2 else { temp temp=temp->next; front rear a++; } A B C } found at if(temp==NULL) index 2 cout<<“value not found"; } };
  • 72.
    Main Method case 2: void main() y=q.pop(); {clrscr(); if(y!=NULL) int n, a, y, z; cout<<"nvalue popped is: "<<y; int e=0; break; linearQueue q; case 3: do q.display(); {cout<<"npress 1 to push"; break; cout<<"npress 2 to pop"; case 4: cout<<"npress 3 to display"; cout<<“enter value to search?”; cout<<“n press 4 to search”; cin>>z; cout<<"npress 5 to exitn"; q.search(z); cin>>n; break; switch(n) case 5: { e=1; case 1: break; cout<<"enter a number"; } cin>>a; } while(e==0); q.push(a); } break;
  • 73.
    TIME COMPLEXITY  Insert ◦ Best Case: O(1) ◦ Worst Case: O(1)  Remove ◦ Best Case: O(1) ◦ Worst Case: O(1)  Search ◦ Best Case: O(1) ◦ Worst Case: O(n)
  • 74.
    LINK LIST IMPLEMENTATION OF CIRCULAR QUEUE
  • 75.
    Defining the linkedlist data structure: struct node { int data; node *next; }; Data next node
  • 76.
    Class circularQueue class circularQueue { public: node *rear; rear circularQueue() { rear=NULL; }
  • 77.
    Insert Method rear void insert(int x) { node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else { ptr->next=rear->next; rear->next=ptr; } rear=ptr; }
  • 78.
    Insert Method rear ptr void insert(int x) { node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else { ptr->next=rear->next; rear->next=ptr; } rear=ptr; }
  • 79.
    Insert Method rear ptr void insert(int x) x { node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else { ptr->next=rear->next; rear->next=ptr; } rear=ptr; }
  • 80.
    Insert Method rear ptr Address void insert(int x) x of itself { node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else { ptr->next=rear->next; rear->next=ptr; } rear=ptr; }
  • 81.
    Insert Method rear ptr Address void insert(int x) x of itself { node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else { ptr->next=rear->next; rear->next=ptr; } rear=ptr; }
  • 82.
    Insert Method void insert(intx) { node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else rear ptr { A Address of itself x ptr->next=rear->next; rear->next=ptr; } rear=ptr; }
  • 83.
    Insert Method void insert(intx) { node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else rear ptr { A Address of itself x ptr->next=rear->next; rear->next=ptr; } rear=ptr; }
  • 84.
    Insert Method void insert(intx) { node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; else rear ptr { A x ptr->next=rear->next; rear->next=ptr; } rear=ptr; }
  • 85.
    Insert Method void insert(intx) { node * ptr = new node; ptr -> data=x; if(rear==NULL) ptr -> next=ptr; rear else ptr { A x ptr->next=rear->next; rear->next=ptr; } rear=ptr; }
  • 86.
    Remove Method int remove() { if(rear==NULL) { cout<<"nlist emptyn"; return 0; rear } else A Address of itself { int temp=rear->next->data; node* ptr=rear->next; if(rear==rear->next) { rear=NULL; } else {rear->next=rear->next->next; } delete ptr; return temp; } }
  • 87.
    Remove Method int remove() { if(rear==NULL) temp=A { cout<<"nlist emptyn"; return 0; rear } else A Address of itself { int temp=rear->next->data; node* ptr=rear->next; if(rear==rear->next) { rear=NULL; } else {rear->next=rear->next->next; } delete ptr; return temp; } }
  • 88.
    Remove Method int remove() { if(rear==NULL) temp=A { cout<<"nlist emptyn"; ptr return 0; rear } else A Address of itself { int temp=rear->next->data; node* ptr=rear->next; if(rear==rear->next) { rear=NULL; } else {rear->next=rear->next->next; } delete ptr; return temp; } }
  • 89.
    Remove Method int remove() { if(rear==NULL) temp=A { cout<<"nlist emptyn"; ptr return 0; } else A Address of itself { int temp=rear->next->data; node* ptr=rear->next; rear if(rear==rear->next) { rear=NULL; } else {rear->next=rear->next->next; } delete ptr; return temp; } }
  • 90.
    Remove Method int remove() { if(rear==NULL) temp=A { cout<<"nlist emptyn"; ptr return 0; } else A Address of itself { int temp=rear->next->data; node* ptr=rear->next; rear if(rear==rear->next) { rear=NULL; } else {rear->next=rear->next->next; } delete ptr; return temp; } }
  • 91.
    Remove Method int remove() { if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else { int temp=rear->next->data; node* ptr=rear->next; rear if(rear==rear->next) { rear=NULL; A B C } else {rear->next=rear->next->next; } delete ptr; return temp; } }
  • 92.
    Remove Method int remove() { if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else temp=A { int temp=rear->next->data; node* ptr=rear->next; rear if(rear==rear->next) { rear=NULL; A B C } else {rear->next=rear->next->next; } delete ptr; return temp; } }
  • 93.
    Remove Method int remove() { if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else temp=A { int temp=rear->next->data; node* ptr=rear->next; ptr rear if(rear==rear->next) { rear=NULL; A B C } else {rear->next=rear->next->next; } delete ptr; return temp; } }
  • 94.
    Remove Method int remove() { if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else temp=A { int temp=rear->next->data; node* ptr=rear->next; ptr rear if(rear==rear->next) { rear=NULL; A B C } else {rear->next=rear->next->next; } delete ptr; return temp; } }
  • 95.
    Remove Method int remove() { if(rear==NULL) { cout<<"nlist emptyn"; return 0; } else temp=A { int temp=rear->next->data; node* ptr=rear->next; ptr rear if(rear==rear->next) { rear=NULL; A B C } else {rear->next=rear->next->next; } delete ptr; return temp; } }
  • 96.
    Display Method void display() { if(rear==NULL) { cout<<"nlist emptyn"; return; } node * temp=rear->next; do { cout<<temp->data<<" "; temp=temp->next; }while(temp!=rear->next); rear } A B C .
  • 97.
    Display Method void display() { if(rear==NULL) { cout<<"nlist emptyn"; return; } node * temp=rear->next; do { cout<<temp->data<<" "; temp=temp->next; }while(temp!=rear->next); temp rear } . A B C
  • 98.
    Display Method void display() { if(rear==NULL) { cout<<"nlist emptyn"; return; } node * temp=rear->next; do { display A cout<<temp->data<<" "; temp=temp->next; }while(temp!=rear->next); temp rear } . A B C
  • 99.
    Display Method void display() { if(rear==NULL) { cout<<"nlist emptyn"; return; } node * temp=rear->next; do { cout<<temp->data<<" "; temp=temp->next; }while(temp!=rear->next); temp rear } A B C .
  • 100.
    Display Method void display() { if(rear==NULL) { cout<<"nlist emptyn"; return; } node * temp=rear->next; do { display B cout<<temp->data<<" "; temp=temp->next; }while(temp!=rear->next); temp rear } A B C .
  • 101.
    Display Method void display() { if(rear==NULL) { cout<<"nlist emptyn"; return; } node * temp=rear->next; do { cout<<temp->data<<" "; temp=temp->next; temp }while(temp!=rear->next); rear } A B C .
  • 102.
    Display Method void display() { if(rear==NULL) { cout<<"nlist emptyn"; return; } node * temp=rear->next; do { display C cout<<temp->data<<" "; temp=temp->next; temp }while(temp!=rear->next); rear } A B C .
  • 103.
    Search Method void search(intx) { int a=0; node * temp= rear->next while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=0 else { temp=temp->next; a++; rear } C } A B if(temp==NULL) cout<<“value not found"; } };
  • 104.
    Search Method void search(intx) { int a=0; node * temp=rear->next; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=0 else { temp=temp->next; temp a++; rear } C } A B if(temp==NULL) cout<<“value not found"; } };
  • 105.
    Search Method void search(intx) { int a=0; node * temp=rear->next; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=1 else { temp=temp->next; temp a++; rear } C } A B if(temp==NULL) cout<<“value not found"; } };
  • 106.
    Search Method void search(intx) { int a=0; node * temp=rear->next; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=2 else { temp=temp->next; temp a++; rear } C } A B if(temp==NULL) cout<<“value not found"; } };
  • 107.
    Search Method void search(intx) { int a=0; node * temp=rear->next; while(temp!=NULL) { if(temp->data==x) { cout<<"found at index "<<a; break; x=C } a=2 else { temp=temp->next; temp a++; rear } C } A B found at if(temp==NULL) index 2 cout<<“value not found"; } };
  • 108.
    Main Method case 2: void main() y=q.pop(); {clrscr(); if(y!=NULL) int n, a, y, z; cout<<"nvalue popped is: "<<y; int e=0; break; circularQueue q; case 3: do q.display(); {cout<<"npress 1 to push"; break; cout<<"npress 2 to pop"; case 4: cout<<"npress 3 to display"; cout<<“enter value to search?”; cout<<“n press 4 to search”; cin>>z; cout<<"npress 5 to exitn"; q.search(z); cin>>n; break; switch(n) case 5: { e=1; case 1: break; cout<<"enter a number"; } cin>>a; } while(e==0); q.push(a); } break;
  • 109.
    TIME COMPLEXITY  Insert ◦ Best Case: O(1) ◦ Worst Case: O(1)  Remove ◦ Best Case: O(1) ◦ Worst Case: O(1)  Search ◦ Best Case: O(1) ◦ Worst Case: O(n)
  • 110.