c with data structures solutions
TRANSCRIPT
-
8/6/2019 C With Data Structures Solutions
1/9
Elementary Data Structures
Elementary data structures such as stacks, queues,
lists, and heaps will be the \of-the-shelf" components
we build our algorithm from. There are two aspects to
any data structure:
The abstract operations which it supports.
The implementatiton of these operations.
The fact that we can describe the behavior of our data
structures in terms of abstract operations explains why
we can use them without thinking, while the fact that
we have di erent implementation of the same abstract
operations enables us to optimize performance.
Ordered Lists
An ordered listis a list in which the order of the items is significant. However, the itemsin an ordered lists are not necessarily sorted. Consequently, it is possible to change theorder of items and still have a valid ordered list.
Program defines the OrderedList interface. The OrderedList interface extends the
SearchableContainer interface defined in Program . Recall that a searchablecontainer is a container that supports the following additional operations:
Insertused to put objects into a the container;
Withdrawused to remove objects from the container;
Findused to locate objects in the container;
IsMemberused to test whether a given object instance is in the container.
-
8/6/2019 C With Data Structures Solutions
2/9
Linked List
A linked list is called so because each of items in the list is a part of a structure, which is
linked to the structure containing the next item. This type of list is called a linked listsince it can be considered as a list whose order is given by links from one item to thenext.
Structure
Item
Each item has a node consisting two fields one containing the variable and anotherconsisting of address of the next item(i.e., pointer to the next item) in the list. A linkedlist is therefore a collection of structures ordered by logical links that are stored as thepart of data.
Consider the following example to illustrator the concept of linking. Suppose we define astructure as follows
struct linked_list{float age;struct linked_list *next;}struct Linked_list node1,node2;
this statement creates space for nodes each containing 2 empty fields
node1
-
8/6/2019 C With Data Structures Solutions
3/9
node1.age
node1.next
node2
node2.age
node2.next
The next pointer of node1 can be made to point to the node 2 by the same statement.node1.next=&node2;
This statement stores the address of node 2 into the field node1.next and thisestablishes a link between node1 and node2 similarly we can combine the process tocreate a special pointer value called null that can be stored in the next field of the lastnode
Advantages of Linked List:
A linked list is a dynamic data structure and therefore the size of the linked list can grow
or shrink in size during execution of the program. A linked list does not require any extraspace therefore it does not waste extra memory. It provides flexibility in rearranging theitems efficiently.
-
8/6/2019 C With Data Structures Solutions
4/9
The limitation of linked list is that it consumes extra space when compared to a arraysince each node must also contain the address of the next item in the list to search for asingle item in a linked list is cumbersome and time consuming.
Types of linked list:
There are different kinds of linked lists they areLinear singly linked listCircular singly linked listTwo way or doubly linked listCircular doubly linked list.
Applications of linked lists:
Linked lists concepts are useful to model many different abstract data types such asqueues stacks and trees. If we restrict the process of insertions to one end of the listand deletions to the other end then
Queue
A queue (pronounced /kju/kew) is a particular kind ofcollection in which the entities
in the collection are kept in order and the principal (or only) operations on the collectionare the addition of entities to the rear terminal position and removal of entities from thefront terminal position. This makes the queue a First-In-First-Out (FIFO) data structure.In a FIFO data structure, the first element added to the queue will be the first one to beremoved. This is equivalent to the requirement that once an element is added, allelements that were added before have to be removed before the new element can beinvoked. A queue is an example of a linear data structure.
Queues provide services in computer science, transport, and operations researchwhere various entities such as data, objects, persons, or events are stored and held tobe processed later. In these contexts, the queue performs the function of a buffer.
Queues are common in computer programs, where they are implemented as datastructures coupled with access routines, as an abstract data structure or in object-oriented languages as classes. Common implementations are circular buffers andlinked lists.
Stack
-
8/6/2019 C With Data Structures Solutions
5/9
In computer science, a stack is a last in, first out (LIFO) abstract data type and datastructure. A stack can have any abstract data type as an element, but is characterizedby only three fundamental operations: push, pop and stack top. The push operationadds a new item to the top of the stack, or initializes the stack if it is empty. If the stackis full and does not contain enough space to accept the given item, the stack is then
considered to be in an overflow state. The pop operation removes an item from the topof the stack. A pop either reveals previously concealed items, or results in an emptystack, but if the stack is empty then it goes into underflow state (It means no items arepresent in stack to be removed). The stack top operation gets the data from the top-most position and returns it to the user without deleting it. The same underflow state canalso occur in stack top operation if stack is empty.
A stack is a restricted data structure, because only a small number of operations areperformed on it. The nature of the pop and push operations also means that stackelements have a natural order. Elements are removed from the stack in the reverseorder to the order of their addition: therefore, the lower elements are those that have
been on the stack the longest.
[1]
Binary Tree
A binary tree is made of nodes, where each node contains a "left" pointer, a "right"
pointer, and a data element. The "root" pointer points to the topmost node in the tree.
The left and right pointers recursively point to smaller "subtrees" on either side. A null
pointer represents a binary tree with no elements -- the empty tree. The formal recursivedefinition is: a binary tree is either empty (represented by a null pointer), or is made of a
single node, where the left and right pointers (recursive definition ahead) each point to a
binary tree.
-
8/6/2019 C With Data Structures Solutions
6/9
A "binary search tree" (BST) or "ordered binary tree" is a type of binary tree where thenodes are arranged in order: for each node, all elements in its left subtree are less-or-equal to the node (). The tree shown above is a binary search tree -- the "root" node is a 5, and itsleft subtree nodes (1, 3, 4) are 5.Recursively, each of the subtrees must also obey the binary search tree constraint: inthe (1, 3, 4) subtree, the 3 is the root, the 1 3. Watch out for the exactwording in the problems -- a "binary search tree" is different from a "binary tree".
The nodes at the bottom edge of the tree have empty subtrees and are called "leaf"nodes (1, 4, 6) while the others are "internal" nodes (3, 5, 9).
Binary Search Tree Niche
Basically, binary search trees are fast at insert and lookup. The next section presents
the code for these two algorithms. On average, a binary search tree algorithm can
locate a node in an N node tree in order lg(N) time (log base 2). Therefore, binary
search trees are good for "dictionary" problems where the code inserts and looks up
information indexed by some key. The lg(N) behavior is the average case -- it's possible
for a particular tree to be much slower depending on its shape.
Hash Table
n computer science, a hash table or hash map is a data structure that uses a hashfunction to map identifying values, known as keys (e.g., a person's name), to theirassociated values (e.g., their telephone number). Thus, a hash table implements an
-
8/6/2019 C With Data Structures Solutions
7/9
associative array. The hash function is used to transform the key into the index (thehash) of an array element (the slotorbucket) where the corresponding value is to besought.
Ideally, the hash function should map each possible key to a unique slot index, but this
ideal is rarely achievable in practice (unless the hash keys are fixed; i.e. new entries arenever added to the table after it is created). Instead, most hash table designs assumethat hash collisionsdifferent keys that map to the same hash valuewill occur andmust be accommodated in some way.
In a well-dimensioned hash table, the average cost (number ofinstructions) for eachlookup is independent of the number of elements stored in the table. Many hash tabledesigns also allow arbitrary insertions and deletions of key-value pairs, at constantaverage (indeed, amortized[1]) cost per operation.[2][3]
In many situations, hash tables turn out to be more efficient than search trees or any
othertable lookup structure. For this reason, they are widely used in many kinds ofcomputersoftware, particularly forassociative arrays, database indexing, caches, andsets.
C program to represents the Stack Implementation on POP and PUSH operation
01#include02#include03#include"apstring.h"04#include"apvector.h"05#include"apstring.cpp"0607classSTACK08{09 private:10 inttop, stack[20];11 public:12 STACK();13 push();14 pop(intx);15 intcurrent ()16 {17 returntop;18 }19};2021STACK::STACK()22{
-
8/6/2019 C With Data Structures Solutions
8/9
23 top=-1;24 for(inti=0;i
-
8/6/2019 C With Data Structures Solutions
9/9
64 cout