# data structures part-2

Document Sample

```					Data Structures

UNIT 2

sumalatha
Topics
• Representation of stack using Linked List
• Representation of Queue using Linked List
sumalatha

sumalatha
• List: Set of elements, organized in sequential manner.
Ex: array
• In array the elements are stored in continuous memory
locations.     0 1    2   3   4
30   40   54   22   11

2000 2001 2002 2003 2004

• Each element is accessed through its index number

sumalatha
Arrays: pluses and minuses
+ Fast element access.
-- Impossible to resize.

• Many applications require resizing!
• Required size not always immediately
available.

sumalatha
• A Linked List is a linear collection of data elements,
called nodes where each node is divided into two parts.
• The first part called as the data part, contains the
information of the element.
of next node.

sumalatha
• A Linked List is a linear collection of data elements,
called nodes where each node is divided into two parts.
• The first part called as the data part, contains the
information of the element.
of next node.

node1           node2         node3        node4

info              info          info          info

• Ex:
30 8000         40     NULL
20     3000

2000                 3000                 8000
sumalatha
Linked List : Pluses and Minuses
++ Dynamic data structure
The linked list can grow or shrink in size during the
execution of a program.
++ Flexibility to rearranging the elements
Thus it is easier to perform insertion and deletion
--Element access is slow
-- Requires more storage

sumalatha
• A node’s successor is the next node in the sequence
– The last node has no successor
• A node’s predecessor is the previous node in the
sequence
– The first node has no predecessor
• A list’s length is the number of elements in it
– A list may be empty (contain no elements)

sumalatha

sumalatha
1.   Creating a list
2.   Inserting an item
3.   Deleting an item
4.   Traversing the list
5.   Looking up an item
6.   Counting the items in the list
7.   Concatenating two lists

sumalatha
• A singly linked list is a
concrete data structure
consisting of a sequence               elem         next
of nodes
• Each node stores                         node
– element
– link to the next node

A               B                 C             D   NULL

sumalatha
pointer
always points to starting
node of a list.

A            B               C    D   NULL

sumalatha
Creating a node in SLL
Procedure:
• Define a node.
• Allocate the memory for node dynamically.

sumalatha
Self-Referential Structures

• Self-referential structures
– Structure that contains a pointer to a structure of the same type

• Diagram of two self-referential structure objects linked together

15                              10

Data member                   NULL pointer (points to nothing)
and pointer

Defining a node:          nextPtr
struct node
{                            • Points to an object of type node
int data;                  • Referred to as a link
struct node *nextPtr;       • Ties one node to another node
}
sumalatha
Node creation in Singly linked lists
Node Structure                                    2000
struct node                                        newnode
{
int data;
struct node *next;                   data              next
};

10           NULL

Creating a node                       2000

struct node *newnode;
newnode = (struct node*) malloc (sizeof(struct node));
newnode -> data = 10;
newnode -> next = NULL;

sumalatha
Operations on SLL

 Insertion:

1. Inserting a node at beginning.
2. Inserting a node at the end of list.
3. Inserting a node at required position in the list

Deletion:

1. Deleting first node .
2. Deleting last node .
3. Deleting a required node from the list

Traversing

sumalatha
Inserting a node at beginning
1. Create a node , which is to be inserted
2. If head = NULL /* check for the list is empty or not */
3. Else
to link field of new node thus new node points to first node*/

node*/

sumalatha
Inserting a node at the beginning
Create a node that is to be inserted
2500
data newnode next                       else
{
10        NULL
}
If the list is empty

NULL
If the list is not empty
1200
newnode
5   1300        5   1330    4   1400   8 NULL
10 NULL                 1200   sumalatha1300       1330        1400
2500
Inserting a node at end of the list
1. Create a node , which is to be inserted
2. If head = NULL /*check for list is empty or not */
3. Else
temp  head /* create one more pointer*/
While( temp->next != NULL)
temp  temp->next /* until this pointer
reaches to end of the list increment pointer*/
4. Temp->next  newnode                /* assign the
sumalatha
Inserting a node at end of the list
Create a node that is to be inserted
2500
data newnode next                            else
{
10        NULL
2500                                          While(temp->next!=NULL)
temp=temp->next;
If the list is empty

NULL
If the list is not empty
1200
5   1300      5   1330     4   1400   8 NULL
1200          1200         1300         1330
sumalatha    1400
temp
Inserting a node at end of the list
Create a node that is to be inserted
2500
data newnode next                            else
{
10        NULL
While(temp->next!=NULL)
temp=temp->next;
If the list is empty

NULL                                     2500
If the list is not empty
1200
5   1300      5   1330     4   1400   8 NULL
1300          1200         1300         1330
sumalatha    1400
temp
Inserting a node at end of the list
Create a node that is to be inserted
2500
data newnode next                            else
{
10        NULL
While(temp->next!=NULL)
temp=temp->next;
If the list is empty

NULL                                     2500
If the list is not empty
1200
1300 temp
5   1300         5   1330     4   1400   8 NULL
1200            1300         1330
sumalatha    1400
Inserting a node at end of the list
Create a node that is to be inserted
2500
data newnode next                            else
{
10        NULL
While(temp->next!=NULL)
temp=temp->next;
If the list is empty

NULL                                     2500
If the list is not empty
1200
1330 temp
5   1300         5   1330     4   1400   8 NULL
1200            1300         1330
sumalatha    1400
Inserting a node at end of the list
Create a node that is to be inserted
2500
data newnode next                            else
{
10        NULL
While(temp->next!=NULL)
temp=temp->next;
If the list is empty

NULL                                     2500
If the list is not empty
1200
1330 temp
5   1300         5   1330     4   1400   8 NULL
1200            1300         1330
sumalatha    1400
Inserting a node at end of list
Create a node that is to be inserted
2500
data newnode next                            else
{
10        NULL
While(temp->next!=NULL)
temp=temp->next;
If the list is empty

NULL                                     2500
If the list is not empty
1200
1400 temp
5   1300         5   1330     4   1400   8 NULL
1200            1300         1330
sumalatha    1400
Inserting a node at end of the list
Create a node that is to be inserted
2500
data newnode next                            else
{
10        NULL
While(temp->next!=NULL)
temp=temp->next;
If the list is empty                         temp -> next = newnode;
}
NULL                                     2500
If the list is not empty
1200
1400 temp
5   1300         5   1330     4   1400     8 NULL
1200            1300         1330
sumalatha     1400
Inserting a node at end of the list
Create a node that is to be inserted
2500
data newnode next                            else
{
10        NULL
While(temp->next!=NULL)
temp=temp->next;
If the list is empty                         temp -> next = newnode;
}
NULL                                     2500                   new node

If the list is not empty                                         10      NULL
1200
1400 temp         2500
5   1300         5   1330     4   1400     8 NULL
1200            1300         1330
sumalatha     1400
Inserting a node at given position
1. Create a node , which is to be inserted
2. If head = NULL /*check for list is empty or not */
3. Else temp  head /* create one more pointer*/
for i= 1 to pos-1
temp  temp->next /* until i reaches to required
position of the list increment temp pointer*/
4. Newnode->next  Temp->next                         /* assign the next
5. Temp->next  newnode                     /* assign new node address

sumalatha
Inserting a node at given position in the list
If the list is not empty                          else{
Printf(“enter position where
u want insert”);
for(i=1;i<pos-1;i++)
1200                                              temp=temp->next;
newnode->next=temp->next;
temp->next=newnode;
1         2                    3           4

5   1300    5   1330          4    1400          8 NULL
1200          1300              1330                1400
pos
1200                                                                       3
temp              2500               NULL
10
newnode                                             i    1
2500

sumalatha
Deleting first node in SLL
print “list is empty”; return;
2. Else
if( head->next=NULL)/* if the list contains only

3. Else

sumalatha
Deleting first node in SLL
If list contains only one node                                    printf(“list is empty”);
else
10 NULL                               else
1200

1200

5   1300           5   1330         4   1400      8 NULL
1200            1300               1330           1400

sumalatha
Deleting last node in SLL
print “list is empty”; return;
2.   else
if( head->next=NULL)/* if the list contains only one node */
3.   else
while(temp->next->next!=NULL)
temp  temp->next
4.   temp->next  NULL

sumalatha
Deleting last node in SLL
printf(“ list is empty”);
else
else{
while(temp->next->next!=NULL)
temp=temp->next;

1200

5   1300     5   1330         4   1400      8 NULL
1200        1300            1330           1400

1200
temp                           sumalatha
Deleting last node in SLL
printf(“ list is empty”);
else
else{
while(temp->next->next!=NULL)
temp=temp->next;

1200

5   1300     5   1330         4   1400      8 NULL
1200        1300            1330           1400

1300 temp
sumalatha
Deleting last node in SLL
printf(“ list is empty”);
else
else{
while(temp->next->next!=NULL)
temp=temp->next;
temp->next=NULL;
}
1200

5   1300     5   1330         4   1400      8 NULL
1200        1300            1330           1400

1330 temp
sumalatha
Deleting selected node in SLL
print “list is empty”; return;
2.    else
if( head->next=NULL)/* if the list contains only one node */
3.    else
for i= 1 to pos-1
temp  temp->next                /* until i reaches to required
position of the list increment temp pointer*/
4.    temp->next  temp->next->next

sumalatha
Delete a selected node in SLL
printf(“ list is empty”);
else
1200                                              for(i=1;i<pos-1;i++)
temp=temp->next;

1         2             3               4

5   1300    5   1330    4   1400          8 NULL
1200           1300        1330               1400
pos

1200                                                                            3
temp
i   1

sumalatha
Delete a selected node in SLL
printf(“ list is empty”);
else
1200                                               for(i=1;i<pos-1;i++)
temp=temp->next;
temp->next=temp->next->next;
1            2             3           } 4
5    1300       5   1330    4   1400          8 NULL
1200            1300        1330               1400
pos

1200 temp                                                     3

i         1

sumalatha
Traversing SLL (C code)
printf (“ List is empty” ); return;
else
{
while(temp->next!=NULL)
{      printf(“%d” , temp->data)
temp=temp->next;
}
If(temp->next==NULL)
printf(“%d” ,temp->data);
}
/*insertion at end*/
/* insertion at beginning*/      If (head==NULL)
else                             else
{                                {
}                                   temp=temp->next;
temp -> next = newnode;
/* insertion at given            }
posion*/
insert()
{
else{
Printf(“enter position where
else
u want insert”);
Printf(“enter the place where u want insert”);
scanf(“%d” ,&pos);
Printf(“1.begining\n2.end\n3.given location”);
for(i=1;i<pos-1;i++)
scanf(“%d” ,&ch);
temp=temp->next;
if(ch==1) {      };
newnode->next=temp->next;
if(ch==2) {     }; if(ch==3) { };
temp->next=newnode;
/* deleting last node*/
/* deleting first node*/            If (head==NULL)
If (head==NULL)                        printf(“ list is empty”);
printf(“list is empty”);       else
temp=temp->next;
/*deleting selected node*/             temp->next=NULL;
}
printf(“ list is empty”);
Deletion()
else
printf(“ list is empty”);
else
else{
for(i=1;i<pos-1;i++)
else
temp=temp->next;
{
temp->next=temp->next->next;
printf(“ enter the place to delete an item”);
}
printf(“1.first\n2.last\n3.given location”);
scanf(“%d” ,&ch);
# include<stdio.h>
switch(ch)
# include<malloc.h>
{
#include<stdio.h>
int ch,i,pos,item;
case 1: insert();
create();
break;
insert();
case 2: deletion();
deletion();
break;
traverse();
case 3: traverse();
struct Node
break;
{
case 4:exit();
int data;
}} }
struct Node *next;
{ printf("enter the item to be
main()                                               inserted:");
{                                                    scanf("%d",&item);
printf("\n 1.Insertion\n 2.Deletion\n 3.Traverse\n   new->data=item;
4.exit”);                                            New->next=NULL;
while(1)                                                 }
{                                                             Insert()
printf("enter the choice:");                                  {
scanf("%d",&ch);                                                 create();
………… }

Doubly Linked List is a collection of nodes in which each node consists 3 parts
One part contains information
One part is used to store the address of previous (processor ) node.
One more part is used to store the address of next (successor) node.

prev data next

Node in DLL

head           NULL   a                b                 c   NULL

prev data next       prev data next   prev    data next

NULL   10 2300        2100 20 2800     2300 30      NULL

2100                      2300             2800

Node Structure:
struct DNode
{
int data;
struct DNode *prev;
struct DNode *next;
};
Node creation in Doubly linked lists
Node Structure                                  2000
struct DNode                                    newnode
{
int data;
struct node *prev,*next;           prev data        next
};

NULL     10      NULL

Creating a node                      2000

struct DNode *newnode;
newnode = (struct DNode*) malloc (sizeof(struct DNode));
newnode -> data = 10;
newnode -> next = NULL;
newnode -> prev = NULL;
sumalatha
Operations on DLL

 Insertion:

1. Inserting a node at beginning.
2. Inserting a node at the end of list.
3. Inserting a node at required position in the list

Deletion:

1. Deleting first node .
2. Deleting last node .
3. Deleting a required node from the list

Traversing:

Forward Direction
Backward Direction
sumalatha
Inserting at beginning in DLL

a                    b   c

x
new
Inserting a node at beginning in DLL

1. Create a node , which is to be inserted
2. If head = NULL /* check for the list is empty or not */
3. Else
to link field of new node */

node*/

sumalatha
Inserting at end in DLL

a              b    c

x

new
Inserting a node at end of the list
1. Create a node , which is to be inserted
2. If head = NULL /*check for list is empty or not */
3. Else
temp  head /* create one more pointer*/
While( temp->next != NULL)
temp  temp->next /* until this pointer reaches to end
of the list increment pointer*/
4. Temp->next  newnode                      /* assign the address of new
node in the last node link field */
5. Newnode->prev  temp
sumalatha
Inserting at given position in DLL

a                  b           c

x
new
Inserting a node at given position
1. Create a node , which is to be inserted
2. If head = NULL /*check for list is empty or not */
3. Else temp  head /* create one more pointer*/
for i= 1 to pos-1
temp  temp->next /* until i reaches to required
position of the list increment temp pointer*/
4. Newnode->next  Temp->next
5.    Temp->next->prev newnode
6.    Temp->next  newnode
7.    Newnode->prev  temp

sumalatha
Deleting First Node in DLL

a            b        c
Deleting first node in DLL
print “list is empty”; return;
2. Else
if( head->next=NULL)/* if the list contains only

3. Else
sumalatha
Deleting Last Node in DLL

a            b            c
Deleting last node in DLL
print “list is empty” return
2.   else
if( head->next=NULL)/* if the list contains only one node */
3.   else
while(temp->next->next!=NULL)
temp  temp->next
4.    temp->next  NULL

sumalatha
Deleting a given node from a DLL
• Node deletion from a DLL involves changing two links
• In this example,we will delete node b

a                b         c

sumalatha
Deleting selected node in DLL
print “list is empty”; return;
2.    else
if( head->next=NULL)/* if the list contains only one node */
3.    else
for i= 1 to pos-1
temp  temp->next                /* until i reaches to required
position of the list increment temp pointer*/
4.    temp->next  temp->next->next
5.    temp->next->prev  temp

sumalatha
Traversing DLL (Forward Direction)
(C code)
printf (“ List is empty” ); return;
else
{
while(temp->next!=NULL)
{      printf(“%d” , temp->data)
temp=temp->next;
}
If(temp->next==NULL)
printf(“%d” ,temp->data);
}
Traversing DLL (Reverse Direction)
(C code)
printf (“ List is empty” ); return;
else
{
while(temp->next!=NULL)
temp=temp->next;
while(temp->prev!=NULL)
{      printf(“%d” , temp->data)
temp=temp->prev;
}
if(temp->prev==NULL)
printf(“%d” ,temp->data);
}
DLLs compared to SLLs
• Advantages:                       – Requires more space
– Can be traversed in             – List manipulations are
either direction (may             slower (because more
be essential for some             links must be
programs)                         changed)
– Greater chance of
having bugs (because
manipulated)

sumalatha
It is just a singly linked list in which the link field of the last node contains the

Thus a circular linked list has no end.

Therefore, it is necessary to establish the FIRST and LAST pointers.
The FIRST pointer points to first node of the list
The LAST pointer points to last node in the list

FIRST                                                                     LAST

10                    20                    30

sumalatha
Node creation in CLL
Node Structure:

struct cnode
{
int data;
struct node *next;
} ;

Struct cnode *first =NULL;
Struct cnode *last=NULL;

Struct cnode *newnode;
newnode = (struct cnode*) malloc (sizeof(struct cnode));
newnode -> data = 10;
newnode -> next = NULL;
Inserting a node at beginning
1.     Create a node , which is to be inserted
2.    If first = NULL /* check for the list is empty or not */
newnode->next  newnode
last  newnode

3. Else
of new node thus new node points to first node*/
4.    first  newnode          /* now head points to newly inserted node*/
5.    last->next  first

sumalatha
Inserting a node at end of the list
1. Create a node , which is to be inserted
2. If first = NULL /*check for list is empty or not */
newnode->next  newnode
first  newnode /* assign new node address to
last  newnode
3. Else
last->next=newnode
last=newnode
last->next=first

sumalatha
Deleting first node in CLL
1. If first=NULL
print “list is empty”; return;
2. Else
if( first->next=NULL)/* if the list contains only
firstNULL                                one node */
last  NULL

3. Else
first  first->next
last->next=first
sumalatha
Deleting last node in CLL
1.   If first=NULL
print “list is empty”; return;
2.   else
if( first->next=NULL)/* if the list contains only one node */
firstNULL
last  NULL
3.   else
temp first
while(temp->next!=last)
temp  temp->next
temp->next  first
last=temp

sumalatha
• The node in the linked list structure is
struct node
{
int data;
};

TOP

4

6

Stack Before Invoking Push and Pop
TOP

2

4                                TOP

6
2

Stack After Push Operation
4

6

Stack After Pop Operation

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 9 posted: 10/3/2012 language: English pages: 70
How are you planning on using Docstoc?