Saturday 20 April 2024

Implementation of stack using linked list

 Linked Lists in Stack Implementation

  • Linked lists offer a dynamic memory allocation alternative to arrays.
  • Despite different data structures, time complexities for stack operations remain consistent.
  • Nodes are non-contiguously maintained in memory, each with a pointer to its successor node.
  • Stack overflow occurs when insufficient memory heap space is available for node creation.
  • Singly linked lists align standard linked list operations with stack operations, adhering to the Last In, First Out (LIFO) principle.
  • Top variable guides operations like Pop, Push, Peek, and Display.
  • Unlike arrays, linked lists offer flexibility, eliminating risk of overflow.


 

 Stack Implementation vs. Array vs. Linked List

  • Array-based stacks work for a fixed number of data values, making them unsuitable for unknown data sizes.
  •  Linked list stacks can work for unlimited data values, eliminating the need to fix the initial size.
  • Linked list stacks insert new elements as 'top' elements, pointing to the 'top' node.
  • To remove an element, move 'top' to its previous node in the list.
  • The first element's next field must always be NULL.

Operations performed on Stack

Following operations can be performed on a stack:

  • push(): It inserts an element to the top of the stack. It takes O(1) time, as each node is inserted at the head/top of the linked list.
  • pop(): It removes an element from the top of the stack. It takes O(1) time, as the top always points to the newly inserted node.
  • peek(): It returns the top element of the stack.
  • size(): It returns the size of the stack, i.e., the total number of items in a stack.
  • isEmpty(): Returns a boolean value. It returns true if the stack is empty. Else, it returns false.

 Before performing all the above operations, lets first define node structure

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Friday 5 April 2024

NumPy Joining Array

In NumPy, joining arrays refers to combining the elements of multiple arrays into a single new array. There are two main ways to achieve this:

  1. Concatenation: This involves joining arrays along a specified axis. The most common function for concatenation is np.concatenate. It takes a sequence of arrays as its first argument and optionally the axis along which to join them. By default, concatenation happens along axis 0 (rows for 2D arrays).

Here's an example of concatenating two arrays:

 


  1. Stacking: This is similar to concatenation but with a key difference. Stacking creates a new axis along which the arrays are joined. NumPy provides convenience functions for stacking along specific axes:
  • np.hstack: Stacks arrays horizontally (column-wise) by creating a new axis 1.
  • np.vstack: Stacks arrays vertically (row-wise) by creating a new axis 0.
  • np.dstack: Stacks arrays along depth (useful for 3D arrays) by creating a new axis 2.

Here are examples

NumPy Array Iterating

You can go through NumPy arrays in two main ways:

1. Using a for loop: This is the easiest way to go through each item in a NumPy collection. You can just do a loop over the array, and each time you do that, you'll be able to reach the current element.

2. Indexing with a for loop: You can also use a for loop and indexing to go through the parts of a NumPy array. This method works well when you need to get both the number and the index of the current element.



Both methods go through the elements of the array in row-major order, which is the usual way to order things in C. This means that the elements are gone through in a way that first fills up one row of the array and then goes on to the next row.

Besides these simple ways, NumPy also has the nditer function for more complex iteration. There are many useful tools like nditer that give you more freedom when iterating over arrays.

nditer is the way to go if you need more advanced features for going through NumPy collections. But a simple for loop, with or without numbering, will do for most simple situations.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top