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

Unit 4

Uploaded by

vsjadhav1085
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Unit 4

Uploaded by

vsjadhav1085
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

1

MRS. RAJANEE KAVATHEKAR


LECTURER (COMPUTER DEPT.)
LESP, KUPWAD, SANGLI
Data Structure using 'C'
COURSE TITLE 2
(DSU)
COURSE CODE 22317
ACADEMIC
YEAR 2020-21

150
Theory – 70 ESE + 30 PA
TOTAL MARKS
Practical – 25# ESE + 25
PA
# - External Assessment
3
Course Outcomes:
Syllabus 4
Unit - 01
Introduction to Data Structure
Concept & Need of DS, Abstract Data
1
Type
Types of Data Structure: Linear, Non
2
Linear DS
3 Algorithm Complexity: Time, Space
Operations on DS: Traversing,
4
searching, insertion, deletion, sorting
Syllabus 5
Unit - 02

Searching & Sorting


Searching : searching an item in data
1 set using following methods: linear
Search, binary Search
Sorting: sorting of data set in an order
using following method: bubble sort,
2
selection sort, insertion sort, quick sort,
radix sort
Syllabus 6
Unit - 03
Stacks & Queues
Introduction to Stack: stack representation in memory using
array; stack as an ADT; stack operations- PUSH & POP; stack
1 operations conditions - stack full, stack overflow, stack empty,
stack underflow; Applications of stack- reversing a string,
polish notations
Conversion of infix to postfix expression, evaluation of postfix
2 expression, converting an infix into prefix expression,
evaluation of prefix expression, recursion, tower of Hanoi
Introduction to Queue: Queue representation in memory using
array; queue as an ADT; types of queue- linear, circular queue,
3 priority of queue; queue operations - INSERT & DELETE;
queue operation conditions- queue full, queue empty;
applications of queue
Syllabus
Unit - 04
Linked List

Introduction to Linked List; terminologies- node, address, pointer,


1 information field/data field, next pointer, null pointer, empty list

2 Types of List: linear list, circular list

Operations on singly linked list: traversing a singly linked list, searching a


3 key in linked list, inserting a new node in a list, deleting a node from a
linked list
Introduction to Linked List 8
Introduction to Linked List 9

struct node
{
int data;
 A linked list is a series of connected nodes struct node *next;
}
 Each node contains at least
A piece of data (any type) node

 Pointer to the next node in the list A

 The last node points to NULL dat pointer


a
Terminologies 10
 Node:- Each data element in a linked list is represented as a node.
Node contains two parts-one is info (data) and other is next pointer
(address). Info part stores data and next pointer stores address of
next node. Node

 Next Pointer:-It is a pointer that holds address of next node in the


list i.e. next pointer points to next node in the list
11
 Null Pointer:- It is a pointer that does not hold any memory
address i.e. it is pointing to nothing. It is used to specify end
of the list. The last element of list contains NULL pointer
to specify end of list.

 Empty list :- Each linked list has a header node. When


header node contains NULL value, then that list is said to
be empty list.
Types of List 12

1.Single linked list


2.Double linked list
3.Circular linked list
Single linked list 13

 A single linked list is one in which all nodes are linked


together in some sequential manner
Double linked list 14

 We add a pointer to the previous node in a doubly-linked


list. Thus, we can go in either direction: forward or
backward.
Circular linked list 15

 A circular linked list is a variation of a linked list in which the last


element is linked to the first element. This forms a circular loop.

 A circular linked list can be either singly linked or doubly


linked.
 for singly linked list, next pointer of last item points to the
first item
 In the doubly linked list, prev pointer of the first item points
to the la
Compare Array Vs linked list
16
Array Linked List

Array is a collection of elements of similar data Linked List is an ordered collection of elements of
type. same type, which are connected to each other
using pointers.
Size of the array must be specified at time of Size of a Linked list is variable. It grows at runtime,
array declaration as more nodes are added to it.
Array supportsRandom Access, which means Linked List supports Sequential Access, which
elements can be accessed directly using their means to access any element/node in a linked
index, likearr[0]for 1st element,arr[6]for 7th list;we have to sequentially traverse the
element etc. complete linked list, up to that element.

Hence, accessing elements in an array isfastwith To accessnthelement of a linked list, time


a constant time complexity ofO (1) complexity isO (n).
Memory is allocated as soon as the array is Memory is allocated atruntime, as and when a
declared, atcompile time. It's also known new node is added. It's also known asDynamic
asStatic Memory Allocation. Memory Allocation.

Array cansingle dimensional,two Linked list can beLinear


dimensionalormultidimensional (Singly),DoublyorCircularlinked list.
Operations on singly linked list 17

 Create a singly linked list.


 Traversing a singly linked list
 Searching a key in linked list
 Inserting a new node in a list
 Deleting a node from a linked list
Create a singly linked list. 18

 To create a linked list we will create a node one by one and


add them to the end of the list
 these nodes can be created using dynamic memory allocation.
 the C function for creating a linked list is as follows:
struct node
{
int data;
struct node *next;
}
 When the above statement is executed a block of memory to
store a node is allocated.
Traversing a singly linked list 19

 In traversing, we simply visit each node of the list at least once in


order to perform some specific operation on it, for example,
printing data part of each node present in the list.
 When temp is NULL, we know that we have reached the end of
