0% found this document useful (0 votes)
59 views

Introduction To DATA STRUCTURES: Basic Principles

This document provides an introduction to data structures and linked lists. It discusses how data structures organize and store data to be used efficiently by programs. Linked lists are described as a data structure where each data record contains a link to the next record, allowing dynamic insertion and removal of nodes. The document outlines different types of linked lists like singly-linked, doubly-linked, and circular linked lists. It compares the properties of linked lists to arrays, noting linked lists can dynamically allocate memory as needed while arrays have fixed sizes.

Uploaded by

Abhi Mahajan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Introduction To DATA STRUCTURES: Basic Principles

This document provides an introduction to data structures and linked lists. It discusses how data structures organize and store data to be used efficiently by programs. Linked lists are described as a data structure where each data record contains a link to the next record, allowing dynamic insertion and removal of nodes. The document outlines different types of linked lists like singly-linked, doubly-linked, and circular linked lists. It compares the properties of linked lists to arrays, noting linked lists can dynamically allocate memory as needed while arrays have fixed sizes.

Uploaded by

Abhi Mahajan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction to DATA STRUCTURES

In computer science, a data structure is a particular way of storing and


organizing data in a computer so that it can be used efficiently.
Different kinds of data structures are suited to different kinds of applications, and
some are highly specialized to specific tasks. For example, B-trees are particularly
well-suited for implementation of databases, while compiler implementations usually
use hash tables to look up identifiers.
Data structures are used in almost every program or software system. Specific data
structures are essential ingredients of many efficient algorithms, and make possible
the management of huge amounts of data, such as large databases and internet
indexing services. Some formal design methods and programming
languages emphasize data structures, rather than algorithms, as the key organizing
factor in software design.

Basic Principles

Data structures are generally based on the ability of a computer to fetch and store data
at any place in its memory, specified by an address — a bit string that can be itself
stored in memory and manipulated by the program. Thus the record and array data
structures are based on computing the addresses of data items with arithmetic
operations; while the linked data structures are based on storing addresses of data
items within the structure itself. Many data structures use both principles, sometimes
combined in non-trivial ways (as in XOR linking).

Abstract Data Structures

The implementation of a data structure usually requires writing a set


of procedures that create and manipulate instances of that structure. The efficiency of
a data structure cannot be analyzed separately from those operations.
This observation motivates the theoretical concept of an abstract data type, a data
structure that is defined indirectly by the operations that may be performed on it, and
the mathematical properties of those operations (including their space and time cost).
Introduction to Linked List
In computer science, a linked list is a data structure that consists of a sequence of data
records such that in each record there is a field that contains a reference (i.e., a link) to
the next record in the sequence.

A linked list whose nodes contain two fields: an integer value and a link to the next
node
Linked lists are among the simplest and most common data structures; they provide
an easy implementation for several important abstract data structures,
including stacks, queues, associative arrays, and symbolic expressions.
The principal benefit of a linked list over a conventional array is that the order of the
linked items may be different from the order that the data items are stored in memory
or on disk. For that reason, linked lists allow insertion and removal of nodes at any
point in the list, with a constant number of operations.
On the other hand, linked lists by themselves do not allow random access to the data,
or any form of efficient indexing. Thus, many basic operations — such as obtaining
the last node of the list, or finding a node that contains a given datum, or locating the
place where a new node should be inserted — may require scanning most of the list
elements.
Linked lists can be implemented in most languages. Languages such
as Lisp and Scheme have the data structure built in, along with operations to access
the linked list. Procedural languages, such as C, or object-oriented languages, such
as C++ and Java, typically rely on mutable references to create linked lists.

Linear and circular lists


In the last node of a list, the link field often contains a null reference, a special value
that is interpreted by programs as meaning "there is no such node". A less common
convention is to make it point to the first node of the list; in that case the list is said to
be circular or circularly linked; otherwise it is said to be open or linear.

A circular linked list

