Sei sulla pagina 1di 26

Habtamu Yedemie

4 Year Department Of
th

Information Technology
ID No: TER/3503/04

Array VS Linked List


IRS:Assignment
.What is Array ?
Array is a data structure that represents a collection of the
same types of data.
An array is a group of related data items that all have the
same name and the same data type.
Arrays can be of any data type we choose.
Arrays are static in that they remain the same size
throughout program execution.
An array’s data items are stored contiguously in memory.
Array is a fixed-size data structure. Once an
array is created, its size cannot be changed.
Conn(….)
Each of the data items is known as an element of the
array. Each element can be accessed individually.
Declaring Array Variables
datatype[] arrayname;
Example:
double[] myList;

datatype arrayname[];
Example:
double myList[];
Conn(…)

Imagine that we have 100 scores. We need to read them,


process them and print them.
We must also keep these 100 scores in memory for the
duration of the program. We can define a hundred variables,
each with a different name, as shown in Figure below.

A hundred individual variables


Conn(…)

An array is a sequenced collection of


elements, normally of the same data type,
although some programming languages
accept arrays in which elements are of
different types. We can refer to the
elements in the array as the first element,
the second element and so forth, until we
get to the last element.
Conn(…)

E.g. Arrays with indexes

Limitations of array
Once an array is created, its size cannot be altered.
Array provides inadequate support for inserting, deleting,
sorting, and searching operations.
What are Advantages and Disadvantages of arrays?
Advantages:

1. It is used to represent multiple data items of same type by


using only single name.
2. It can be used to implement other data structures like
linked lists, stacks, queues, trees, graphs etc.
3. 2D arrays are used to represent matrices.

Disadvantages:
1. We must know in advance that how many elements are to be
stored in array.
2. Array is static structure. It means that array is of fixed size. The
memory which is allocated to array can not be increased or reduced.
3. Since array is of fixed size, if we allocate more memory than
requirement then the memory space will be wasted. And if we
allocate less memory than requirement, then it will create problem.
4. The elements of array are stored in consecutive memory locations.
So insertions and deletions are very difficult and time consuming.
.Linked List
 Linked list is an ordered collection of
element s called nodes which has two parts.
.The nodes connected by pointer.
Head Data Pointer Data Pointer Data \
 What is the difference between
Linked List and Linear Array?

Array
Insertions and deletions are difficult.
It needs movements of elements for insertion and deletion.
In it space is wasted.
It is more expensive.
It requires less space as only information is stored.
Its size is fixed.
Conn(….)

It cannot be extended or reduced according to


requirements.
Same amount of time is required to access each
element.
Elements are stored in consecutive memory
locations.
If have to go to a particular element then we can
reach there directly.
Linked List
Insertions and deletions can be done easily.
It does not need movement of nodes for insertion and
deletion.
In it space is not wasted.
It is less expensive.
It requires more space as pointers are also stored along with
information.
Its size is not fixed.
A linked list is a series of connected nodes.
Each node contains at least:
1.A piece of data (any type).
3.Pointer to the next node in the list.
Head: pointer to the first node.
The last node points to NULL.
Conn(….)

It can be extended or reduced according to


requirements.
Different amount of time is required to access
each element.
Elements may or may not be stored in
consecutive memory locations.
If we have to go to a particular node then we
have to go through all those nodes that come
before that node.
Conn(….)

-A linked list is a data structure which can


change during execution.
-Successive elements are connected by pointers.
-Last element points to NULL.
-It can grow or shrink in size during execution of
a program.
-It can be made just as long as required.
–It does not waste memory space.
Keeping track of a linked list:
–Must know the pointer to the first element of the list (called start, head,
etc.).
•Linked lists provide flexibility in allowing the items to be rearranged
efficiently.
–Insert an element.
–Delete an element.
Linked List Declaration
 Syntax Example
