SlideShare une entreprise Scribd logo
1  sur  16
Advanced data structure using C++
Assignment-3
Q1:-Differenciate between dequeue and priority queues?
ANS:-Dequeue:-
 A Double ended queue
 At a post office, when it is your turn, you need to fill up a form, step aside and get
served when finished.
 Has operations that
 Add, remove, or retrieve entries
 At both its front and back
 Combines and expands the operations of queue and stack
 public interface DequeInterface
{ public void addToFront(Object newEntry);
public void addToBack(Object newEntry);
public Object removeFront();
public Object removeBack();
public Object getFront();
public Object getBack();
public boolean isEmpty();
public void clear();
} // end DequeInterface
Priority queue:-
 Organizes objects according to priorities
 Contrast to regular queue in order of arrival
 Priority queue example – a hospital ER: assign a priority number to each patient to override the
arrival time.
 Priority can be specified by an integer
 Must define whether high number is high priority or …
 Low number (say 0) is high priority
 Other objects can specify priority
Object must have a compareTo method
public interface PriorityQueueInterface
{ public void add(Comparable newEntry);
public Comparable remove();
public Comparable get();
public boolean isEmpty();
public int getSize();
public void clear();
} // end PriorityQueueInterface
Q2:-Expalin Dequeue and its application?
ANS:- Dequeue
The dequeue function dequeues a packet for sending. It returns the next packet that needs to be sent
out on the output interface. This packet is determined by the scheduler in the queuing discipline. The
scheduler can be very complicated for complex queuing disciplines like the CBQ. At the same time, it can
be very simple too, as in the case of a FIFO queue. The dequeue function for a simple FIFO queuing
discipline (in net/sched/sch_fifo.c) is shown below:
static struct sk_buff *
pfifo_dequeue(struct Qdisc* sch)
{
return __skb_dequeue(&sch->q);
}
As shown in this example, the next packet in the queue is dequeued and returned, which is desired
behavior from a simple FIFO.
The dequeue function for a priority FIFO is shown next.
static struct sk_buff *
prio_dequeue(struct Qdisc* sch)
{
.
.
.
for (prio = 0; prio < q->bands;prio++) {
qdisc = q->queues[prio];
skb = qdisc->dequeue(qdisc);
if (skb) {
sch->q.qlen--;
return skb;
}
}
.
.
.
}
As shown in this example, whenever the prio_dequeue function is called, packets from the highest
priority queue are sent first. After all the packets in the highest priority level are sent, packets from the
next priority level are dequeued. This required behavior is provided by the portion of the code given
above.
Having discussed the dequeue function of the queuing disciplines, let us now see the places where the
dequeue function is invoked. Whenever a packet is enqueued in dev_queue_xmit, the qdisc_wakeup
function (in include/net/pkt_sched.h) is invoked in an attempt to send the packet that was just
enqueued. qdisc_wakeup invokes the qdisc_restart function (in net/sched/sch_generic.c), which invokes
the dequeue function of the queuing discipline attached to the device. The dequeue function returns
the next packet that needs to be sent out on the interface. qdisc_restart then invokes hard_start_xmit
of the device to send the packet down to the device. If hard_start_xmit fails for some reason, the packet
is requeued in the queuing discipline. The requeue function is discussed in a later section.
qdisc_wakeup can also be invoked from the watchdog timer handlers in the CBQ, TBF and CSZ
schedulers. In the dequeue function of these queuing disciplines, when a packet is dequeued to be sent
on the output interface, a watchdog timer is initiated. If for some reason, qdisc_restart does not send
the packet out in time, the watchdog timer will go off and qdisc_restart is called. For example, the
setting of the TBF watchdog timer in tbf_dequeue (in net/sched/sch_tbf.c) is shown below:
static struct sk_buff *
tbf_dequeue(struct Qdisc* sch)
{
.
.
.
if (!sch->dev->tbusy) {
.
.
del_timer(&q->wd_timer);
q->wd_timer.expires = jiffies + delay;
add_timer(&q->wd_timer);
}
.
.
.
This example shows the way the watchdog timer is set.
Aplications:-
 The A-steal algorithm implements task scheduling for several processors.
 The processor gets the first element from the deque.
 When one of the processor completes execution of its own threads it can steal a thread of
processor.
 It gets the last element from the deque of another processor and executes it.
Q4:-Differenciate between DFS and BFS?
ANS:- Breadth First Search (also known as BFS) is a search method used to broaden all the nodes of a
particular graph. It accomplishes this task by searching every single solution in order to examine and
expand these nodes (or a combination of sequences therein). As such, a BFS does not use a heuristic
algorithm (or an algorithm that searches for a solution through multiple scenarios). After all the nodes
are obtained, they are added to a queue known as the First In, First Out queue. Those nodes that have
not been explored are ‘stored’ in a container marked ‘open’; once explored they are transported to a
container marked ‘closed’.
Depth First Search (also known as DFS) is a search method that burrows deeper into a child node of a
search until a goal is reached (or until there is a node without any other permutations or ‘children’).
After one goal is found, the search backtracks to a previous node that has gone with a solution,
repeating the process until all the nodes have been successfully searched. As such, nodes continue to be
put aside for further exploration – this is called non-recursive implementation.
The features of the BFS are space and time complexity, completeness, proof of completeness, and
optimality. Space complexity refers to the proportion of the number of nodes at the deepest level of a
search. Time complexity refers to the actual amount of ‘time’ used for considering every path a node
will take in a search. Completeness is, essentially, a search that finds a solution in a graph regardless of
what kind of graph it is. The proof of the completeness is the shallowest level at which a goal is found in
a node at a definite depth. Finally, optimality refers to a BFS that is not weighted – that is a graph used
for unit-step cost.
A DFS is the most natural output using a spanning tree – which is a tree made up of all vertices and some
edges in an undirected graph. In this formation, the graph is divided into three classes: Forward edges,
pointing from a node to a child node; back edges, pointing from a node to an earlier node; and cross
edges, which do not do either one of these.
1. bfs uses queue implementation ie.FIFO
dfs uses stack implementation ie. LIFO
2. dfs is faster than bfs
3. dfs requires less memory than bfs
4. dfs are used to perform recursive procedures.
Q5:- How can we convert a general tree into binary tree?
ANS:- General Trees and Conversion to Binary Trees
General trees are those in which the number of subtrees for any node is not
required to be 0, 1, or 2. The tree may be highly structured and therefore
have 3 subtrees per node in which case it is called a ternary tree.
However, it is often the case that the number of subtrees for any node may
be variable. Some nodes may have 1 or no subtrees, others may have 3, some
4, or any other combination. The ternary tree is just a special case of a
general tree (as is true of the binary tree).
General trees can be represented as ADT's in whatever form they exist.
However, there are some substantial problems. First, the number of references
for each node must be equal to the maximum that will be used in the tree.
Obviously, some real problems are presented when another subtree is added to
a node which already has the maximum number attached to it. It is also
obvious that most of the algorithms for searching, traversing, adding and
deleting nodes become much more complex in that they must now cope with
situations where there are not just two possibilities for any node but
multiple possibilities. It is also possible to represent a general tree in
a graph data structure (to be discussed later) but many of the advantages of
the tree processes are lost.
Fortunately, general trees can be converted to binary trees. They don't
often end up being well formed or full, but the advantages accrue from
being able to use the algorithms for processing that are used for binary
trees with minor modifications. Therefore, each node requires only two
references but these are not designated as left or right. Instead they are
designated as the reference to the first child and the reference to next
sibling. Therefore the usual left pointer really points to the first child
of the node and the usual right pointer points to the next sibling of the
node. One obvious saving in this structure is the number of fields which
must be used for references. In this way, moving right from a node accesses
the siblings of the node ( that is all of those nodes on the same level as
the node in the general tree). Moving left and then right accesses all of
the children of the node (that is the nodes on the next level of the general
tree).
Creating a Binary Tree from a General Tree
Since the references now access either the first child or successive siblings,
the process must use this type of information rather than magnitude as was
the case for the binary search tree. Note that the resulting tree is a
binary tree but not a binary search tree.
The process of converting the general tree to a binary tree is as follows:
* use the root of the general tree as the root of the binary tree
* determine the first child of the root. This is the leftmost node in the
general tree at the next level
* insert this node. The child reference of the parent node refers to this
node
* continue finding the first child of each parent node and insert it below
the parent node with the child reference of the parent to this node.
* when no more first children exist in the path just used, move back to the
parent of the last node entered and repeat the above process. In other
words, determine the first sibling of the last node entered.
* complete the tree for all nodes. In order to locate where the node fits
you must search for the first child at that level and then follow the
sibling references to a nil where the next sibling can be inserted. The
children of any sibling node can be inserted by locating the parent and then
inserting the first child. Then the above process is repeated.
Given the following general tree:
A
B C D
K H I J E F
G
The following is the binary version:
Traversing the Tree
Since the general tree has now been represented as a binary tree the
algorithms which were used for the binary tree can now be used for the
general tree (which is actually a binary tree). In-order traversals make no
sense when a general tree is converted to a binary tree. In the general
tree each node can have more than two children so trying to insert the
parent node in between the children is rather difficult, especially if there
are an odd number of children.
Pre-order
This is a process where the root is accessed and processed and then each of
the subtrees is preorder processed. It is also called a depth-first
traversal. With the proper algorithm which prints the contents of the nodes
in the traversal it is possible to obtain the original general tree. The
algorithm has the following general steps:
A
B
K C
H
I
J
D
E
F
G
* process the root node and move left to the first child.
* each time the reference moves to a new child node, the print should be
indented one tab stop and then the node processed
* when no more first children remain then the processing involves the right
sub-tree of the parent node. This is indicated by the nil reference to
another first child but a usable reference to siblings. Therefore the first
sibling is accessed and processed.
* if this node has any children they must be processed before moving on to
other siblings. Therefore the number of tabs is increased by one and the
siblings processed. If there are no children the processing continues
through the siblings.
* each time a sibling list is exhausted and a new node is accessed the
number of tab stops is decreased by one.
In this way the resulting printout has all nodes at any given level starting
in the same tab column. It is relatively easy to draw lines to produce the
original general tree except that the tree is on its side with it's root at
the left rather than with the root at the top. The result of doing this traversal is
shown below.
A
B
K
C
H
D
I
E
J
F
G
B-Trees
A B-Tree is a tree in which each node may have multiple children and
multiple keys. It is specially designed to allow efficient searching for
keys. Like a binary search tree each key has the property that all keys to
the left are lower and all keys to the right are greater. Looking at a
binary search tree :
20
10
5 15
from position 10 in the tree all keys to the left are less than 10 and all
keys to the right are greater than 10 and less than 20. So, in fact, the key
in a given node represents an upper or lower bound on the sets of keys below
it in the tree. A tree may also have nodes with several ordered keys. For
example, if each node can have three keys, then it will also have four
references. Consider the node below:
: 20 : 40 : 60 :
/ | | 
In this node (:20:40:60:) the reference to the left of 20 refers to nodes with
keys less than 20, the reference between 20 & 40 refers to nodes with keys
from 21 to 39, the reference between keys 40 & 60 to nodes with keys between
41 and 59, and finally the reference to the right of 60 refers to nodes with
keys with values greater than 61.
This is the organisational basis of the B-Tree. For m references there must
be (m-1) keys in a given node. Typically a B-tree is specified in terms of
the maximum number of successors that a given node may have. This is also
equivalent to the number of references that may occupy a single node, also
called the order of the tree. This definition of order is chosen because it
makes most of the explanations simpler. However, some texts define order as
the number of keys and therefore the number of references is m + 1. You
should keep this in mind when (or if) you refer to these texts. If the node
shown above is full then it belongs to an order 4 B-tree. Several other
constraints are also placed upon the nodes of a B-tree:
Constraints
-----------
* For an order m B-tree no node has more than m subtrees
* Every node except the root and the leaves must have at least m/2 subtrees
* A leaf node must have at least m/2 -1 keys
* The root has 0 or >= 2 subtrees
* Terminal or leaf nodes are all at the same depth.
* Within a node, the keys are in ascending order
Putting these constraints on the tree results in the tree being built
differently than a binary search tree. The binary search tree is
constructed starting at the root and working toward the leaves. A B-tree is
constructed from the leaves and as it grows the tree is pushed upward. An
example showing the process of constructing a B-tree may be the easiest way
to understand the implications of the constraints and the structure of the
tree.
The tree will be of order 4 therefore each node can hold a maximum of 3 keys.
The keys are always kept in ascending order within a node. Because the tree
is of order 4, every node except the root and leaves must have at least 2
subtrees ( or one key which has a pointer to a node containing keys which are
less than the key in the parent node and a pointer to a node containing
key(s) which are greater than the key in the parent node). This essentially
defines a minimum number of keys which must exist within any given node.
If random data are used for the insertions into the B-tree, it generally
will be within a level of minimum height. However, as the data become
ordered the B-tree degenerates. The worst case is for data which is sorted
in which case an order 4 B-tree becomes an order 2 tree or a binary search
tree. This obviously results in much wasted space and a substantial loss of
search efficiency.
Deletions from B-trees
Deletions also must be done from the leaves. Some deletions are relatively
simple because we just remove some key from the leaf and there are still enough
keys in the leaf so that there are (m/2-1) keys in total.
The removal of keys from the leaves can occur under two circumstances -- when
the key actually exists in the leaf of the tree, and when the key exists in an
internal leaf and must be moved to a leaf by determining which leaf position
contains the key closest to the one to be removed. The deletion of a single
key which does not result in a leaf which does not have enough keys is normally
referred to as deletion.
When the removal of a key from the leaf results in a leaf (or when this occurs
recursively an internal node with insufficient keys) then the process which
will be tried first is redistribution. If the keys among three nodes can be
redistributed so that all of them meet the minimum, then this will be done.
When redistribution does not work because there are not enough keys to
redistribute, then three nodes will have to be made into two nodes through
concatenation. This is the reverse of splitting. It may also recur
recursively through the tree.
Efficiency of B-Trees
---------------------
Just as the height of a binary tree related to the number of nodes through
log2 so the height of a B-Tree is related through the log m where m is the
order of the tree:
height = logm n + 1
This relationship enables a B-Tree to hold vast amounts of data in just a
few levels. For example, if the B-tree is of order 10, then level 0 can
hold 9 pieces of data, level 1 can hold 10 nodes each with 9 pieces of data,
or 90 pieces of data. Level 2 can hold 10 times 10 nodes (100), each with
9 pieces of data for a total of 900. Thus the three levels hold a total of
999. Searches can become very efficient because the number of nodes to be
examined is reduced a factor of 10 times at each probe. Unfortunately,
there is still some price to pay because each node can contain 9 pieces of
data and therefore, in the worst case, all 9 keys would have to be searched
in each node. Thus finding the node is of order log (base m) n but the
total search is m-1 logm n. If the order of the tree is small there
are still a substantial number of searches in the worst case. However if m
is large, then the efficiency of the search is enhanced. Since the data are
in order within any given node, a binary search can be used in the node.
However, this is not of much value unless the order is large since a simple
linear search may be almost as efficient for short lists.
It should be clear that although the path length to a node may be very
short, examining a node for a key can involve considerable searching within
the node. Because of this the B-Tree structure is used with very large data
sets which cannot easily be stored in main memory. The tree actually resides
on disk. If a node is stored so that it occupies just one disk block then it
can be read in with one read operation. Hence main memory can be used for
fast searching within a node and only one disk access is required for each
level in the tree. In this way the B-Tree structure minimises the number of
disk accesses that must be made to find a given key.
Q6:-Explain doubly linked list with its Implementation?
ANS:- Overall Structure of Doubly-Linked Lists
If you wish to traverse a list both forwards and backwards efficiently, or if you wish, given a list element,
to determine the preceding and following elements quickly, then the doubly-linked listcomes in handy. A
list element contains the data plus pointers to the next and previous list items as shown in the picture
below.
Of course we need a pointer to some link in the doubly-linked list to access list elements. It is convenient
for doubly-linked lists to use a list header, or head, that has the same structure as any other list item and
is actually part of the list data structure. The picture below shows an empty and nonempty doubly-
linked list. By following arrows it is easy to go from the list header to the first and last list element,
respectively.
Insertion and removal of an element in a doubly-linked list is in this implementation rather easy. In the
picture below we illustrate the pointer changes for a removal of a list item (old pointers have been
drawn solid and new pointers are dashed arrows). We first locate the previous list item using
the previous field. We make the next field of this list item point to the item following the one in cursor
position pos. Then we make the previous field of this following item point to the item preceding the one
in the cursor position pos. The list item pointed to by the cursor becomes useless and should be
automatically garbage collected.
In a pseudo-programming language we could write the following code:
remove(cursor pos)
begin
if empty list then
ERROR("empty list.")
else if cursor points at list header then
ERROR("cannot remove the list header")
else
pos previous next = pos next;
pos next previous = pos previous;
endif
end
Implementation:-
#include <iostream>
#include <string>
#include <iomanip>
#include <stdio.h>
using namespace std;
//template <class Object>
//template <class Object>
class Node
{
//friend ostream& operator<<(ostream& os, const Node&c);
public:
Node( int d=0);
void print(){
cout<<this->data<<endl;
}
//private:
Node* next;
Node* prev;
int data;
friend class LinkList;
};
Node::Node(int d):data(d)
{
}
//template <class Object>
class LinkList
{
void create();
public:
//LinkList();
LinkList():head(NULL),tail(NULL),current(NULL)
{create();}
int base;
//LinkList(const LinkList & rhs, const LinkList & lhs);
~LinkList(){delete current;}
const Node& front() const;//element at current
const Node& back() const;//element following current
void move();
void insert (const Node & a);//add after current
void remove (const Node &a);
void print();
private:
Node* current;//current
Node* head;
Node* tail;
};
void LinkList::print()
{
Node *nodePt =head->next;
while(nodePt != head)
{
cout<<"print function"<<endl;
cout<<nodePt->data<<endl;
nodePt=nodePt->next;
}
}
//element at current
void LinkList::create()
{
current = head = tail = new Node(0);
current->next = current->prev = current;
}
const Node& LinkList::back()const
{
return *current;
}
//element after current
const Node& LinkList::front() const
{
return *current ->next;
}
void LinkList::move()
{
current = current ->next;
}
//insert after current
void LinkList :: insert(const Node& a)
{
Node* newNode= new Node();
newNode->prev=current;
newNode->next=current->next;
newNode->prev->next=newNode;
newNode->next->prev=newNode;
current=newNode;
}
void LinkList::remove(const Node& a)
{
Node* oldNode;
oldNode=current;
oldNode->prev->next=oldNode->next;
oldNode->next->prev=oldNode->prev;
delete oldNode;
}
//main file
#include <iostream>
#include <string>
#include <iomanip>
#include <stdio.h>
#include "LinkedList.h"
using namespace std;
int main()
{
int n;
cout<<"How many Nodes would you like to create"<<endl;
cin>>n;
LinkList list1 ;
list1.print();
for(int loop = 0;loop < n;++loop)
{
list1.insert(loop);
}
list1.print();
}

Contenu connexe

Tendances

Object oriented design
Object oriented designObject oriented design
Object oriented design
lykado0dles
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
Syed Faizan Hassan
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
Vince Vo
 

Tendances (20)

Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Object oriented design
Object oriented designObject oriented design
Object oriented design
 
ccc
cccccc
ccc
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
Java unit2
Java unit2Java unit2
Java unit2
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
Unit ii
Unit   iiUnit   ii
Unit ii
 
Op ps
Op psOp ps
Op ps
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 

En vedette

Data Structure
Data StructureData Structure
Data Structure
sheraz1
 

En vedette (20)

Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Structured query language constraints
Structured query language constraintsStructured query language constraints
Structured query language constraints
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
 
Pp
PpPp
Pp
 
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. PatilDiscrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
 
Working with Cookies in NodeJS
Working with Cookies in NodeJSWorking with Cookies in NodeJS
Working with Cookies in NodeJS
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
 
C++ data types
C++ data typesC++ data types
C++ data types
 
Data Structure
Data StructureData Structure
Data Structure
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structure
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structure
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
 
11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil
 

Similaire à Advanced data structures using c++ 3

Unit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docxUnit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docx
dickonsondorris
 
Exploring Algorithms
Exploring AlgorithmsExploring Algorithms
Exploring Algorithms
Sri Prasanna
 
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docxConsider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
maxinesmith73660
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in C
Jabs6
 

Similaire à Advanced data structures using c++ 3 (20)

Advance data structure
Advance data structureAdvance data structure
Advance data structure
 
UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptx
 
Unit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docxUnit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docx
 
K nearest neighbor
K nearest neighborK nearest neighbor
K nearest neighbor
 
Ai1.pdf
Ai1.pdfAi1.pdf
Ai1.pdf
 
Exploring Algorithms
Exploring AlgorithmsExploring Algorithms
Exploring Algorithms
 
Data Structure
Data StructureData Structure
Data Structure
 
Fusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with ViewsFusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with Views
 
Branch and Bound.pptx
Branch and Bound.pptxBranch and Bound.pptx
Branch and Bound.pptx
 
Parallel search
Parallel searchParallel search
Parallel search
 
Ee693 sept2014quizgt1
Ee693 sept2014quizgt1Ee693 sept2014quizgt1
Ee693 sept2014quizgt1
 
Machine Learning Algorithms (Part 1)
Machine Learning Algorithms (Part 1)Machine Learning Algorithms (Part 1)
Machine Learning Algorithms (Part 1)
 
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docxConsider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 
DS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docxDS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docx
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in C
 
3 recursion
3 recursion3 recursion
3 recursion
 
3-Recursion.ppt
3-Recursion.ppt3-Recursion.ppt
3-Recursion.ppt
 

Dernier

Dernier (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

Advanced data structures using c++ 3

  • 1. Advanced data structure using C++ Assignment-3 Q1:-Differenciate between dequeue and priority queues? ANS:-Dequeue:-  A Double ended queue  At a post office, when it is your turn, you need to fill up a form, step aside and get served when finished.  Has operations that  Add, remove, or retrieve entries  At both its front and back  Combines and expands the operations of queue and stack  public interface DequeInterface { public void addToFront(Object newEntry); public void addToBack(Object newEntry); public Object removeFront(); public Object removeBack(); public Object getFront(); public Object getBack(); public boolean isEmpty(); public void clear(); } // end DequeInterface Priority queue:-  Organizes objects according to priorities  Contrast to regular queue in order of arrival  Priority queue example – a hospital ER: assign a priority number to each patient to override the arrival time.  Priority can be specified by an integer  Must define whether high number is high priority or …  Low number (say 0) is high priority
  • 2.  Other objects can specify priority Object must have a compareTo method public interface PriorityQueueInterface { public void add(Comparable newEntry); public Comparable remove(); public Comparable get(); public boolean isEmpty(); public int getSize(); public void clear(); } // end PriorityQueueInterface Q2:-Expalin Dequeue and its application? ANS:- Dequeue The dequeue function dequeues a packet for sending. It returns the next packet that needs to be sent out on the output interface. This packet is determined by the scheduler in the queuing discipline. The scheduler can be very complicated for complex queuing disciplines like the CBQ. At the same time, it can be very simple too, as in the case of a FIFO queue. The dequeue function for a simple FIFO queuing discipline (in net/sched/sch_fifo.c) is shown below: static struct sk_buff * pfifo_dequeue(struct Qdisc* sch) { return __skb_dequeue(&sch->q); } As shown in this example, the next packet in the queue is dequeued and returned, which is desired behavior from a simple FIFO. The dequeue function for a priority FIFO is shown next. static struct sk_buff * prio_dequeue(struct Qdisc* sch) { . . . for (prio = 0; prio < q->bands;prio++) { qdisc = q->queues[prio]; skb = qdisc->dequeue(qdisc); if (skb) { sch->q.qlen--; return skb;
  • 3. } } . . . } As shown in this example, whenever the prio_dequeue function is called, packets from the highest priority queue are sent first. After all the packets in the highest priority level are sent, packets from the next priority level are dequeued. This required behavior is provided by the portion of the code given above. Having discussed the dequeue function of the queuing disciplines, let us now see the places where the dequeue function is invoked. Whenever a packet is enqueued in dev_queue_xmit, the qdisc_wakeup function (in include/net/pkt_sched.h) is invoked in an attempt to send the packet that was just enqueued. qdisc_wakeup invokes the qdisc_restart function (in net/sched/sch_generic.c), which invokes the dequeue function of the queuing discipline attached to the device. The dequeue function returns the next packet that needs to be sent out on the interface. qdisc_restart then invokes hard_start_xmit of the device to send the packet down to the device. If hard_start_xmit fails for some reason, the packet is requeued in the queuing discipline. The requeue function is discussed in a later section. qdisc_wakeup can also be invoked from the watchdog timer handlers in the CBQ, TBF and CSZ schedulers. In the dequeue function of these queuing disciplines, when a packet is dequeued to be sent on the output interface, a watchdog timer is initiated. If for some reason, qdisc_restart does not send the packet out in time, the watchdog timer will go off and qdisc_restart is called. For example, the setting of the TBF watchdog timer in tbf_dequeue (in net/sched/sch_tbf.c) is shown below: static struct sk_buff * tbf_dequeue(struct Qdisc* sch) { . . . if (!sch->dev->tbusy) { . . del_timer(&q->wd_timer); q->wd_timer.expires = jiffies + delay; add_timer(&q->wd_timer); } . . . This example shows the way the watchdog timer is set. Aplications:-
  • 4.  The A-steal algorithm implements task scheduling for several processors.  The processor gets the first element from the deque.  When one of the processor completes execution of its own threads it can steal a thread of processor.  It gets the last element from the deque of another processor and executes it. Q4:-Differenciate between DFS and BFS? ANS:- Breadth First Search (also known as BFS) is a search method used to broaden all the nodes of a particular graph. It accomplishes this task by searching every single solution in order to examine and expand these nodes (or a combination of sequences therein). As such, a BFS does not use a heuristic algorithm (or an algorithm that searches for a solution through multiple scenarios). After all the nodes are obtained, they are added to a queue known as the First In, First Out queue. Those nodes that have not been explored are ‘stored’ in a container marked ‘open’; once explored they are transported to a container marked ‘closed’. Depth First Search (also known as DFS) is a search method that burrows deeper into a child node of a search until a goal is reached (or until there is a node without any other permutations or ‘children’). After one goal is found, the search backtracks to a previous node that has gone with a solution, repeating the process until all the nodes have been successfully searched. As such, nodes continue to be put aside for further exploration – this is called non-recursive implementation. The features of the BFS are space and time complexity, completeness, proof of completeness, and optimality. Space complexity refers to the proportion of the number of nodes at the deepest level of a search. Time complexity refers to the actual amount of ‘time’ used for considering every path a node will take in a search. Completeness is, essentially, a search that finds a solution in a graph regardless of what kind of graph it is. The proof of the completeness is the shallowest level at which a goal is found in a node at a definite depth. Finally, optimality refers to a BFS that is not weighted – that is a graph used for unit-step cost. A DFS is the most natural output using a spanning tree – which is a tree made up of all vertices and some edges in an undirected graph. In this formation, the graph is divided into three classes: Forward edges, pointing from a node to a child node; back edges, pointing from a node to an earlier node; and cross edges, which do not do either one of these. 1. bfs uses queue implementation ie.FIFO dfs uses stack implementation ie. LIFO 2. dfs is faster than bfs
  • 5. 3. dfs requires less memory than bfs 4. dfs are used to perform recursive procedures. Q5:- How can we convert a general tree into binary tree? ANS:- General Trees and Conversion to Binary Trees General trees are those in which the number of subtrees for any node is not required to be 0, 1, or 2. The tree may be highly structured and therefore have 3 subtrees per node in which case it is called a ternary tree. However, it is often the case that the number of subtrees for any node may be variable. Some nodes may have 1 or no subtrees, others may have 3, some 4, or any other combination. The ternary tree is just a special case of a general tree (as is true of the binary tree). General trees can be represented as ADT's in whatever form they exist. However, there are some substantial problems. First, the number of references for each node must be equal to the maximum that will be used in the tree. Obviously, some real problems are presented when another subtree is added to a node which already has the maximum number attached to it. It is also obvious that most of the algorithms for searching, traversing, adding and deleting nodes become much more complex in that they must now cope with situations where there are not just two possibilities for any node but multiple possibilities. It is also possible to represent a general tree in a graph data structure (to be discussed later) but many of the advantages of the tree processes are lost. Fortunately, general trees can be converted to binary trees. They don't often end up being well formed or full, but the advantages accrue from being able to use the algorithms for processing that are used for binary trees with minor modifications. Therefore, each node requires only two references but these are not designated as left or right. Instead they are designated as the reference to the first child and the reference to next sibling. Therefore the usual left pointer really points to the first child of the node and the usual right pointer points to the next sibling of the node. One obvious saving in this structure is the number of fields which must be used for references. In this way, moving right from a node accesses the siblings of the node ( that is all of those nodes on the same level as the node in the general tree). Moving left and then right accesses all of the children of the node (that is the nodes on the next level of the general tree). Creating a Binary Tree from a General Tree Since the references now access either the first child or successive siblings,
  • 6. the process must use this type of information rather than magnitude as was the case for the binary search tree. Note that the resulting tree is a binary tree but not a binary search tree. The process of converting the general tree to a binary tree is as follows: * use the root of the general tree as the root of the binary tree * determine the first child of the root. This is the leftmost node in the general tree at the next level * insert this node. The child reference of the parent node refers to this node * continue finding the first child of each parent node and insert it below the parent node with the child reference of the parent to this node. * when no more first children exist in the path just used, move back to the parent of the last node entered and repeat the above process. In other words, determine the first sibling of the last node entered. * complete the tree for all nodes. In order to locate where the node fits you must search for the first child at that level and then follow the sibling references to a nil where the next sibling can be inserted. The children of any sibling node can be inserted by locating the parent and then inserting the first child. Then the above process is repeated. Given the following general tree: A B C D K H I J E F G
  • 7. The following is the binary version: Traversing the Tree Since the general tree has now been represented as a binary tree the algorithms which were used for the binary tree can now be used for the general tree (which is actually a binary tree). In-order traversals make no sense when a general tree is converted to a binary tree. In the general tree each node can have more than two children so trying to insert the parent node in between the children is rather difficult, especially if there are an odd number of children. Pre-order This is a process where the root is accessed and processed and then each of the subtrees is preorder processed. It is also called a depth-first traversal. With the proper algorithm which prints the contents of the nodes in the traversal it is possible to obtain the original general tree. The algorithm has the following general steps: A B K C H I J D E F G
  • 8. * process the root node and move left to the first child. * each time the reference moves to a new child node, the print should be indented one tab stop and then the node processed * when no more first children remain then the processing involves the right sub-tree of the parent node. This is indicated by the nil reference to another first child but a usable reference to siblings. Therefore the first sibling is accessed and processed. * if this node has any children they must be processed before moving on to other siblings. Therefore the number of tabs is increased by one and the siblings processed. If there are no children the processing continues through the siblings. * each time a sibling list is exhausted and a new node is accessed the number of tab stops is decreased by one. In this way the resulting printout has all nodes at any given level starting in the same tab column. It is relatively easy to draw lines to produce the original general tree except that the tree is on its side with it's root at the left rather than with the root at the top. The result of doing this traversal is shown below. A B K C H D I E J F G
  • 9. B-Trees A B-Tree is a tree in which each node may have multiple children and multiple keys. It is specially designed to allow efficient searching for keys. Like a binary search tree each key has the property that all keys to the left are lower and all keys to the right are greater. Looking at a binary search tree : 20 10 5 15 from position 10 in the tree all keys to the left are less than 10 and all keys to the right are greater than 10 and less than 20. So, in fact, the key in a given node represents an upper or lower bound on the sets of keys below it in the tree. A tree may also have nodes with several ordered keys. For example, if each node can have three keys, then it will also have four references. Consider the node below: : 20 : 40 : 60 : / | | In this node (:20:40:60:) the reference to the left of 20 refers to nodes with keys less than 20, the reference between 20 & 40 refers to nodes with keys from 21 to 39, the reference between keys 40 & 60 to nodes with keys between 41 and 59, and finally the reference to the right of 60 refers to nodes with keys with values greater than 61. This is the organisational basis of the B-Tree. For m references there must be (m-1) keys in a given node. Typically a B-tree is specified in terms of the maximum number of successors that a given node may have. This is also equivalent to the number of references that may occupy a single node, also called the order of the tree. This definition of order is chosen because it makes most of the explanations simpler. However, some texts define order as the number of keys and therefore the number of references is m + 1. You should keep this in mind when (or if) you refer to these texts. If the node shown above is full then it belongs to an order 4 B-tree. Several other constraints are also placed upon the nodes of a B-tree: Constraints ----------- * For an order m B-tree no node has more than m subtrees
  • 10. * Every node except the root and the leaves must have at least m/2 subtrees * A leaf node must have at least m/2 -1 keys * The root has 0 or >= 2 subtrees * Terminal or leaf nodes are all at the same depth. * Within a node, the keys are in ascending order Putting these constraints on the tree results in the tree being built differently than a binary search tree. The binary search tree is constructed starting at the root and working toward the leaves. A B-tree is constructed from the leaves and as it grows the tree is pushed upward. An example showing the process of constructing a B-tree may be the easiest way to understand the implications of the constraints and the structure of the tree. The tree will be of order 4 therefore each node can hold a maximum of 3 keys. The keys are always kept in ascending order within a node. Because the tree is of order 4, every node except the root and leaves must have at least 2 subtrees ( or one key which has a pointer to a node containing keys which are less than the key in the parent node and a pointer to a node containing key(s) which are greater than the key in the parent node). This essentially defines a minimum number of keys which must exist within any given node. If random data are used for the insertions into the B-tree, it generally will be within a level of minimum height. However, as the data become ordered the B-tree degenerates. The worst case is for data which is sorted in which case an order 4 B-tree becomes an order 2 tree or a binary search tree. This obviously results in much wasted space and a substantial loss of search efficiency. Deletions from B-trees Deletions also must be done from the leaves. Some deletions are relatively simple because we just remove some key from the leaf and there are still enough keys in the leaf so that there are (m/2-1) keys in total. The removal of keys from the leaves can occur under two circumstances -- when the key actually exists in the leaf of the tree, and when the key exists in an internal leaf and must be moved to a leaf by determining which leaf position contains the key closest to the one to be removed. The deletion of a single key which does not result in a leaf which does not have enough keys is normally referred to as deletion. When the removal of a key from the leaf results in a leaf (or when this occurs
  • 11. recursively an internal node with insufficient keys) then the process which will be tried first is redistribution. If the keys among three nodes can be redistributed so that all of them meet the minimum, then this will be done. When redistribution does not work because there are not enough keys to redistribute, then three nodes will have to be made into two nodes through concatenation. This is the reverse of splitting. It may also recur recursively through the tree. Efficiency of B-Trees --------------------- Just as the height of a binary tree related to the number of nodes through log2 so the height of a B-Tree is related through the log m where m is the order of the tree: height = logm n + 1 This relationship enables a B-Tree to hold vast amounts of data in just a few levels. For example, if the B-tree is of order 10, then level 0 can hold 9 pieces of data, level 1 can hold 10 nodes each with 9 pieces of data, or 90 pieces of data. Level 2 can hold 10 times 10 nodes (100), each with 9 pieces of data for a total of 900. Thus the three levels hold a total of 999. Searches can become very efficient because the number of nodes to be examined is reduced a factor of 10 times at each probe. Unfortunately, there is still some price to pay because each node can contain 9 pieces of data and therefore, in the worst case, all 9 keys would have to be searched in each node. Thus finding the node is of order log (base m) n but the total search is m-1 logm n. If the order of the tree is small there are still a substantial number of searches in the worst case. However if m is large, then the efficiency of the search is enhanced. Since the data are in order within any given node, a binary search can be used in the node. However, this is not of much value unless the order is large since a simple linear search may be almost as efficient for short lists. It should be clear that although the path length to a node may be very short, examining a node for a key can involve considerable searching within the node. Because of this the B-Tree structure is used with very large data sets which cannot easily be stored in main memory. The tree actually resides on disk. If a node is stored so that it occupies just one disk block then it can be read in with one read operation. Hence main memory can be used for fast searching within a node and only one disk access is required for each level in the tree. In this way the B-Tree structure minimises the number of disk accesses that must be made to find a given key. Q6:-Explain doubly linked list with its Implementation?
  • 12. ANS:- Overall Structure of Doubly-Linked Lists If you wish to traverse a list both forwards and backwards efficiently, or if you wish, given a list element, to determine the preceding and following elements quickly, then the doubly-linked listcomes in handy. A list element contains the data plus pointers to the next and previous list items as shown in the picture below. Of course we need a pointer to some link in the doubly-linked list to access list elements. It is convenient for doubly-linked lists to use a list header, or head, that has the same structure as any other list item and is actually part of the list data structure. The picture below shows an empty and nonempty doubly- linked list. By following arrows it is easy to go from the list header to the first and last list element, respectively. Insertion and removal of an element in a doubly-linked list is in this implementation rather easy. In the picture below we illustrate the pointer changes for a removal of a list item (old pointers have been drawn solid and new pointers are dashed arrows). We first locate the previous list item using the previous field. We make the next field of this list item point to the item following the one in cursor position pos. Then we make the previous field of this following item point to the item preceding the one
  • 13. in the cursor position pos. The list item pointed to by the cursor becomes useless and should be automatically garbage collected. In a pseudo-programming language we could write the following code: remove(cursor pos) begin if empty list then ERROR("empty list.") else if cursor points at list header then ERROR("cannot remove the list header") else pos previous next = pos next; pos next previous = pos previous; endif end Implementation:- #include <iostream> #include <string> #include <iomanip> #include <stdio.h> using namespace std; //template <class Object> //template <class Object> class Node
  • 14. { //friend ostream& operator<<(ostream& os, const Node&c); public: Node( int d=0); void print(){ cout<<this->data<<endl; } //private: Node* next; Node* prev; int data; friend class LinkList; }; Node::Node(int d):data(d) { } //template <class Object> class LinkList { void create(); public: //LinkList(); LinkList():head(NULL),tail(NULL),current(NULL) {create();} int base; //LinkList(const LinkList & rhs, const LinkList & lhs); ~LinkList(){delete current;} const Node& front() const;//element at current const Node& back() const;//element following current void move(); void insert (const Node & a);//add after current void remove (const Node &a); void print(); private: Node* current;//current Node* head; Node* tail; }; void LinkList::print() { Node *nodePt =head->next; while(nodePt != head) {
  • 15. cout<<"print function"<<endl; cout<<nodePt->data<<endl; nodePt=nodePt->next; } } //element at current void LinkList::create() { current = head = tail = new Node(0); current->next = current->prev = current; } const Node& LinkList::back()const { return *current; } //element after current const Node& LinkList::front() const { return *current ->next; } void LinkList::move() { current = current ->next; } //insert after current void LinkList :: insert(const Node& a) { Node* newNode= new Node(); newNode->prev=current; newNode->next=current->next; newNode->prev->next=newNode; newNode->next->prev=newNode; current=newNode; } void LinkList::remove(const Node& a) { Node* oldNode; oldNode=current; oldNode->prev->next=oldNode->next; oldNode->next->prev=oldNode->prev; delete oldNode; } //main file #include <iostream>
  • 16. #include <string> #include <iomanip> #include <stdio.h> #include "LinkedList.h" using namespace std; int main() { int n; cout<<"How many Nodes would you like to create"<<endl; cin>>n; LinkList list1 ; list1.print(); for(int loop = 0;loop < n;++loop) { list1.insert(loop); } list1.print(); }