Arrays and Link List

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Arrays and Link List

Array
An array is a collection of items of same data type stored at contiguous series of elements memory
locations.

Syntax:

datetype arrayName [ arraySize ] ;


float arr [5] = { } ;
float arr [5] = { 1,2,3,4,5} ;
01234

Terms
<iostream> - It is used to define the cout, cin and cerr objects, which correspond to standard output
stream, standard input stream and standard error stream, respectively.
Using namespace, you can define the context in which names are defined. In essence, a namespace
defines a scope. C++ has a standard library that contains common functionality you use in building your
applications like containers, algorithms, etc.
cout is an object of the output stream that is used to show output. extraction operator( << ).
cin is an object of the input stream and is used to take input from input streams like files, console, etc.
insertion operator( >> )
The endl is a predefined object of ostream class. It is used to insert a new line characters
A float point type is a type of variable that holds real numbers including a numbers with decimal
points. Float need for decimal points like 100 is also 100.0
First Example – intro and tell that using array is much better
Second Example (assign number) -
Third Example (with user input) -
Fourth Example (with loop) -
Fifth Example –
SEARCH
INSERT
DELETE
Rotate Array

Let us take arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2.


First Step:
=> Rotate to left by one position.
=> arr[] = {2, 3, 4, 5, 6, 7, 1}
Second Step:
=> Rotate again to left by one position
=> arr[] = {3, 4, 5, 6, 7, 1, 2}
Rotation is done by 2 times.
So the array becomes arr[] = {3, 4, 5, 6, 7, 1, 2}
https://www.geeksforgeeks.org/arrays-in-c-cpp/?ref=lbp

https://www.geeksforgeeks.org/search-insert-and-delete-in-an-unsorted-array/?ref=lbp
https://www.tutorjoes.in/cpp_programming_tutorial/search_in_cpp_program#:~:text=Number%20of
%20elements%20in%20array,searched%20let%20if%20be%20num.
Linked list

Stores data one after other memory – non continuous collection it does not store data one after other
but the elements of a linked list are randomly position in your memory.

How do you access elements of a linked list? Link the element of the list in order to be able to access all
of the elements

The Head or The first element stores two things The value (1) and 2nd the position or address of the
next element, so it’s like having a pointer to the second element then it also stores two things, the value
(2) the address of the next element and so on. Then the last is null because this is the last one

Adv

Arrays has fix size and link list has dynamic size

Dis (value of the element and the pointer so need more space)

Random access to the element is not allowed – how will you access forward element of the
linked list, di pwede to gawin kase di natin alam san nag store yung 4th element natin since it is
saan saan siya sa memory, naka random position yung element natin sa memory. So yung 4 th
element natin is nasa third then yung third nasa 2 nd then 2nd nasa 1st or head so ang alam natin is
nasaan ang 1st element natin. So if gusto mo ma access ang 4th element mag traverse sa path na to.

Array can be directly element access like name ng array or index.

 The size of the arrays is fixed: So we must know the upper limit on the number of elements in
advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the
usage.
Singly Linked List: It is the simplest type of linked list in which every node contains some data
and a pointer to the next node of the same data type. The node contains a pointer to the next
node means that the node stores the address of the next node in the sequence. A single linked
list allows traversal of data only in one way.
doubly linked list or a two-way linked list is a more complex type of linked list which contains
a pointer to the next as well as the previous node in sequence, Therefore, it contains three parts
are data, a pointer to the next node, and a pointer to the previous node. This would enable us to
traverse the list in the backward direction as well
A circular linked list is that in which the last node contains the pointer to the first node of the
list. While traversing a circular linked list, we can begin at any node and traverse the list in any
direction forward and backward until we reach the same node we started. Thus, a circular linked
list has no beginning and no end.
Header A header linked list is a special type of linked list which contains a header node at the beginning
of the list. So, in a header linked list START will not point to the first node of the list but START will
contain the address of the header node.
https://www.geeksforgeeks.org/types-of-linked-list/?ref=lbp

You might also like