Вы находитесь на странице: 1из 50

Unit-1 [DATA STRUCTURES] I YEAR II SEM

GUDLAVALLERU ENGINEERING COLLEGE


(An Autonomous Institute with Permanent Affiliation to JNTUK, Kakinada)
Seshadri Rao Knowledge Village, Gudlavalleru – 521 356.

Department of Information Technology

HANDOUT
On
DATA STRUCTURES

1 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

Vision

To be a centre of innovation by adopting changes in Information Technology, imparting


quality education, research to produce visionary computer professionals and entrepreneurs.
Mission

 To provide an academic environment in which students are given the essential resources for
solving real world problems and work in multidisciplinary teams.
 To impart value based education and research among students, particularly belonging to
rural areas, for their sustained growth in technological aspects and leadership.
 To collaborate with the industry for making the students adoptable to evolving changes in
Information Technology and related areas

Program Educational Objectives

PEO1: To exhibit analytical skills in modeling and solving computing problems by applying
mathematical, scientific and engineering knowledge and to pursue their higher studies.

PEO2: To communicate effectively with multidisciplinary teams to develop quality software systems
with an orientation towards research and development for lifelong learning.

PEO3: To use emerging technologies in project development to fulfill industry and societal needs for
the growth of global economy following professional ethics.

Program Outcomes:
Graduates of the Information Technology Program will have an ability to

a Apply the knowledge of mathematics, science and computing in the core information
technologies.
b Analyze real time problems using basic engineering knowledge and develop algorithms suitable
for computing environment.
c Design and demonstrate a system or a model that meets the desired specifications considering
economic, environmental, and social aspects.
d Apply latest technical concepts and practices in IT related fields, like user interface creation,
information management, programming, networking and web systems.

2 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

e Select and use current techniques, skills, and tools necessary for computing practice and provide
IT-based solutions for the user environment.
f Provide engineering solutions to meet the local and global challenges.
g Recognize the importance of professional engineering skills, by inventing sustainable
methodologies in software development useful for the society and environment.
h Understand professional, ethical, legal, security, social, and economic issues and responsibilities
i Work individually or as a member with responsibility to function on multi-disciplinary teams.

j Communicate effectively in oral and writing, including documentation of hardware and software
systems.
k Keep abreast of latest trends in the computing discipline and engage in life-long learning.

l Understand the engineering and project management principles and apply them into their own
enterprise or work environment for effective financial management.

3 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

HANDOUT ON DATA STRUCTURES USING C


Class& Sem. : I B.Tech – II Semester Year: 2016-17
Branch : I.T Credits: 3
============================================================================
1. Brief History and Scope of the Subject
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. Data structures provide a means to manage huge amounts of data efficiently, such as large
databases and internet indexing services. Usually, efficient data structures are a key to designing
efficient algorithms. Some formal design methods and programming languages emphasize data
structures, rather than algorithms, as the key organizing factor in software design.
2. Pre-Requisites
 Knowledge of C programming language that supports pointers for referencing.

3. Course Objectives:
 To introduce the concepts of linear and non-liner data structures.
• To choose appropriate data and storage structures for solving problems.
• To impart the concepts of searching and sorting.
4. Course Outcomes:
• Represent linear and nonlinear data structures using arrays and linked lists.
•Perform insertion, deletion, searching, updating and traversal operation on linear and non-
linear data structures.
• Implement searching and internal sorting techniques on data sets.
• Choose appropriate data and storage structures for solving problems.

5. Mapping of Course Outcomes with Program Outcomes:


a b c d e f g h i j k l
CO1 M H M H H M H M H
CO2 M H M H H M H M H
CO3 M H M H H M H L H
CO4 M H M H H M H M H
6. Prescribed Text Books
a. Debasis samanta, Classic Data Structures, PHI, 2nd edition, 2011.
b. Richard F, Gilberg , Forouzan, Data Structures, 2nd edition, , Cengage
7. Reference Text Books
a. Seymour Lipschutz, Data Structure with C, TMH.
b. G. A. V. Pai, Data Structures and Algorithms, TMH, 2008.
c. Horowitz, Sahni, Anderson Freed, Fundamentals of Data Structure in C, University Press, 2nd
edition

4 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

8. Urls and Other E-Learning Resources


a. Https://www.coursera.org/learn/data-structures
b. Http://www.studytonight.com/data-structures/
c. Http://www.indiabix.com/technical/data-structures/
9. Digital Learning Materials:
 Http://freevideolectures.com/Course/2279/Data-Structures-And-Algorithms/2#
 Http://nptel.ac.in/courses/106102064/1

5 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

10. Lecture Schedule / Lesson Plan


No. Of Periods
Topic
Theory Tutorial
UNIT –1: Linked Lists
Concepts of data structures 1
Storage Structure and File Structures,
Primitive data types,Non Primitive datatypes-linear & Non Linear data types -
Operations on Data Structures,ADT
Linear List 1
Single linked list-operations 4
2
Circular linked list 2
Double linked list 3
2
Applications of linear lists 1
12 4
UNIT – 2: Stacks
Array representation of stack and performing operations 2
2
Linked representation of stack and performing operations 2
Conversion of infix expression into postfix expression 1
2
Evaluation of Postfix expression 2
7 4
UNIT – 3: Queues
Array representation of queue and performing operations 2
2
Linked representation of queue and performing operations 2
Circular queue 1
2
Dequeue 2
7 4
UNIT – 4:Binary- Trees
Basic tree -Properties 2
-
Representation of Binary Trees usinglinked lists and operations 1
Binary search trees: Properties, ADT 1
2
BST operations: Search, insertion, Deletion, Traversals 2
Heap-Properties ,ADT 2
2
Representation of Heap Tree using Array & Linked list 2
Heap Operations 1
2
Applications-Expression Trees, Priority Queues and Histogramming. 2
13 6
UNIT – 5: Searching and Sorting
Linear search, Binary search 1
2
Internal sorting: Basic concepts 1
Insertion sort, Selection sort 1
Bubble sort, Quick sort 1
2
Radix Sort 3
Merge Sort, heap Sorting Methods 1
Introduction to external Searching and sorting 2 2
10 6
UNIT – 6: Graphs
Graphs-Basic concepts, ADT, Representations of graphs 1
2
Graph traversals Breadth First Search (BFS), Depth First Search (DFS) 2
Graph Algorithms- Dijkstra’s Prims and Kruskal’s Algorithms. 2 2
7 4
Total No.of Periods: 56 28

6 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

11. Seminar Topics:


 Recursion Using Stacks
 Postfix evaluation using stack
 Circular Linked List
 Binary Search tree
 Graph Traversals

UNIT –I
Objective:

 To gain knowledge on linked lists.


Syllabus:

Unit-I: Linked lists


Introduction-Concept of data structures, storage structure and file structure; Primitive data
Structures;Non-primitive data structures-Linear and Non-linear;Operations on data structures-Create,
Insert, Delete, Search, Update and Traverse; Abstract Data Type(ADT).
Linear Lists-ADT, array representation and performing operations,Single linked list,Circular linked
list,double linked list,Circular double linked list.