Singly-, doubly-, and multiply-linked lists


Singly-linked lists contain nodes which have a data field as well as a next field, which
points to the next node in the linked list.

A singly-linked list whose nodes contain two fields: an integer value and a link to the
next node
In a doubly-linked list, each node contains, besides the next-node link, a second link
field pointing to the previous node in the sequence. The two links may be
called forward(s) andbackwards, or next and prev(ious).

A doubly-linked list whose nodes contain three fields: an integer value, the link
forward to the next node, and the link backward to the previous node
The technique known as XOR-linking allows a doubly-linked list to be implemented
using a single link field in each node. However, this technique requires the ability to
do bit operations on addresses, and therefore may not be available in some high-level
languages.
In a multiply-linked list, each node contains two or more link fields, each field being
used to connect the same set of data records in a different order (e.g., by name, by
department, by date of birth, etc.). (While doubly-linked lists can be seen as special
cases of multiply-linked list, the fact that the two orders are opposite to each other
leads to simpler and more efficient algorithms, so they are usually treated as a
separate case.)
In the case of a doubly circular linked list, the only change that occurs is the end, or
"tail" of the said list is linked back to the front, "head", of the list and vice versa.

Why Linked Lists?


Linked lists and arrays are similar since they both store collections of data. The
terminology is that arrays and linked lists store "elements" on behalf of "client" code.
The
specific type of element is not important since essentially the same structure works to
store elements of any type. One way to think about linked lists is to look at how arrays
work and think about alternate approaches.
Array Review
Arrays are probably the most common data structure used to store collections of
elements. In most languages, arrays are convenient to declare and the provide the
handy
[ ] syntax to access any element by its index number. The following example shows
some
typical array code and a drawing of how the array might look in memory. The code
allocates an array int scores[100], sets the first three elements set to contain
the
numbers 1, 2, 3 and leaves the rest of the array uninitialized...
void ArrayTest() {
int scores[100];
// operate on the elements of the scores array...
scores[0] = 1;
scores[1] = 2;
scores[2] = 3;
}
3
Here is a drawing of how the scores array might look like in memory. The key point is
that the entire array is allocated as one block of memory. Each element in the array
gets
its own space in the array. Any element can be accessed directly using the [ ] syntax.

Once the array is set up, access to any element is convenient and fast with the [ ]
operator. (Extra for experts) Array access with expressions such as scores[i] is
almost always implemented using fast address arithmetic: the address of an element is
computed as an offset from the start of the array which only requires one
multiplication and one addition.
The disadvantages of arrays are...
1) The size of the array is fixed — 100 elements in this case. Most often this size is
specified at compile time with a simple declaration such as in the example above .
With a little extra effort, the size of the array can be deferred until the array is created
at runtime, but after that it remains fixed. (extra for experts) You can go to the trouble
of dynamically allocating an array in the heap and then dynamically resizing it with
realloc(), but that requires some real programmer effort.
2) Because of (1), the most convenient thing for programmers to do is to allocate
arrays which seem "large enough" (e.g. the 100 in the scores example). Although
convenient, this strategy has two disadvantages: (a) most of the time there are just 20
or 30 elements in the array and 70% of the space in the array really is wasted. (b) If
the program ever needs to process more than 100 scores, the code breaks. A surprising
amount of commercial code has this sort of naive array allocation which wastes space
most of the time and crashes for special occasions. (Extra for experts) For relatively
large arrays (larger than 8k bytes), the virtual memory system may partially
compensate for this problem, since the "wasted" elements are never touched.
3) (minor) Inserting new elements at the front is potentially expensive because
existing elements need to be shifted over to make room. Linked lists have their own
strengths and weaknesses, but they happen to be strong where arrays are weak. The
array's features all follow from its strategy of allocating the memory for all its
elements in one block of memory. Linked lists use an entirely different strategy.
As we will see, linked lists allocate memory for each element separately and only
when necessary.

You might also like