Structure nodename struct node
{ {
Variable declarations; int data;//to
nodename*next; store integer
}; data.
Node*Next;
// a pointer to
next node.
};
E.g1:A linked list node, STUDNODE stores an
ID of a student (4 digit characters) name of
student (20 characters) and floating point
Conn(…)
CGPA and a pointer to the next node. Write
the declaration of the node.
Struct STUDENODE{

char ID[4];

char name[20];

float CGPA;

STUDENOD*Next;

};
Advantages and Disadvantages of linked list

Advantages of linked list:-


It is not necessary to know in advance the number of
elements to be stored in the list and therefore, need
not allocate. d as and when necessary.
In a linked list, insertions and deletions can be handled
efficiently without fixing the size of the memory in
advance.
An important advantage of linked lists over arrays is
that the linked list uses exactly as much memory as it
needs, and can be made to expand to fill all available
memory locations if needed.
Disadvantages:-
The traversal is sequential.
Increased overhead for storing pointers for linking the
data items.
What is Node?
Linked list is a series of nodes.
Each node consists of two parts . Data
part and Pointer part.
Pointer part stores in the address of
the next node.
Basic Operation of Linked List
1.Create a Dynamic Node.
2.create a List.
3.Traversing list.
4.Add node.
6.Delate node.
1.Create a Dynamic Node

To start with, we have to create a node (the


first node), and make
head
point to it.
head = (node *) malloc(sizeof(node));
If there are n number of nodes in the initial
linked list:
–Allocate n records, one by one.
–Read in the fields of the records.
–Modify the links of the records so that the
chain is formed.
-Examples for creating a Dynamic Node as follows
below.
node *create_list()
{
int k, n;
node *p, *head;
printf ("\n How many
elements to enter?");
scanf ("%d", &n);
for (k=0; k<n; k++)
{
if (k == 0) {
head = (node *) mall
oc(sizeof(node));
p = head;
}
else {
p->next = (node *)
malloc(sizeof(node));
p = p->next;
}
scanf ("%d %s %d", &p->rol
l, p->name, &p->age);
}
p->next = NULL;
return (head);
}
6.Deleting a Node

Deleting a node from a linked list


requires two steps:
Remove the node from the list
without breaking the links created
by the next pointers
Deleting the node from memory
The deleteNode function begins on
the next slide.
3.Traversing the List

The displayList member function traverses the list,


displaying the value member of each node. The following
pseudocode represents the algorithm. The C++ code for the
member function follows on the example below.

void FloatList::displayList(void)
{
ListNode *nodePtr;

nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
4. Adding a Node
There are four steps to add a node to a linked list:

1.Allocate memory for the new node.


2.Determine the insertion point (you need to know
only the new node’s predecessor (pPre)
3.Point the new node to its successor.
4.Point the predecessor to the new node.

Pointer to the predecessor (pPre) can be in one of


two states:
1.it can contain the address of a node (i.e. you are
adding somewhere after the first node – in the
middle or at the end)
2.it can be NULL (i.e. you are adding either to an
empty list or at the beginning of the list)
void FloatList::deleteNode(float num)
{
ListNode *nodePtr, *previousNode;

// If the list is empty, do nothing.


if (!head)
return;

// Determine if the first node is the one.


if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// Initialize nodePtr to head of list
nodePtr = head;

// Skip all nodes whose value member is


// not equal to num.
while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}

// Link the previous node to the node after


// nodePtr, then delete nodePtr.
previousNode->next = nodePtr->next;
delete nodePtr;
}
Examples
To hold the list.
Struct NumNode { To traverse the list.
int value;
NumNode*Next; To mark a node in the list
};
NumNode*head,*current,*temp,*Nptr;
head=NULL;\\the list is empty.

To allocate a new node.


Array VS Linked List
Aspect Array Linked List
Size : .Fixed number .Grow and contact
.size need to be since insertions and
specific during deletions.
declaration.
Storage: .Static: location is .Dynamic: location is
allocated during allocated during
compile time. Run time.
Order and .Sorted consecutively .Stored Randomly.
Sorting:
Accessing .Direct or random .Sequential access
the Element: access method. Method.
.Specify the array .Travers starting
index. from the first
node in the list
by pointer.
Search: .Binary search and .Linear search.
Linear search.

Potrebbero piacerti anche