Learning Outcomes:

At the end of the unit student will be able to:


1. Define a self-referential structure.
2. Describe about linked lists.
3. Implement the operations on linked lists.
4. Choose an appropriate linked list for a given problem.
5. Distinguish between single, double and circular linked lists.
Learning Material

 Concepts of Data Structure:


 Data:Data means value (or) set of values.
Eg: 34,120, 20/06/2016
 Information: Information can be defined as meaningful data (or) processed data.
Eg: 34 is age of a person
120 is price of a book
20/06/2016 is sem starting date
 Difference between data and information

7 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

Information_1
Information_2Inf
Data Process ormation_3

Information_ n

Fig: Relationship between Data and Information

Where data doesn’t have any meaning, but information have some meaning.
 Data Structure: The logical (or) mathematical model of particular organization of data is
called as data structure.
 Storage Structure:
A data storage structures is a means of organizing data as blocks in secondary memory that optimizes I/O
read/write operations.
 Need for storage structure:
 The main purpose for computer systems is to execute programs.
 These programs together with the data they act upon must be stored in main memory
during execution.
 Storage Hierarchy:
 Primary Storage: Fastest media but volatile (cache, main memory).
 Secondary Storage: next level in hierarchy, non-volatile, moderately fast access timealso
called on-line storage
 Ex: flash memory, magnetic disks
 Tertiary Storage: lowest level in hierarchy, non-volatile, slow access time also called off-
line storage.

8 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

Reg

cache

main memory

magnetic disk
magnetic tapes or Optical
tapes
Fig . Memory Hierarchy

Storage Purpose Size Cost Time Application


Register The purpose of a Its size is in Its cost is It takes A processor
register is to hold bits low only a few register (CPU
an instruction, a storage clock register) is
address, or any kind of cycles time one of a small
data . set of data
holding places
that are part of
the computer
processor
Cache The purpose of cache Its size is in Its cost is Its time is Cache
memory is to store Kilo Bytes very high in nano memory is the
program instructions seconds fastest
that are frequently re- but 10 memory
referenced by software times placed
during operation. faster than between cpu
main and main
memory memory in the
data transfer
operations
Main Primary storage, also Megabytes Its cost is Its time is Its is used as
memory known as main storage to Giga too high in nano RAM(Random
or memory, is the area bytes compared seconds access
in a computer in which to cache memory)
data is stored for quick
access by the
computer's processor.
Hard disks The main purpose of a Gigabytes& Its cost is Its time is To hold
hard disk drive is to terabytes low in milli programs and
operate as a storage compared seconds data files on a
device for data. A hard to cache smaller and
disk stores data and maon easier to get to
internally by recording memory the system
9 Department of Information Technology | GEC, Gudlavalleru
Unit-1 [DATA STRUCTURES] I YEAR II SEM

it on metallic plates that Ex:


are charged SATA
electromagnetically. Harddisk.
Magnetic A magnetic disk is a Its Its cost is Its time is Holding data
disks storage device maximum too low in seconds. files such as
that uses a capacity is compared Word
magnetization process 1.44 Mega to others processing
to write, rewrite and Bytes documents,
access data. graphics,
video, sound,
database etc.

 File Structure/File Organization:


Each file is a sequence of records.A record is a sequence of files.
 The physical arrangement of data in a file into records and pages on the disk.
 File organization determines the set of access methods for
 Storing and retrieving records from a file
 Therefore, ‘file organization’ synonymous with ‘access method’
 We study three types of file organization
 Ordered or sequential files
 Hash files
 Unordered or Heap files
 We examine each of them in terms of the operations we perform
 Insert a new record
 Search for a record (or update a record)
 Delete a record.
1. Sequential Files:
Records are sorted on the values of one or more fields
 Ordering field – the field on which the records are sorted.
 Ordering key – the key of the file when it is used for record sorting.
 A collection of records, Stored in key sequence.
 Adding /deleting record requires making new file
 Used as master files.

10 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

2. Indexing
Index - a data structure that allows to locate particular records in a file more quickly
 Very similar to the index at the end of a book to locate various topics covered in the book
Types of Index
 Clustering index – one clustering index per file – data file is ordered on a non-key field
and the index file is built on that non-key field
 Primary index – one primary index per file
 Sparse index – has only some of the search key values in the file
 Dense index – has an index corresponding to every search key value in the file
 Secondary index – many secondary indexes per file
 Indexed sequential files are important for applications where data needs to be accessed.....
 Sequentially.
 Randomly using the index.
 An indexed sequential file can only be stored on a random access device
e.g. Magnetic disc, CD.

3. Hash File:
Is an array of buckets.
 Given a record, r a hash function, h(r) computes the index of the bucket in which record r
belongs
 H uses one or more fields in the record called hash fields
 Hash key - the key of the file when it is used by the hash function
• Insert Operation: Fast – because the hash function computes the index of the bucket to which the
record belongs. If that bucket is full you go to the next free one.

• Search Operation:Fast – because the hash function computes the index of the bucketPerformance
may degrade if the record is not found in the bucket suggested by hash function

11 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

• Delete Operation:Fast – once again for the same reason of hashing function being able to locate
the record quick.
4. Heap File:
Records are stored in the same order in which they are created.
 Insert operation:Fast – because the incoming record is written at the end of the last page of the file
 Search (or update) operation: Slow – because linear search is performed on pages
 Delete Operation: Slow – because the record to be deleted is first searched for Deleting the record
creates a hole in the pagePeriodic file compacting work required to reclaim the wasted space

 Primitive data structure:


Primitive data structures are the fundamental data types, which are supported by a
programming language .The primitive data types are used to represent single values.
 Integer: This is used to represent a number without decimal point. Examples are 5, 54.
 Float and Double: This is used to represent a number with decimal point. Eg: 45.1, 67.3
 Character: This is used to represent single character. Eg: ‘C’, ‘a’
 String: This is used to represent group of characters. Eg: "data structure"
 Boolean: This is used represent logical values either true or false.
 Non Primitive data structure:
Non Primitive data structures are created using Primitive data structures. These data types are used
to store group of values.
 Examples of such data structures include linked lists, stacks, trees, and graphs.
 Non-primitive data structures can further be classified into two categories: linearand non-linear
data structures.
 Linear Data Structures:A linear data structure traverses the data elements sequentially, in
which only one data element can directly be reached.
Eg: Arrays, Stacks, Queues, Linked Lists.
 Non Linear Data Structures:Every data item is attached to several other data items in a way
that is specific for reflecting relationships. The data items are not arranged in a sequential
structure.
E.g:Trees and graphs

12 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

Classic data structures

Primitive Data Structures Non-primitive data structures

int Linear Data Structures Non-Linear Data Structures

float Arrays Trees

Stacks Graphs
Boolean
Queues
Tables
char Linked Lists Sets
Figure: Classification of Classic Data Structures.
 OPERATIONS ON DATA STRUCTURES:
