Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 2, 2023 06:46 pm GMT

Data structures in Computer Science

Data structures

What is a data structure?

Data structure is a particular pattern for organizing info in a way, so we will be able to interact with it in the fastest way possible.

Arrays in Data Structure

so, maybe for some people the first question that comes to your mind is that, what's an array? The answer is simple. An array is a data structure organized as a linear collection of elements for which is a index which is a numerical interactor of position used for accessing content. In Arrays the same info may appear more than once, but in a different index!

Here is an example that i created. As you can see there are 2 starred numbers that are the same but, the indexes are different!
Image description

We can differentiate between arrays based on whether their size is declared up-front/static or able to change at run time (dynamic).

Most people who code must know about arrays and how they are useable in programming languages.

  • //Java
    int[] myArray = {1, 2, 3};Array

  • //C++
    int Array[] = {1, 2, 3};

  • //Python
    from array import array
    myArray = array('i', [1, 2, 3])

Or we can declare empty arrays of different size (I will use 3 here)

  • //Java
    int[] myArray = new int[3];

  • //C++
    int Array[3];

Stack Data Structure

A stack is a collection of objects optimized for 2 elements:

  1. Adding items
  2. Removing the most recent-added item in the list first

The items are inserted and removed according to the last-in, fist-out (LIFO)

Example: Imagine Putting 3 trays in the oven, the last tray (The one at the highest floor) is the first out.

Stack ADT

independent of any language, a stack data structure should provide the following update methods

  • push(e): Adds the element called "e" to the top of the stack.

  • pop(): If the stack is not empty, it removes and returns the top element

there are also other methods we call helpers: top(): returns the top element.
isEmpty(): Returns true if there are no elements in the stack, otherwise, returns false.

size(): Returns the number of elements in the stack.

Queue Data Structure

This structure is a collection of objects which is optimized for adding new objects to the collection ad removing the oldest object from the collection, therefore the first object-in will be the first object-out (FIFO)

Example: Imagine a shop line, the first person in the line is the first person out.

Queue stacks update methods

  • enqueue(e): Adds "e" to the end of the queue.
  • dequeue(): Removes and returns the element at the front of the queue or returns null if the queue is empty.

Accessor Methods

  • size(): Returns the number of elements in the queue.
  • isEmpty(): Returns true if the queue is empty.
  • first(): Returns the element at the front of the queue.

Queue options in programming languages (Didn't write every language)

  • //Javascript:supports the push and shift methods which is able to emulate the behavior of the behavior of a queue.

Finally, these were Most of the basics of Data structure used in computer science and is a must know for people who are interested in Computer Science


Original Link: https://dev.to/geminiixd/data-structures-in-computer-science-158g

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To