Академический Документы
Профессиональный Документы
Культура Документы
1-QUEUE
1.1-What is Queue in Data Structure?
Code Example
Removal of an element from the queue. An element can only be deleted when
there is at least an element to delete i.e. rear > 0. Now, element at arr[front]can be
deleted but all the remaining elements have to shifted to the left by one position in
order for the dequeue operation to delete the second element from the left on
another dequeue operation.
deQueue(q)
1) If stack1 is empty then error
2) Pop an item from stack1 and return it
Here time complexity will be O(1)
Code Example
// Return top of s1
int x = s1.peek();
s1.pop();
return x;
}
This function is used to reference the first or the oldest element of the queue
container. This function can be used to fetch the first element of a queue.
queuename.front()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the first element of the queue container.
Code Example
int main()
{
queue<int> myqueue;
myqueue.push(3);
myqueue.push(4);
// Queue becomes 3, 4
This function is used to reference the last or the newest element of the queue
container. This function can be used to fetch the first element from the back of a
queue.
queuename.rear()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the last element of the queue container.
Code Example
int main()
{
queue<int> myqueue;
myqueue.push(3);
myqueue.push(4);
// Queue becomes 3, 4
For example:
interface Queue<T> {
Queue<T> enqueue(T ele);
T dequeue();
}
In a linked queue, each node of the queue consists of two parts i.e. data part
and the link part. Each element of the queue points to its immediate next element in
the memory.
In the linked queue, there are two pointers maintained in the memory i.e. front pointer
and rear pointer. The front pointer contains the address of the starting element of the
queue while the rear pointer contains the address of the last element of the queue.
Insertion and deletions are performed at rear and front end respectively. If front and
rear both are NULL, it indicates that the queue is empty.
Before create Queue Link, we should Node class. In node we have one or more data
and one pointer next type.
Code Example
After that we can create an Queue object using node, node’s pointer and data value.
All new item will adding last pointer. That’s queue rule.
So that’s mean last item will be new item.
public QueueLinkedList() { }
Push the item into the stack, and pop the item out of the stack. A stack is a
limited access data structure - elements can be added and removed from the stack
only at the top. push adds an item to the top of the stack, pop removes the item from
the top. A helpful analogy is to think of a stack of books; you can remove only the top
book, also you can add a new book on the top.
PUSH()
stackname.push(value)
Parameters :
The value of the element to be inserted is passed as the parameter.
Result :
Adds an element of value same as that of
the parameter passed at the top of the stack.
Code Example
Input : mystack
mystack.push(6);
Output : 6
POP()
stackname.pop()
Parameters :
No parameters are passed.
Result :
Removes the newest element in the stackor basically the top element.
Code Example
Input : mystack = 0, 1, 2
mystack.pop();
Output : 0, 1
2.1.2-What is Top Method in Stack?
Stacks are a type of container adaptors with LIFO(Last In First Out) type of
working, where a new element is added at one end and (top) an element is removed
from that end only.
TOP()
stackname.top()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the top element of the stack container.
Code Example
Input : stackname.push(5);
stackname.push(1);
stackname.top();
Output : 1
Code Example
STACK.push("Welcome");
STACK.push("Home");
System.out.println("The element at the top of the" + " stack is: " +
STACK.peek());
Code Example
Code Example
struct Node {
int data;
struct Node* link;
};
struct Node* top;
Code Example
class DynamicStack {
private int top;
private int capacity;
private int[] array;
3-HASHTABLE
Like HashMap, Hashtable stores key/value pairs in a hash table. When using a
Hashtable, you specify an object that is used as a key, and the value that you want
linked to that key. The key is then hashed, and the resulting hash code is used as the
index at which the value is stored within the table.
1-void clear( )
Code Example
hashSet.put(5);
hashSet.put(3);
hashSet.reset();
//Output: null
Returns the object that contains the value associated with the key. If the key is not in
the hash table, a null object is returned.
Code Example
hashSet.put(5);
hashSet.put(3);
hashSet.get(1)
//Output: 3
3-boolean isEmpty( )
Returns true if the hash table is empty; returns false if it contains at least one key.
Code Example
hashSet.put(5);
hashSet.put(3);
hashSet.isEmpty();
//Output: false
Inserts a key and a value into the hash table. Returns null if the key isn't already in
the hash table; returns the previous value associated with the key if the key is already
in the hash table.
Code Example
hashSet.put(5);
hashSet.put(3);
//Output: 5,3
Removes the key and its value. Returns the value associated with the key. If the key
is not in the hash table, a null object is returned.
Code Example
hashSet.put(5);
hashSet.put(3);
hashSet.remove(3);
//Output: 5
4-LINKED LIST
4.1-What is Linked List in Data Structure?
A linked list is a linear data structure, in which the elements are not stored at
contiguous memory locations. The elements in a linked list are linked using pointers
as shown in the below image:
Code Example
class LinkedList {
Node head; // head of list
In this article, insertion in the list is done at the end, that is the new node is
added after the last node of the given Linked List. For example, if the given Linked
List is 5->10->15->20->25 and 30 is to be inserted, then the Linked List becomes 5-
>10->15->20->25->30.
Since a Linked List is typically represented by the head pointer of it, it is required to
traverse the list till the last node and then change the next of last node to new node.
Code Example
Output:
LinkedList: 1 2 3 4 5 6 7 8
For traversal, below is a general purpose function printList() that prints any
given list by traversing the list from head node to the last.
Code Example
System.out.print("LinkedList: ");
// Go to next node
currNode = currNode.next;
}
}
How to do it:
To delete a node from linked list, do following steps.
1. Search the key for its first occurrence in the list
2. Now, Any of the 3 conditions can be there:
// CASE 1:
// If head node itself holds the key to be deleted
//
// CASE 2:
// If the key is somewhere other than at head
//
Output:
LinkedList: 1 2 3 4 5 6 7 8
1 found and deleted
LinkedList: 2 3 4 5 6 7 8
4 found and deleted
LinkedList: 2 3 5 6 7 8
10 not found
LinkedList: 2 3 5 6 7 8
5-DYNAMIC ARRAY
Add Element: Add element at the end if array size is not enough then then extend
size of array and add element at the end of original array as well as given index.
Doing all that copying takes O(n) time, where n is the number of elements in our
array. That’s an expensive cost for an append. In a fixed-length array, appends only
take O(1) time.
But appends take O(n) time only when we insert into a full array. And that is pretty
rare, especially if we double the size of the array every time we run out of space. So
in most cases appending is still O(1) time, and sometimes it’s O(n) time.
Code Example
public void add(int data)
{
// check no of element is equql to size of array
if (count == size) {
growSize(); // make array size double
} // insert element at end of array
array[count] = data;
count++; }
When array has null/zero data(exclude add by you) at the right side of array
whose take unrequited memory, the method srinkSize() free extra memory. When all
space is consumed, and an additional element is to be added, then the underlying
fixed size array need to be increase size. Typically resizing is expensive because you
have to allocate a bigger array and copy over all of the elements from the array you
have overgrow before we can finally append our item.
Code Example
Java array is an object which contains elements of a similar data type. It is a data
structure where we store similar elements. We can store only a fixed set of elements
in a Java array.
Array in java is index-based, the first element of the array is stored at the 0 index.
Code Example
class TestArray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
// Traversing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}
}
6.1.1-Advantages
Code Optimization: It makes the code optimized, we can retrieve or sort the
data efficiently.
Size Limit: We can store only the fixed size of elements in the array. It doesn't
grow its size at runtime. To solve this problem, collection framework is used in
Java which grows automatically.