The basic operations that are performed on data structures are as follows:
1) Traversing:It means to access each data item exactly once so that it can be processed.
For example: to print the names of all the students in a class.
2) Searching:It is used to find the location of one or more data items that satisfy the given constraint.
Such a data item may or may not be present in the given collection of data items.
For example, to find the names of all the students who secured 100 marks in mathematics?
3) Inserting:It is used to add new data items to the given list of data items.
For example, to add the details of a new student who has recently joined the course?
4) Deleting: It means to remove (delete) a particular data item from the given collection of data items.
For example, to delete the name of a student who has left the course.
5) Sorting: Dataitems can be arranged in some order like ascending order or descending order
depending on the type of application.
For example, arranging the names of students in a class in an alphabetical order, or calculating the top
three winners by arranging the participants’ scores in descending order and then extracting the top
three.
6) Merging: Lists of two sorted data items can be combined to form a single list of sorted data items.

13 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

 Abstract Data Type(ADT):


 Abstract data type is a mathematical model or concept that defines a data type
logically.
 ADT specifies a set of data and collection of operations that can be performed on that
data.
 The definition of ADT only mentions What Operations are to be Performed but not
how it is implemented.
 It does not specify how data will be organized in memory and what algorithms will be
used for implementing the operations.
 It is called “abstract” because it gives an implementation independent view
(overview).
 The process of providing only the essentials and hiding the details is known as
Abstraction.
 LIST ADT:
 ADT is a collection of data and a set of operations that can be performed on the data.
 List is an ordered set of elements.
 The general form of the list is A1, A2, A3, ........ , AN .
o A1 - First element of the List .
o AN - Last element of the List .
o N - Size of the list.
 If the element at position i is Ai then its successor is Ai+1 and its predecessor is Ai-1.
 List ADT :A List contains elements of same type arranged in sequential order and Following
operations can be performed.
o Insertion.
o Deletion.
o Searching.
o Sorting.
o Traversing.
 Array Representation:
 It uses an array to store the elements of linear list.
 Individual element is located in the array using a mathematical formula.
 Typical formula:Location(n) = n – 1
 N-th element of the list is in position n-1 of the array.

14 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

Actual number
of items Unused Spaces

size Array with [0. . m] of locations

n a1 a2 a3 ……………… an. . . . . . . . .

0 1 2 n-1 m-1
Internal of the list ADT, Array Version

 Operations on List Using Arrays:


1. Traversing a List
2. Insertion of an element in to List.
3. Deletion of anelement from List.
4. Search for anelement in List.
5. Sorting a elements in a List
1. Traversing a List:
 Traversal is an operation in which each element of a list, stored in an array, is visited.
 The travel proceeds from the zeroth element to the last element of the list.
 Processing of the visited element can be done as per the requirement of a problem.
Algorithm Traverse_ Array()
Input: An Array ‘A’ with elements.
Output: Visiting of every element in List.
Data Structure:Array A [L … U].
1. I=L
2. For L to U do
A) print “A[i]”
3. End loop
End Traverse_Array().
2. Insertion of an element in to List:
 Insertion is an operation in which a new element is added at a particular place in a list.
 When the list is represented using an array, the element can be added very easily at the end
of the list provided the space is available.
AlgorithmInsert(key, location)
Input: Key is the item, Location is the index of the element where it is to be inserted.
Output: Array enriched with KEY.
Data Structure: An array A[L…….U].
1.if(A[U]!=NULL) then
1. Print “Array is full: No insertion Possible”

15 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

2.exit //End of Execution


2.else
1. I=U
2.for U to location do
1.A[i+1]=A[i]
3.endloop
4.A[Location]=Key
5.U=U+1
3.End If
4.Stop.
The algorithm Insert only checks the last element for vacancy . But an array may be
empty from any i-thposition ; in the case number of push down can be reduced instead of pushing
down the entire trailing part.
3. Deletion of a element from List:
 It is used to delete a particular element from an array.
 The element will be deleted by overwriting it with its subsequent element then is to be deleted.
Example:

Remove the item at 5th position


size

8 a 1 a2 a3 a4 a5 a6 a7 a8 ………..

size

7 a 1 a2 a3 a4 a6 a7 a8 ………..

Algorithm DELETE(KEY)
Input: Key the element to be deleted.
Output: Slimed array without a KEY.
Data structure:An array A[L…..U].
Steps:
1.I = Search_array(A,KEY)
2.If(i=0) then
1. Print “Key is not found:No deletion”
2. Exit.
3. Else
1. For L to U do
1. A[i] =A[i+1] //replace the element by its successor
2.Endloop
4. Endif
5. A[U]=NULL

16 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

6. U=U-1
7.stop.
4. Search for a element in List:
 It is applied to search an element of interest in a List.
Algorithm Search_Array(Key)
Input: Key is the element to be searched.
Output: Index of Key in A or a message on Failure.
DataStructure:An Array A[L …… U].
Steps:
1.i=L,found=0,location=0
2.for I to L do
1.If compare(A[i],key) =True then
1.found=1
2.location=i
3.endloop
4. If found =0 then
Print “Search is un successful :KEY is not in the list”
5.else
Print “Search is Successful :Key is in the array at location”,location
6.endif
7.return (location)
8.stop.

5. Sorting a elements in a List:


 Sorting will performed on,if performed on an array ,will sort it in a specified
order(ascending/decending order).
The following algorithm is used to store the elements of an integer array in ascending order.
Algorithmsort_ARRAY()
Input: An Array with integer data.
Output: An array with sorted elements in an order according to ORDER(Ascending)
Data Structures: An integer array A[L…..U].
Steps:
1. i=U;
2. For i to L do
1. J=L

17 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

2. For j to ido
1. If ORDER(A[j] ,A[j+1]) = FALSE
i. Swap(A[j],A[J+1])
2. End if
3. Endloop
3. Endloop
4. Stop.
Where, Order () is a procedure to test whether two elements are in order and
SWAP () - interchange the elements.

…………… A[j] A[j]+1 ……………

. .. . . . . . . A[j]+1 A[j] . . . . . . . . . ..

Fig: Swapping of 2 elements.


 LINKED LISTS:
 In arrays once memory is allocated, it can’t extended any more. So array is known as static
Data Structure.
 Linked List is dynamic Data Structure, where amount of memory required can be vary during
it use.
Definition:A linked list is an ordered collection of finite, homogeneous data elements
called nodes.
 Where the linear order is maintained by means of links or pointers.
 The representation of node is as follows:

Data Link

Node: an element in Linked List


 A node consists of two parts. i.e. Data part and Link part.
 The data part contains actual data to be represented.
 The link part is also referred as address field, which contains address of the next node.

• Representation of Linked List in memory:


There are two ways to represent a Linked List in memory.
1. Static memory allocation using arrays.
2. Dynamic allocations using pool of storage.

18 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

1. Static memory allocation using arrays


This representation maintains two arrays.One array used for Data and another array is used for
link.

header N1 N2 N3 N4

X45 102 43 105 46 103 44 101X

Fig: SLL with 4 nodes

The static representation for a linked list shown in the above figure is given in the following figure.

