0% found this document useful (0 votes)
34 views6 pages

CSC 217 Week 4

The document discusses records and linked lists. It defines records as collections of related data items that can contain different data types. Linked lists are sequences of data structures connected by links, where each link contains a pointer to the next link. The document describes creating, inserting, deleting, and reversing items in linked lists.

Uploaded by

adenoch113
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views6 pages

CSC 217 Week 4

The document discusses records and linked lists. It defines records as collections of related data items that can contain different data types. Linked lists are sequences of data structures connected by links, where each link contains a pointer to the next link. The document describes creating, inserting, deleting, and reversing items in linked lists.

Uploaded by

adenoch113
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Module 4

RECORDS AND LISTS


4.1 Records
Records will be easy to understand if we compare them with the arrays. Records are
like one-dimensional arrays in that they are compared of series of related data items.
However, they differ from arrays in that all the elements of an array are of one type while the
elements of the record are usually not of one type; consider the simple record item relating to
an individual examination candidate:
Candidate_number: integer
Candidate_name: string
Candidate_mark: real
Although the three data items belong together they cannot be stored in an array because they
are of different types. The answer is to store them in a record.
The simple record declaration is of the form.
Candidate:=record
Candidate_number : interger
Candidate_name : string
Candidate_mark : real
End record

The elements of a record are called field. In BASIC, you first define a record, then declare an
array whose Elements are the record. You can then perform any operation you can perform
on an array on this array of records. To access the fields above , use the dot notation below:
REC (1).Candidate_number =20
REC (1).Candidate_name = "Obi"
REC (1).Candidate_mark = 76
The example above assigns value to the first record REC(1) which is similar to the
array element 1. But because this is a record, you have to give the fields different values as
indicated. You treat the other records the same way, with a subscript, and values for each
field. You can also work with the entire array by doing a record assignment, REC
(2)=REC(3).
A record is a collection of fields, possibly of different data types, typically in fixed
number and sequence. Records are distinguished from arrays by the fact that their number of
fields is typically fixed, each field has a name, and that each field may have a different type.
A record type is a data type that describes such values and variables. Most modern computer
languages allow the programmer to define new record types. The definition includes
specifying the data type of each field and an identifier (name or label) by which it can be
accessed.
Records can exist in any storage medium, including main memory and mass storage
devices such as magnetic tapes or hard disks. Records are a fundamental component of most
data structures, especially linked data structures. Many computer files are organized as arrays
of logical records, often grouped into larger physical records or blocks for efficiency.
4.2 Lists
List provides a flexible way of handling data items in order. Changes to the order can be
achieved with minimal data movement and little loss of storage spaces. Example: The
sentence “Kossy does not like cake” is written as a list as shown below:

HEAD

KOSSY DOES NOT LIKE CAKE

Fig. 4.a

We regard each word in our sentence as a data item or datum that is linked to the next datum
by a pointer. Datum plus pointer, datum plus pointer make one element or mode of list. The
last pointer in the list is a terminator. The list may be stored in an array of records.

4.2.1 Deletion from a list: When elements are deleted from a list, the freed storage can be
reused. In the example given above, “NOT” has been deleted so the list now reads “KOSSY
DOES LIKE CAKE”

HEAD

KOSSY DOSE NOT LIKE CAKE

4.2.2 Insertions on a list: The free storage pointer is used for insertions.
In the example, the list is changed to “KOSSY DOES NOT LIKE CREAM CAKE” by
inserting “CREAM”
HEAD

KOSSY DOSE LIKE CAKE

CREAM

4.3 Linked Lists


A linked list is a sequence of data structures, which are connected together via links.
Linked List is a sequence of links which contains items. Each link contains a connection to
another link. Linked list is the second most-used data structure after array. Following are the
important terms to understand the concept of Linked List.
• Link − Each link of a linked list can store a data called an element.
• Next − Each link of a linked list contains a link to the next link called Next.
• LinkedList − A Linked List contains the connection link to the first link called First.
Linked List Representation
Linked list can be visualized as a chain of nodes, where every node points to the next node.

As per the above illustration, following are the important points to be considered.
• Linked List contains a link element called first.
• Each link carries a data field(s) and a link field called next.
• Each link is linked with its next link using its next link.
• Last link carries a link as null to mark the end of the list.

Types of Linked List


Following are the various types of linked list.
• Simple Linked List − Item navigation is forward only.
• Doubly Linked List − Items can be navigated forward and backward.
• Circular Linked List − Last item contains link of the first element as next and the first
element has a link to the last element as previous.
Basic Operations
Following are the basic operations supported by a list.
• Insertion − Adds an element at the beginning of the list.
• Deletion − Deletes an element at the beginning of the list.
• Display − Displays the complete list.
• Search − Searches an element using the given key.
• Delete − Deletes an element using the given key.

Insertion Operation
Adding a new node in linked list is a more than one step activity. We shall learn this with
diagrams here. First, create a node using the same structure and find the location where it has
to be inserted.
Imagine that we are inserting a node B (NewNode), between A (LeftNode) and C
(RightNode). Then point B.next to C –
NewNode.next −> RightNode;
It should look like this –

Now, the next node at the left should point to the new node.
LeftNode.next −> NewNode;

This will put the new node in the middle of the two. The new list should look like this –

Similar steps should be taken if the node is being inserted at the beginning of the list. While
inserting it at the end, the second last node of the list should point to the new node and the
new node will point to NULL.

Deletion Operation
Deletion is also a more than one step process. We shall learn with pictorial representation.
First, locate the target node to be removed, by using searching algorithms.

The left (previous) node of the target node now should point to the next node of the target
node −
LeftNode.next −> TargetNode.next;
This will remove the link that was pointing to the target node. Now, using the following code,
we will remove what the target node is pointing at.
TargetNode.next −> NULL;

We need to use the deleted node. We can keep that in memory otherwise we can simply
deallocate memory and wipe off the target node completely.

Reverse Operation
This operation is a thorough one. We need to make the last node to be pointed by the head
node and reverse the whole linked list.

First, we traverse to the end of the list. It should be pointing to NULL. Now, we shall make it
point to its previous node –

We have to make sure that the last node is not the lost node. So we'll have some temp node,
which looks like the head node pointing to the last node. Now, we shall make all left side
nodes point to their previous nodes one by one.
Except the node (first node) pointed by the head node, all nodes should point to their
predecessor, making them their new successor. The first node will point to NULL

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy