0% found this document useful (0 votes)
106 views19 pages

Chapter 2 Arrays Iteration Invariants

The document discusses data structures and arrays. It explains that data structures use algorithms and storage to hold values, which are typically fundamental data types like integers and floats. Arrays provide a convenient way to store sequenced collections of variables of the same type in adjacent memory locations. The document then discusses key concepts related to arrays like elements, indexes, and basic array operations. It also covers linked lists and their characteristics like efficient insertion and searching times. Specific algorithms are presented for inserting values, searching, and deleting from linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views19 pages

Chapter 2 Arrays Iteration Invariants

The document discusses data structures and arrays. It explains that data structures use algorithms and storage to hold values, which are typically fundamental data types like integers and floats. Arrays provide a convenient way to store sequenced collections of variables of the same type in adjacent memory locations. The document then discusses key concepts related to arrays like elements, indexes, and basic array operations. It also covers linked lists and their characteristics like efficient insertion and searching times. Specific algorithms are presented for inserting values, searching, and deleting from linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

DATA STRUCTURES AND ARRAYS

Data structure uses some algorithms and need


storage for storing values. For storing these values,
programmers must need to have the fundamental data
type's names such as char, int, float & double. These
particular data types are beneficial for declaring
variables, constants or a return type for a function;
they are in control by the fact that, these types can
store only a specific form of value at a time. For
processing such a large amount of data, programmers
need powerful data types that would facilitate efficient
storage, accessing and dealing with such data items.
Using C++, you can implement the concept of arrays.
What are arrays?
The array is a fixed-size sequenced collection
of variables belonging to the same data types.
The array has adjacent memory locations to
store values. Since the array provides a
convenient structure for representing data, it
falls under the category of the data structures in
C. The syntax for declaring array are:
data_type array_name [array_size];
The Following are the essential terminologies
used for understanding the concepts of Arrays:
• Element: Every item stored in an array is
termed as an element
• Index: each memory location of an element in
an array is denoted by a numerical index
which is used for identifying the element
Why Do You Need Arrays for Building a
Specific Data Structure?
When a program works with many variables
which hold comparable forms of data, then
organizational and managerial difficulty quickly
arise. If you are not using arrays, then the
number of variables used will increase. Using
the array, the number of variables reduces, i.e.,
you can use a single name for multiple values,
you need to deal with its index values (starting
from 0 to n).
So if the total run of each player is getting
stored in separate variables, using arrays you
can bring them all into one array having single
name like: plrscore[11];
Collecting Input Data in Arrays
Arrays are particularly helpful for making a collection of
input data which arrive in random order. An excellent
example will be vote counting: You can write a program
which tallies the votes of a four-candidate in an
election. (For your ease, you will say use the
candidates' names as Cand 0, Cand 1, Cand 2, and Cand
3.) Votes arrive once at a time, where a vote for
Candidate i is denoted by the number, i. So according to
this example, two votes for Cand 3 followed by one
vote for Cand 0 would appear:
3, 3, 0, and so on....
i.e., the structure will be: int votes[4];
Basic Operations
There is some specific operation that can be performed
or those that are supported by the array. These are:
• Traversing: It prints all the array elements one
after another.
• Inserting: It adds an element at given index.
• Deleting: It is used to delete an element at given
index.
• Searching: It searches for an element(s) using
given index or by value.
• Updating: It is used to update an element at
given index.
LINKED LIST
The linked list or one way list is a linear set of data
elements which is also termed as nodes. Here, the
linear order is specified using pointers.
Each node is separated into two different parts:
• The first part holds the information of the
element or node
• The second piece contains the address of the
next node (link / next- pointer field) in this
structure list.
Linked lists can be measured as a form of high-level
standpoint as being a series of nodes where each
node has at least one single pointer to the next
connected node, and in the case of the last node, a
null pointer is used for representing that there will be
no further nodes in the linked list.
In the data structure, you will be
implementing the linked lists which always
maintain head and tail pointers for
inserting values at either the head or tail of
the list is a constant time operation.
Randomly inserting of values is excluded
using this concept and will follow a linear
operation.
As such, linked lists in data structure have
some characteristics which are mentioned below:
• Insertion is O(1)
• Deletion is O(n)
• Searching is O(n)
Linked lists have a few key points that usually
make them very efficient for implementing. These
are:
• the list is dynamic and hence can be resized
based on the requirement
• Secondly, the insertion is O(1).
Singly Linked List
Singly linked lists are one of the most
primitive data structures you will learn in this
tutorial. Here each node makes up a singly
linked list and consists of a value and a reference
to the next node (if any) in the list.
Insertion of Values in Linked List
In general, when people talk about insertion
concerning linked lists of any form, they implicitly
refer to the adding of a node to the tail of the list.
Adding a node to a singly linked list has only two
cases:
• head = fi, in which case the node we are
adding is now both the head and tail of the list; or
• we simply need to append our node onto the
end of the list updating the tail reference
appropriately
Algorithm for inserting values to Linked List

• also Add(value)
• Pre: value is the value to be added to the list
• Post: value has been placed at the tail of the list
• n <- node(value)
• if head = fi
• head <- n
• tail<- n
• else
• Next <- n
• tail <- n
• end if
• end Add
Searching in Linked Lists
Searching a linked list is straightforward: we
simply traverse the list checking the value we
are looking for with the value of each node in
the linked list.
• also Contains(head, value)
• Pre: the head is the head node in the list
• value is the value to search for
• Post: the item is either in the linked list, true; otherwise false
• n <- head
• While n 6= fi and n.Value 6= value
• n <- n.Next
• end while
• if n = fi
• return false
• end if
• return true
• end Contains
Deletion in Linked Lists
Deleting a node from a linked list is straight-forward,
but there are some cases that you need to account for:
• the list is empty; or
• the node to remove is the only node in the linked
list; or
• we are removing the head node; or
• we are removing the tail node; or
• the node to remove is somewhere in between the
head and tail; or
• the item to remove doesn't exist in the linked list
Algorithm for Deletion
• algorithm Remove(head, value)
• Pre: the head is the head node in the list
• value is the value to remove from the list
• Post: value is removed from the list, true; otherwise false
• if head = fi
• return false
• end if
• n <- head
• If n.Value = value
• if the head = tail
• head <- fi
• tail <- fi
• else
• HHead <- head.Next
• end if
• return true
• end if
• while n.Next 6= fi and n.Next.Value 6= value
• n <- n.Next
• end while
• if n.Next 6= fi
• if n.Next = tail
• tail <- n
• end if
• // this is only case 5 if the conditional on line 25 was false
• Next <- n.Next.Next
• return true
• end if
• return false
• end Remove

You might also like