Array1 Array 2
Address
(Data) (Link)
41

42

43 105 46

44 101 X

45 102 43

46 103 44

47

Static Representation of SLL using arrays

2. Dynamic allocations using pool of storage


 The efficient way of representation of linked list is using pool of storage.
 In this method memory bank, memory manager and garbage collector is available.

Memory bank:It is a collection of free memory spaces.

Memory manager:It is a program.

 Whenever a linked list requires a anode, then request is placed to memory manager.
 If the required node is available in the memory bank, then that node is send to caller.
 If the required node is not available in the memory bank, then memory manager send NULL to
caller.

Garbage collector:collect the unused nodes in the linked list and send back to memory bank.
19 Department of Information Technology | GEC, Gudlavalleru
Unit-1 [DATA STRUCTURES] I YEAR II SEM

Types of linked lists

1. Single Linked List (SLL)

2. Double Linked List (DLL)

3. Circular Linked List (CLL)

 Single Linked List (SLL):


 In SLL, each node contains only one link, which points to the next node in the list.
 The pictorial representation of SLL is as follows.

header N1 N2 N3 N4

X 102 105 103 101X

SLL with 4 nodes

 Here header is an empty node, i.e. Data part is NULL, represented by X mark.
 The link part of the header node contains address of the first node in the list.
 In SLL, the last node link part contains NULL.
 In SLL we can move from left to right only. So SLL is called as one way list.

 Operations on Single Linked List:

1. Traversing a SLL

2. Insertion of a node in to SLL

3. Deletion of a node from SLL

4. Search for a node in SLL

5. Reversing a SLL

1. Traversing a SLL:

Traversing a SLL means, visit every node in the list starting from first node to the last node.

AlgoritmSLL_Traverse(header)
Input: header is a header node.
Output: Visiting of every node in SLL.
1. Ptr=header
2. While(ptr.link != NULL)
A) ptr=ptr.link go to step(b)
B) print “ptr.data”

20 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

3. End loop
End SLL_Traverse
2. Insertion of a node into SLL:

 The Insertion of a node in to SLL can be done in various positions.


I) Insertion of a node into SLL at beginning.
Ii) Insertion of a node into SLL at ending.
Iii) Insertion of a node into SLL at any position.
 For insertion of a node into SLL, we must get node from memory bank.
 The procedure for getting node from memory bank is as follows:
Procedure for getnewnode( )
1. Check for availability of node in memory bank
2. If ( AVAIL = NULL)
A) print “Required node is not available in memory”
B) return NULL
3. Else
A) return address of node to the caller
4. End if
End Procedure for getnewnode
i) Insertion of a node into SLL at beginning
AlgoritmSLL_Insert_Begin(header,x)
Input: header is a pointer to the header node, x is data part of new node to be inserted.
Output: SLL with new node inserted at beginning.
1. New=getnewnode( )
2. If(new = = NULL)
A) print “required node was not available in memory, so unable to process”
3. Else
A) new.link=header.link /* 1 */
B) header.link=new /* 2 */
C) new.data=x
4. End if
End SLL_Insert_Begin

1. Link part of new node is replaced with address of first node in list, i.e. Link part of header node.
2. Link part of header node is replaced with new node address

21 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

header N1 N2 N3

X X
2 1
Before Insertion

new

header new N1 N2 N3

X X

After Insertion
.
ii) Insertion of a node into SLL at ending
 To insert a node into SLL at beginning first we need to traverse to last node, then insert as new
node as last node.
Algorithm SLL_Insert_Ending(header,x)
Input: header is header node, x is data part of new node to be insert.
Output: SLL with new node at ending.
1. New=getnewnode()
2. If(new = NULL)
A) print “Required node was not available in memory bank, so unable to process”
3. Else
A) ptr=header
B) while(ptr.link!=NULL)
I) ptr=ptr.link go to step(b)
C) end loop
D) ptr.link=new /* 1 */
E) new.link=NULL /* 2 */
F) new.data=x
4. End if
End_SLL_Insert_Ending

22 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

header N1 N2 N3 new

X X
1
2
Before Insertion ptr NULL

header N1 N2 N3 new

X X

After Insertion ptr

1. Previous last node link part is replaced with address of new node.
2. Link part of new node is replaced with NULL, because new node becomes the last node.
iii) Insertion of a node into SLL at any position.

• For insertion of a node at any position in SLL, a key value is specified. Where key being the
data part of a node, after this node new node has to be inserting.
Algorithm SLL_Insert_ANY(header,x,key)
Input: header is header node, x is data pat of new node to be inserting, key is the data part of a
node, after this node we want to insert new node.
Output: SLL with new node at ANY
1. New=Getnewnode( )
2 .if(new = = NULL)
A. Print “required node was not available in memory bank, so unable to process”
3. Else
I. Ptr=header
Ii. While(ptr.data!=key and ptr.link!=NULL)
A) ptr=ptr.link
Iii. End loop
Iv. If(ptr.link=NULL and ptr.data!=key)
A) print “required node with data part as key value is not available, so unable to process”
V. Else
A) new.link=ptr.link /* 1 */
B) ptr.link=new /* 2 */
C) new.data=x
Vi. End if

23 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

4. End if.
End SLL_insert_ANY
header N1 N2 N3

X 100 20 30X
2
1
ptr

new
Before Insertion

header N1 N2 new N3

X 100 20 30X

After Insertion

1. Link part of new node is replaced by the address of next node. I.e. In the above example N3
becomes next node for newly inserting node.
2. Link part of previous node is replaced by the address of new node. I.e. In the above example N2
becomes previous node for newly inserting node.
3. Deletion of a node from SLL:
The deletion of a node in from SLL can be done in various positions.
I) Deletion of a node from SLL at beginning.
Ii) Deletion of a node from SLL at ending.
Iii) Deletion of a node from SLL at any position.
I) Deletion of a node from SLL at beginning
Algorithm SLL_Delete_Begin(header)
Input: Header is a header node.
Output: SLL with node deleted at Beginning.
1. If(header.link = = NULL)
A) print “SLL is empty, so unable to delete node from list”
2. Else /*SLL is not empty*/
I. Ptr=header.link /* ptr points to first node into list*/
Ii. Header.link=ptr.link /* 1 */
Iii. Return(ptr) /*send back deleted node to memory bank*/
3. End if
End SLL_Delete_Begin

24 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

header N1 N2 N3 N4

X X

Before Deletion
ptr

header N2 N3 N4

X X

After Deletion

1. Link part of the header node is replaced with address of second node. I.e. Address of second node
is available in link part of first node.
Ii) Deletion of a node from SLL at ending
 To delete a node from SLL at ending, first we need to traverse to last node in the list.
 After reach the last node in the list, last but one node link part is replaced with NULL.
Algorithm SLL_ Delete_End (header)
Input: header is a header node
Output: SLL with node deleted at ending.
1. If(header.link = = NULL)
A)print “SLL is empty, so unable to delete the node from list”
2. Else /*SLL is not empty*/
A) ptr=header /*ptr initially points to header node*/
B) while(ptr.link!=NULL)
I) ptr1=ptr
Ii) ptr=ptr.link /*go to step b*/
C) end loop
D) ptr1.link=NULL /* 1 */
E) return(ptr)
3. End if
End SLL_ Delete_ End