the linked list so we get out of the while loop.
struct node *temp = head;
printf("\n\n List elements are - \n");
while(temp != NULL)
{
printf("%d --->",temp->data);
temp = temp->next;
}
Searching a key in linked list 20

 In searching, we match each element of the list with the given


element. If the element is found on any of the location then
location of that element is returned otherwise null is returned.
Step 1: SET PTR = HEAD.
Step 2: Set I = 0.
Step 3: IF PTR = NULL.
Step 4: REPEAT STEP 5 TO 7 UNTIL PTR != NULL.
Step 5: if ptr → data = item.
Step 6: I = I + 1.
Step 7: PTR = PTR → NEXT.
Step 8: EXIT.
Example 21

Create a singly linked list using data fields 90, 25, 46, 39,56.
Search a node 40 from the SLL and show procedure step-by-step
with the help of diagram from start to end

SEARCHING A NODE
STEP 1: Compare 40 with 90
40!=90
Inserting a new node in a list 22

 Insertion is a process of of adding a new element in the


linked list the insert operation adds a new element to the
linked list depending on the location where the node is to be
added
 There are three different possibilities for
inserting a node into a linked list.
 These three possibilities are:
1) Insertion at the beginning of the list.
2) Insertion at specific location of the list.
3) Insertion at the end of the list
Insertion at the beginning of the list. 23

 In the first case, we make a new node and points its next to
the head of the existing list and then change the head to the
newly added node. It is similar to picture given below.
 So, the steps to be followed are as follows:
1) Make a new node
2) Point the ‘next’ of the new node to the ‘head’
of the linked list.
3) Mark new node as ‘head’.
Insertion at the end of the list. 24

 The second case is the simplest one. We just add a new node
at the end of the existing list.
 It is shown in the picture given below:
 So, the steps to add the end if a linked list are:
1) Make a new node
2) Point the last node of the linked list to the new
node
Insertion at the specific location of the list.
25
 To insert a node in between a linked list, we need to
first break the existing link and then create two new links.
It will be clear from the picture given below.
 The steps for inserting a node after node ‘a’ are:
1) Make a new node
2) Point the ‘next’ of the new node to the node
‘b’ (the node after which we have to insert the
new node). Till now, two nodes are pointing
the same node ‘b’, the node ‘a’ and the new node.
3) Point the ‘next’ of ‘a’ to the new node.
Deleting a node from a linked list 26

 In a linear linked list, a node can be deleted from the


beginning of list, from in between positions and from end of the
list.
1) Delete a node from the beginning
2) Delete a node from in between position
3) Delete a node from the end
Delete a node from the beginning 27
 Node to be deleted is node1.
 Create a temporary node as ‘temp’. Set ‘temp’ node with the
address of first node. Store address of node 2 in header pointer
‘start’ and then delete ‘temp’ pointer with free function.
 Deleting temp pointer deletes the first node from the list.
Step 1: Create temporary node ‘temp’.
Step 2: Assign address of first node to ‘temp’ pointer.
Step 3: Store address of second node (temp->next) in header pointer
‘start’.
Step 4: Free temp.
Delete a node from in between position 28
 Node to be deleted is node3.
 Create a temporary node as ‘temp’ and ‘q’. Set ‘temp’ node with the
address of first node.
 Traverse the list up to the previous node of node 3 and mark the
next node (node3) as ‘q’. Store address from node ‘q’ into address
field of ‘temp’ node.
 Then delete ‘q’ pointer with free function. Deleting ‘q’ pointer
deletes the node 3 from the list.
Cont… 29
Step 1: Create temporary node ‘temp’, ’q’.
Step 2: Assign address of first node to ‘temp’ pointer.
Step 3: Traverse list up to previous node of node to be deleted.
Step 4: Mark the node to be deleted ‘q’.
Step 5: Store address from node ‘q’ in address field of ‘temp’ node
(temp->next=q->next).
Step 6: Free q.
Delete a node from the end 30
 Node to be deleted is node 3.
 Create a temporary node as ‘temp’ and ‘q’. Set ‘temp’ node with the
address of first node.
 Traverse the list up to the second last node and mark the last node
as ‘q’. Store NULL value in address field of ‘temp’ node
 Then delete ‘q’ pointer with free function.
 Deleting q pointer deletes the last node from the list.
Cont… 31
Step 1: Create temporary node ‘temp’,’q’.
Step 2: Assign address of first node to ‘temp’ pointer.
Step 3: Traverse list upto second last node.
Step 4: Mark last node’s address in node ‘q’.
Step 5: store NULL value in address field of second last
node (temp->next).
Step 6: Free q
Assignment 4 32
1)Describe the concept of linked list with the terminologies : node,
next pointer, null pointer and empty list.
2)Describe circular linked list with suitable diagram. Also state
advantage of circular linked list over linear linked list.
4) create a singly linked list using data fields 15, 20, 22, 58, 60.
Search a node 22 from the SLL and show procedure step-by-step
with the help of diagram from start to end.
5) Compare Linked List and Array.
6) Construct a singly linked list using data fields 21, 25, 96, 58,
74 and show procedure step-by-step with the help of diagram start
to end.
7) show with suitable diagrams how to delete a node from singly
linked list at the beginning, in between and at the end of the list.

You might also like