Linked List Vs Array
1. An array is the data structure that contains a collection
of similar type data elements whereas the Linked list is
considered as data structure contains a collection of
unordered linked elements known as nodes.
2. Accessing an element in an array is fast using Index,
Random access is not allowed in linked List. We have to
access elements sequentially starting from the first
node.
3. Operations like insertion and deletion in arrays
consume a lot of time. On the other hand, the
performance of these operations in Linked lists is fast.
5. Arrays are of fixed size. In contrast, Linked lists are
dynamic and flexible and can expand and contract its size.
6. In an array, memory is assigned during compile time while
in a Linked list it is allocated during execution or runtime.
memory utilization is inefficient in the array. Conversely,
memory utilization is efficient in the linked list.
7. Elements are stored consecutively in arrays whereas it is
stored randomly in Linked lists.
8. The requirement of memory is less due to actual data
being stored within the index in the array. As against, there is
a need for more memory in Linked Lists due to storage of
additional next and previous referencing elements.
• A linked list is represented by a pointer to the
first node of the linked list. The first node is
called head. If the linked list is empty, then
value of head is NULL.
• Each node in a list consists of at least two
parts:
1) data
2) Pointer (Or Reference) to the next node
In C language, a linked list can be
implemented using structure and pointers.
Declaring a Linked list :
// A linked list node
struct Node
{
int data;
struct Node *next;
};
• Each element in a linked list is stored in the form
of a node..
Types Of Linked Lists
1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List
• They are passed to main() function.
• They are parameters/arguments supplied to the
program when it is invoked.
• They are used to control program from outside
instead of hard coding those values inside the
code.
• Command-line arguments are given after the name
of the program in command-line shell of Operating
Systems.
• To pass command line arguments, we typically
define main() with two arguments : first argument
is the number of command line arguments and
second is list of command-line arguments.
• argc (ARGument Count) is int and stores number
of command-line arguments passed by the user
including the name of the program. So if we pass
a value to a program, value of argc would be 2
(one for program name and one for argument).
• The value of argc should be positive always.
• argv(ARGument Vector) is array of character
pointers listing all the arguments.
• argv[0] holds the name of the program , after
that till argv[argc-1] every element is command -
line arguments.
• argv[argc] is a NULL pointer.
Working with File
• fopen() is used to open a file.
• fopen() returns a pointer to structure FILE which
is a predefined structure in "stdio.h" header file.
• Opening a file means we bring the file contents
from disk to RAM to perform operations on it.
• If the file is successfully opened then fopen()
returns a pointer to the file and if it is unable to
open the file then it returns NULL.
• The file to be opened must be present in the
directory in which the executable file of this
program is present.
• fgetc() read a character from the file, and fputc()
write a character into the file.
Dynamic memory allocation:
functions
• malloc():
– memory allocation
– create memory block of given size.
– By default value will be garbage value in allocated
block.
– Most application in allocating size for structures.
– E.g: malloc(4)
• calloc():
– calculated allocation
– used to allocate the continuous memory on element
by element by basis.
– It is useful to create array type structure.
– By default value will zero(0) in allocated block.
– E.g: calloc(3,2)
• realloc():
– re-allocation.
– used to increase/decrease the size of the block while
preserving the address and content of the beginning
of the block.
– E.g: realloc(ptr,4)
• free(): used to release (de-allocate)memory.
– Free(address of pointer which points to the allocated
block)
68
Example
• Associate a "cost" with each statement.
• Find the "total cost“ by finding the total number of
times each statement is executed.
Algorithm 1 Algorithm 2
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
... ...
arr[N-1] = 0; c1
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 =
= O(N) = O(N)
69
Another Example
• Algorithm 3 Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x (N )x (N+1) + c3 x N2
=O(N2)