25 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

header N1 N2 N3 N4

X X
1

Before Deletion ptr1 NULL ptr

header N1 N2 N3

X X

After Deletion ptr1

1. Link part of last but one node is replaced with NULL. Because after deletion of last node in the
list, last but one node become the last node.
Iii) Deletion of a node from SLL at any position
 For deletion of a node from SLL at any position, a key value is specified.
 Where key being the data part of a node to be deleting.
Algorithm SLL_ Delete_ ANY (header,key)
Input: header is a header node, key is the data part of the node to be delete.
Output: SLL with node deleted at Any position. I.e. Required element.
1. If(header.link = = NULL)
A)print “SLL is empty, so unable to delete the node from list”
2. Else /*SLL is not empty*/
A) ptr=header /*ptr initially points to header node*/
B) while(ptr.link!=NULL and ptr.data!=key)
I) ptr1 = ptr
Ii) ptr=ptr.link go to step b
C) end loop
D) if(ptr.link = = NULL and ptr.data!=key)
I) print “Required node with data part as key value is not available”
E) else /* node with data part as key value available */
I) ptr1.link = ptr.link /* 1 */
Ii) return(ptr)
F) end if
3. End if
End SLL_ Delete_ ANY

26 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

1. Previous node link part is replaced with address of next node in the list. I.e. In the above example
N2 becomes the previous node and N4 becomes the next node for the node to be delete.

header N1 N2 N3 N4

X 100 20 30 18 X

ptr1 ptr
Before Deletion

header N1 N2 N4

X 100 20 18X

ptr1
After Deletion

4. Search for a node in SLL:


 For searching a node in SLL, a key value is specified by the user.
 If any node’s data part is equal to key value, then search is successful.
 Otherwise the required node is not available or unsuccessful search.
Algorithm SLL_Search(header,key)
Input: header is a header node
Output: Location is pointer to a node with data part as key value.
1. Ptr= header
2. Flag = 0
3. Location = NULL
4. While(ptr.link != NULL)
A) ptr = ptr.link
B)if(ptr.data==key)
I) flag=1
Ii) Location=ptr
Iii) break
C)end if
5. End loop
6. If (ptr.data = = key)

27 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

A) flag = 1
B) Location = ptr
C) return (Location)
7. Else
A) print “required node was not available”
8. End if
End SLL_Search

5. Reversing a SLL:
 Here all the nodes present in SLL, except the header node are reversed.
 Reversing of node is done by exchanging the address of nodes.
Algorithm SLL_Reverse(header)
Input: header is a header node.
Output: Reversed SLL.

1. Ptr= header.link
2. If(header.link = = NULL)
A) print “SLL is empty”
3. Else
A) ptr1 = NULL
B) while(ptr != NULL)
I) temp = ptr1
Ii) ptr1=ptr
Iii) ptr = ptr.link
Iv) ptr1.link = temp
C) end loop
D) header.link = ptr1
4. End if
End SLL_Reverse

2. Double Linked List:


 In a SLL one can move from the header node to any node in one direction only. I.e. From left to
right.
 A DLL is a two way list. Because one can move either from left to right or right to left.
 In DLL, each node maintains two links.

28 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

LLink RLink
Data

Structure of a node in DLL

 Herellink refers Left Link and rlink refers Right Link.


 The llinkpart of a node in DLL always points to the previous node. I.e. Llinkpart of a node
Consists address of previous node.
 The rlinkpart of a node in DLL always points to the next node. I.e. Rlinkpart of a node Consists
address of next node.
 Operations on Double Linked List:

1. Insertion of a node in to DLL

2. Deletion of a node from DLL

1. Insertion of a node in to DLL:

The Insertion of a node in to DLL can be done in various positions.


I) Insertion of a node into DLL at beginning.
Ii) Insertion of a node into DLL at ending.
Iii) Insertion of a node into DLL at any position.
 For insertion of a node into DLL, we must get node from memory bank. The procedure for getting
node from memory bank is same as getting node for SLL from memory bank.
I) Insertion of a node into DLL at beginning
Algorithm DLL_insertion_Begin(header,X)
Input: header is a header node.
Output: DLL with new node at begin.
1. New=getnewnode()
2. If(new = = NULL)
A) print “required node is not available in memory”
3. Else
A) ptr=header.rlink
B) new.rlink=ptr /* 1 */
C) new.llink=header /* 2 */
D) header.rlink=new /* 3 */
E) ptr.llink=new /* 4 */
4. End if

29 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

End DLL_insertion_Begin

ptr

header N1 N2 N3
X X
4
3 2 1

new Before Insertion

header new N1 N2 N3
X X

ptr
After Insertion

1. Rlink part of new node is replaced with the address of first node in the DLL. I.e. Address of first
node is available in Rlink part of header node.
2. Llink part of new node is replaced with the address of header.
3. Rlink part of header node is replaced with the address of new node.
4. Llink part of previous first node is replaced with the address of new node.
Ii) Insertion of a node into DLL at ending.
Algorithm DLL_Insert_Ending(Header,x)
Input: Header is the header node, x is the data part of new node to be inserted.
Output: DLL with new node inserted at the ending.
1. New=getnewnode()
2. If(new = = NULL)
A)print “Required node was not available”
3. Else
A) ptr=header
B) while(ptr.rlink != NULL)
I) ptr=ptr.rlink gotostep(b)
C) end while loop
D) ptr.rlink=new /* 1 */
E) new.llink=ptr /* 2 */
F) new.rlink=NULL /* 3 */
G) new.data=x

30 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

4. End if
End DLL_Insertion_Ending

header N1 N2 N3 new
1
X X
2 3
ptr NULL
Before Insertion

header N1 N2 N3 new
X X

After Insertion ptr

1. Rlinkpart of last node in the DLL is replaced with address of new node.
2. Llinkpart of new node is replaced with address of previous last node.
3. Rlinkpart of new node is replaced with NULL. Because newly inserted node becomes the last node
in the list.
iii) Insertion of a node into DLL at any position
 For insertion of a node at any position in DLL, a key value is specified.
 Where key being the data part of a node, after this node new node has to be inserting.
Algorithm DLL_Insertion_ANY(header,x,key)
Input: Header is a header node, key is the data part of a node, after that node new node is inserted,
x is data part of new node to be insert.
Output: DLL with new node inserted after the node with data part as key value
1. New=getnewnode()
2. If(new = = NULL)
A) print“required node is not available in memory”
3. Else
A) ptr=header
B) while(ptr.data!=key and ptr.rlink!=NULL)
I) ptr=ptr.rlink go to step(b)
C) end loop
D) if(ptr.rlink = = NULL and ptr.data != key)
I) print “required node with key value was not available”
E) else
I) ptr1=ptr.rlink
Ii) new.rlink=ptr1 /* 1 */

31 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

Iii) new.llink=ptr /* 2 */
Iv) ptr.rlink=new /* 3 */
V) ptr1.llink=new /* 4 */
Vi)new.data=x
F) end if
4. End if
Enddll_Insertion_ANY

ptr ptr1
header N2 N3
N1
X 10 20 30X

3 4
2
1

new
Before Insertion

header N1 N2 new N3

X 10 20 30X

ptr ptr1
After Insertion

1. Rlinkpart of new node is replaced with the address of next node. I.e. In the above example N3
becomes the next node for newly inserting node.
2. Llinkpart of new node is replaced with the address of previous node. I.e. In the above example N2
becomes the previous node for newly inserting node.
3. Rlinkpart of previous node is replaced with address of new node.
4. Llinkpart of next node is replaced with address of new node

2. Deletion of a node from DLL:

The deletion of a node in from DLL can be done in various positions.


I) Deletion of a node from DLL at beginning.
Ii) Deletion of a node from DLL at ending.
Iii) Deletion of a node from DLL at any position.
I) Deletion of a node from DLL at beginning
Algorithm DLL_Deletion_Begin(header)
Input: header is a header node
32 Department of Information Technology | GEC, Gudlavalleru
Unit-1 [DATA STRUCTURES] I YEAR II SEM

Output: DLL with node deleted at begin


1. If(header.rlink = = NULL)
a) Print “DLL is empty, not possible to perform deletion operation”
2. Else
a) Ptr=header.rlink
b) Ptr1=ptr.rlink
c) Header.rlink=ptr1 /* 1 */
d) Ptr1.llink=header /* 2 */
e) Return(ptr)
3. End if
End DLL_Deletion_Begin

header N1 N2 N3 N4
X X

ptr ptr1
2
Before Deletion

header N2 N3 N4
X X

ptr
After Deletion

1. Rlinkpart of header node is replaced with the address of second node. I.e. Address of second node
is available rlinkpart of first node.
2. Llinkpart of second node is replaced with the address of header node.

ii) Deletion of a node from DLL at ending.


Algorithm DLL_Deletion_End(header)
Input: header is a header node.
Output: DLL with deleted node at ending.
1. If(header.rlink=NULL)
a) Print “DLL is empty, not possible to perform deletion operation”
2. Else

33 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

A) ptr=header
B) while(ptr.rlink != NULL)
I) ptr1=ptr
Ii) ptr=ptr.rlink
C) end loop
D) ptr1.rlink=NULL /* 1 */
E) return(ptr)
3. End if
End DLL_Deletioon_Ending

header N1 N2 N3 N4
X X

1
ptr1 NULL ptr
Before Deletion

header N1 N2 N3
X X

ptr1
AfterDeletion

1. Rlinkpart of last but one node in DLL is replaced with NULL. Because last but one node becomes
last node.
iii) Deletion of a node from DLL at any position.
 For deletion of a node from DLL at any position, a key value is specified. Where key being the
data part of a node to be deleting.
Algorithm DLL_Deletion_Any(header,key)
Input: header is header node, key is the data part of a node to be delete.
Output: DLL without node as data part is key value.
1. If(header.rlink = = NULL)
A) print “DLL is empty, not possible for deletion operation”
2. Else
I) ptr=header
Ii) while(ptr.data!=key and ptr.rlink!=NULL)
A) ptr=ptr.rlink
Iii) end loop
Iv) if(ptr.rlink=NULL and ptr.data != key)
34 Department of Information Technology | GEC, Gudlavalleru
Unit-1 [DATA STRUCTURES] I YEAR II SEM

A) print “required node was not available in list”


V) else
A) ptr1 = ptr.llink
B) ptr2 = ptr.rlink
C) ptr1.rlink = ptr2 /* 1 */
D) ptr2.rlink = ptr1 /* 2 */
Vi) end if
3. End if
End DLL_Deletion_Any

1
header N1 N2 N3 N4
X 10 20 30X 40X

ptr
ptr1 2 ptr2

Before Deletion

header N1 N2 N4
X 10 20 40X

ptr1 ptr2
After Deletion

1. Rlinkpart of previous node is replaced with the address of next node. I.e. In the above example N2
become the previous node to node to be delete, N4 becomes the next node to node to be delete.
2. Llinkpart of next node is replaced with the address of previous node. I.e. In the above example N2
become the previous node to node to be delete, N4 becomes the next node to node to be delete.

35 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

 Circular Linked List :


• Circular linked list is a special type of linked list.

• In a Circular Linked list, the field of the last node points to the first node of the list.

• It is mainly used in lists that allow to access to nodes in the middle of the list without starting
at the
beginning.

header N1 N2 N3 N4

Circular Linked List

• If a CLL is empty, then the link part of the header node points to itself.
header

Empty Circular Linked List


 Operations on Circular Linked List:
1. Insertion of a node in to CLL
2. Deletion of a node from CLL
1. Insertion of a node in to CLL
The Insertion of a node in to CLL can be done in various positions.
I) Insertion of a node into CLL at beginning.
Ii) Insertion of a node into CLL at ending.
Iii) Insertion of a node into CLL at any position.

I) Insertion of a node into CLL at beginning.


Algorithmcll_Insert_Begin(header,x)
Input: header is a header node, x is data part of new node to be insert.
Output: CLL with new node inserted at beginning.
1. New=getnewnode( )

36 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

2. If(new = = NULL)
A) print “required node was not available in memory, so unable to process”
3. Else
A) new.link = header.link /* 1 */
B) header.link = new /* 2 */
C )new.data = x
4. End if
End CLL_Inset_Begin

N1 N2 N3
header
X
2 1

new Before Insertion

header new N1 N2 N3

X
After Insertion

1. Link part of new node is replaced with address of first node in list, i.e. Link part of header node.
2. Link part of header node is replaced with new node address.
ii) Insertion of a node into CLL at ending.
AlgorithmCLL_Insert_END(header,x)
Input: header is a header node, x is data part of new node to be insert.
Output: CLL with new node inserted at ending.
1. New=getnewnode()
2 .if(new = = NULL)
A) print “required node was not available at memory bank, so unable to process”
3. Else
A) ptr=header
B) while(ptr.link != header)
I) ptr=ptr.link //goto step b
C) end loop
D) ptr.link=new
E) new.link=header

37 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

F) new.data=x
4. End if
End CLL_Insertion_END

N1 N2 N3 new
header
X
1
2
header node address
Before Insertion

header N1 N2 N3 new

After Insertion

1. Previous last node link part is replaced with address of new node.
2. Link part of new node is replaced with address of header node, because new node becomes the last
node.
Iii) Insertion of a node into CLL at any position.
Algorithm CLL_Insert_ANY(header,x,key)
Input: header is header node, x is data pat of new node to be insert, key is the data part of a node,
after this node we want to insert new node.
Output: CLL with new node at ANY
1. New=Getnewnode()
2 .if(new=NULL)
A. Print “required node was not available in memory bank, so unable to process”
3. Else
I. Ptr=header
Ii. While(ptr.data! =key and ptr.link!=header)
A) ptr=ptr.link
Iii. End loop
Iv. If(ptr.link=header and ptr.data!=key)
A) print “required node with data part as key value is not available, so unable to
process”
V. Else
A) new.link=ptr.link
B) ptr.link=new

38 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

C) new.data=x
Vi. End if
4. End if.
End CLL_insert_ANY

header
N1 N2 N3

X 100 20 30

2
1
ptr

Before Insertion new

N1 N2 new N3
header

X 100 20 30

After Insertion

1. Link part of new node is replaced by the address of next node. I.e. In the above example N3
becomes next node for newly inserting node.
2. Link part of previous node is replaced by the address of new node. I.e. In the above example N2
becomes previous node for newly inserting node.
2. Deletion of a node from CLL:
The Deletion of a node from CLL can be done in various positions.
I) Deletion of a node from CLL at beginning.
Ii) Deletion of a node from CLL at ending.
Iii) Deletion of a node from CLL at any position
I) Deletion of a node from CLL at beginning
Algorithm CLL_Delete_Begin(header)
Input: Header is a header node.
Output: CLL with node deleted at Beginning.
1. If(header.link = = header)
A) print “CLL is empty, so unable to delete node from list”
2. Else /*DLL is not empty*/
I. Ptr=header.link /* ptr points to first node into list*/
Ii. Header.link=ptr.link /* 1 */

39 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

Iii. Return(ptr) /*send back deleted node to memory


bank*/
3. End if
End CLL_Delete_Begin

header N1 N2 N3 N4

X
Before Deletion
ptr

header N2 N3 N4

X
After Deletion

1. Link part of the header node is replaced with address of second node. I.e. Address of second
node is available in link part of first node.
Ii) Deletion of a node from CLL at ending
Algorithm CLL_ Delete_ End (header)
Input: header is a header node
Output: CLL with node deleted at ending.
1. If(header.link = = header)
A)print “CLL is empty, so unable to delete the node from list”
2. Else /*CLL is not empty*/
A) ptr=header /*ptr initially points to header node*/
B) while(ptr.link!=header)
I) ptr1=ptr
Ii) ptr=ptr.link /*go to step b*/
C) end loop
D) ptr1.link=header /* 1 */
E) return(ptr)
3. End if
End CLL_ Delete_ End

40 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

1. Link part of last but one node is replaced with address of header node. Because after deletion of
last node in the list, last but one node become the last node.

header N1 N2 N3 N4

X
1

Before Deletion header


ptr1 node ptr
address

header N1 N2 N3

After Deletion ptr1

Iii) Deletion of a node from CLL at any position

• For deletion of a node from CLL at any position, a key value is specified. Where key being the
data part of a node to be deleting.
Algorithm CLL_ Delete_ ANY (header,key)
Input: header is a header node, key is the data part of the node to be delete.
Output: CLL with node deleted at Any position. I.e. Required element.
1. If(header.link = = header)
A)print “SLL is empty, so unable to delete the node from list”
2. Else /*CLL is not empty*/
A) ptr=header /*ptr initially points to header node*/
B) while(ptr.link!=header and ptr.data!=key)
I) ptr1 = ptr
Ii) ptr=ptr.link go to step b
C) end loop
D) if(ptr.link = = header and ptr.data!=key)
I) print “Required node with data part as key value is not available”
E) else /*node with data part as key value available
I) ptr1.link = ptr.link /* 1 */
Ii) return(ptr)
F) end if

41 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

3. End if
End CLL_ Delete_ ANY

1
header N1 N2 N3 N4

X 100 20 30 18

ptr1 ptr
Before Deletion

header N1 N2 N4

X 100 20 18

ptr1

After Deletion

1. Previous node link part is replaced with address of next node in the list. I.e. In the above
example N2 becomes the previous node and N4 becomes the next node for the node to be
delete.

42 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

Assignment/Tutorial Questions

A. Questions testing the remembering / understanding level of students

I) Objective Questions

1.The logical or mathematical model of a particular organization of data is called a ______________.

2. An ordered collection of finite, homogeneous data elements where the linear order is maintained by means
of links or pointers is called as ___________________.

3. In single linked list each node contain minimum of two fields. One field is data field to store the data
second field is used to store _____________? [ ]

A) Pointer to character b) Pointer to integer c) Pointer to next node d) None

4. Linked list is generally considered as an example of _________ type of memory allocation.

A)Dynamic b)Compile Time c)Static d)None of these

5.A variant of linked list in which last node of the list points to the first node of the list is? [ ]

A)Singly linked list b) Doubly linked list c)Circular linked list d) Multiply linked list

6.In doubly linked lists, traversal can be performed? [ ]

A)Only in forward direction b) Only in reverse direction c)In both directions d) None

7.A variant of the linked list in which none of the node contains NULL pointer is? [ ]

A)Singly linked list b) Doubly linked list c)Circular linked list d) None

8. Identify non-linear Data Structure from the following [ ]

A. Array b. Stack c. Graph d. Linked list

9.A node in single linked list can reference the previous node. [True/False]

10. Which type of structure is used to create a linked list? [ ]

A) Nested structure b) Self referential structure c) Array of structure d)pointers to structure

11. Which type of linked list occupies more memory? [ ]

A)SLL b) DLL c)CLL d)None

II) Descriptive Questions

1. Explain about delete operation in singly linked list.


2. Compare single linked list and circular single linked list.
3. Write an algorithm to perform deletion operation on circular linked list.
4. Write an algorithm to perform insertion operation on a double linked list.
5. Write an algorithm to perform deletion operation on a double linked list.
6. Write short notes on data structures.

43 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

B. Question testing the ability of students in applying the concepts.

I) Multiple Choice Questions:

1. Inserting a node at the beginning of the single linked list needs to modify ____pointers [ ]

A) 1 b) 2 c) 3 d) 0

2. Inserting a node at the end of the single linked list needs to modify____pointers. [ ]

3. A double linked list is declared as follows: [ ]

struct dllist
{
struct dllist *fwd, *bwd;
int data;
}
Where fwd and bwd represents forward and backward links to adjacent elements of the list. Which
among the following segments of code deletes the element pointed to by X from the double linked
list, if it is assumed that X points to neither the first nor last element of the list?

A. X -> bwd -> fwd = X -> fwd;


X -> fwd -> bwd = X -> bwd
B. X -> bwd -> fwd = X -> bwd;
X -> fwd -> bwd = X -> fwd
C. X -> bwd -> bwd = X -> fwd;
X -> fwd -> fwd = X -> bwd
D. X -> bwd -> bwd = X -> bwd;
X -> fwd -> fwd = X -> fwd

4. Inserting a node at any position(middle) of the single linked list needs to modify _________pointers.[ ]

A) 1 b) 2 c) 3 d) 0

5. Deleting a node from the beginning of the double linked list needs to modify _________pointers. [ ]

A) 1 b) 2 c) 3 d) 0

6. Which among the following segment of code deletes the element pointed to by X from the double
linked list, if it is assumed that X points to the first element of the list and start pointer points to
beginning of the list? [ ]
A. X -> bwd = X -> fwd;
X -> fwd = X -> bwd
B. start = X -> fwd;
start -> bwd = NULL;
C. start = X -> fwd;
X -> fwd = NULL
D. X -> bwd -> bwd = X -> bwd;
X -> fwd -> fwd = X -> fwd

44 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

7. Which among the following segment of code counts the number of elements in the double linked list, if
it is assumed that X points to the first element of the list and ctr is the variable which counts the number

of elements in the list? [ ]

A. for (ctr=1; X != NULL; ctr++)

X = X -> fwd;

B. for (ctr=1; X != NULL; ctr++)

X = X -> bwd;

C. for (ctr=1; X -> fwd != NULL; ctr++)

X = X -> fwd;

D. for (ctr=1; X -> bwd != NULL; ctr++)

X = X -> bwd;

8. Deleting a node from the end of the double linked list needs to modify _________pointers. [ ]

A) 1 b) 2 c) 3 d) 0

9. Which among the following segment of code inserts a new node pointed by X to be inserted at the
beginning of the double linked list. The start pointer points to beginning of the list?

A. X -> bwd = X -> fwd;

X -> fwd = X -> bwd;

B. X -> fwd = start;

start -> bwd = X;

start = X;

C. X -> bwd = X -> fwd;

X -> fwd = X -> bwd;

start = X;

D. X -> bwd -> bwd = X -> bwd;

X -> fwd -> fwd = X -> fwd

10. Deleting a node from any position (middle) of the double linked list needs to
modify_________pointers. [ ]

A) 1 b) 2 c) 3 d) 0

11. Inserting a node at the beginning of the circular double linked list needs to modify_________pointers.
A)1 b) 2 c) 3 d) 0 [ ]

45 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

12. Which of the following statement is true [ ]

I. Using single linked list it is not possible to traverse the list in backward direction.

II. To find the predecessor it is required to traverse the list from the first node in case of single linked
list.

A. I only b. II only c. Both I and II d. None of the above

13. The following C function takes a singly linked list as input argument. It modifies the list by moving the
last element to the front of the list and returns the modified list. Some part of the code is left blank.
[ ]

typedef struct node


{
Intvalue;
struct node *next;
}Node;

node *move_to_front(node *head)


{
Node *p, *q;
if ((head == NULL: || (head->next == NULL))
Return head;
Q = NULL; p = head;
While (p->next !=NULL)
{
Q = p;
P = p->next;
}
----------------------------------------
Return head;
}
Analyze the code above and choose the correct alternative to replace the blank line.
(A) q = NULL; p->next = head; head = p;
(B) q->next = NULL; head = p; p->next = head;
(C) head = p; p->next = q; q->next = NULL;
(D) q->next = NULL; p->next = head; head = p;

46 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

II) Problems:

1. Consider the following single linked list.

header N1 N2 N3 N4

X 1 2 3 4 X

Do the following operations on this list and draw the updated single linked listafter each operation.

1. Insert 5 at end 2. Insert 6 at begin 3.Insert 9 after 2

4. Delete 6 5. Delete 5 6. Delete 3

2. Consider the following double linked list.

Do the following operations on this list and draw the updated single linked list after each operation.

1. Insert 50 at end 2. Insert 60 at begin 3.Insert 90 after 20

4. Delete 60 5. Delete 50 6. Delete 30

3. Consider the following single linked list.

header

X 8 20 40 X

Insert the following elements into the list 2,15,30,50. Such that the list will be in ascending order and
draw the updated single linked list after each insertion operation.

4. Consider the following double linked list.

Insert the following elements into the list 2,15,30,50. Such that the list will be in ascending order and
draw the updated single linked list after each insertion operation.

5.Write a program to implement insert operation in a doubly linked List.

6.Develop a program to reverse elements of a single linked list.

47 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

C. Questions testing the analyzing / evaluating ability of students:


1.The following C function takes a single-linked list of integers as a parameter and rearranges the
elements of the list. The function is called with the list containing the integers 1, 2, 3, 4, 5, 6, 7 in the
given order. What will be the contents of the list after the function completes execution?

Struct node

Int value;

Struct node *next;

};

Void rearrange(struct node *list)

Struct node *p, * q;

Int temp;

If((!List) || !List->next)

Return;

P = list;

Q = list->next;

While(q)

Temp = p->value;

P->value = q->value;

Q->value = temp;

P = q->next;

Q = p? P->next:0;

48 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

2.The following C function(myfunction) takes a single-linked list of integers as a parameter and


rearranges the elements of the list. The function is called with the list containing the integers 1, 2, 3,
4,2, 5, 6,5, 7 in the given order. Analyze the following program and print the contents of the list after
the function completes its execution?

Int myfunction(struct listnode*head)


{
struct listnode*current;
Struct listnode*previous;
Struct listnode*itr;
Struct listnode*tmp;
if(head == NULL)
Return 0;
if(head->next == NULL)
Return 1;
Current = head->next;
Previous = head;
While(current != NULL)
{
itr= head;
While(itr != current)
{

If(itr->data == current->data)
{
Tmp= current;
Current = current->next;
Previous->next = current;
Delete tmp;
Break;
}
Itr= itr->next;
}
If(itr == current)
{
Current = current->next;
Previous = previous->next;
}
}
}

49 Department of Information Technology | GEC, Gudlavalleru


Unit-1 [DATA STRUCTURES] I YEAR II SEM

3. Write a “C” function to split a given list of integers represented by a single linked list into two lists in the
following way. Let the list be L = (l0, l1, ….., ln). The resultant lists would be R1 = (l0, l2, l4, …..) and R2
= (l1, l3, l5, …..).

. Implement the following function as a new function for the linked list

Precondition: head_ptr points to the start of a linked list.

The list might be empty or it might be non-empty.

Postcondition: The return value is the sum of all the data components of all the nodes.

NOTE: If the list is empty, the function returns 0.

D. Questions asked in Previous Competitive exams (GATE/NET/SET)


1. Consider the function f defined below. (GATE 2003)
struct item
{
int data;
struct item * next;
};
int f(struct item *p)
{
return( (p == NULL) || (p->next == NULL) || (( P->data <= p->next->data) && f(p->next)) );
}
For a given linked list p, the function f returns 1 if and only if
a) the list is empty or has exactly one element
b) the elements in the list are sorted in non-decreasing order of data value
c) the elements in the list are sorted in non-increasing order of data value
d) not all elements in the list have the same data value.

2. A circularly linked list is used to represent a Queue. A single variable p is used to access the Queue. To
which node should p point such that both the operations enQueue and deQueue can be performed in
constant time? (GATE 2004)
a) rear node
b) front node
c) not possible with a single pointer
d) node next to front

3. In the worst case, the number of comparisons needed to search a singly linked list of length n for a
given element is (GATE 2002)
a) log 2 n
b) n/2
c) log 2 n – 1
d) n

50 Department of Information Technology | GEC, Gudlavalleru

Вам также может понравиться