0% found this document useful (0 votes)
7 views22 pages

Ds 2

Data structure question paper

Uploaded by

tchate45
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)
7 views22 pages

Ds 2

Data structure question paper

Uploaded by

tchate45
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/ 22

CSPCC2002: Data Structures

Unit 2
Linear Data Structure & their representation:
 Definition,
 concept,
 operation on linked lists,
 Circular linked lists,
 Doubly linked lists,
 Operations Like Insertion,
 deletion, insertion order, searching, updating,
 Application Of linked list such a polynomial manipulation,
 Comparison Singly Linked,
circularly linked list & doubly linked list.

•What are the drawbacks of using sequential storage to represent


stacks and queues?
•One major drawback is that a fixed amount of storage remains
allocated to the stack or queue even when the structure is actually
using a smaller amount or possibly no storage at all.
• Further, no more than that fixed amount of storage may be allocated, thus introducing the
possibility of overflow.

Linked List:
Definition:
● Linked list is used to represent linear data structure.
● Linked list is a collection of nodes, every node consists of two fields.

1. Data field - It contains information of data element.


2. Next field - It is a pointer variable which contains address of next node.

● There are four types of linked list:


1. Singly Linked List
2. Circular Linked List
3. Doubly Linked List
4. Doubly Circular Linked List.

Advantages of Linked Lists:

1. Linked list allocates the memory dynamically when required.


2. Insertion and deletion operations can be easily implemented.
3. Stacks and queues can be easily executed.
4. Linked List reduces the access time.
Disadvantages of Linked Lists

1. The memory is wasted as pointers require extra memory for storage.


2. No element can be accessed randomly, it has to access each node sequentially.
3. Reverse Traversing is difficult in linked list.

Applications of Linked Lists:

1. Linked lists are used to implement stacks, queues, graphs, etc.


2. Linked lists is used to implement the hash tables.
3. Linked list is used to represent the sparse matrix.
4. For manipulation of polynomial, we use linked list.

- Basic Terms of Linked List:


1. Node: A linked list is a linear data structure where each element is a separate object. Each
element is called as a node of a linked list. Every node consists of two fields, first is data and
second is address of next node.

2. Information Field/ Data field: It is used to store the data inside the node.
3. Address/Next Pointer: It is used to store the address of next node.
4. Null Pointer: : It is used to specify the end of the list. The last element of list contains NULL
pointer to specify end of list.
5. Empty List: A linked list is said to be empty if head node contains NULL pointer.
i.e. head=NULL.

• Suppose that the items of a stack or a queue were explicitly ordered,


that is, each item contained within itself the address of the next item.
• Such an explicit ordering gives rise to a data structure pictured in
Figure 4.2.1, which is known as a linear linked list.
• Each item' in the list is called a node and contains two fields, an
information field and a next address .field
• The information field holds the actual element on the list.
• The next address field contains the address of the next node in the
list. Such an address, which is used to access a particular node, is
known as a pointer.• The next address field of the last node in the list contains a special
value, known as null, which is not a valid address. This null pointer is
used to signal the end of a list.
• The list with no nodes on it is called the empty list or the null list. The
value of the external pointer list to such a list is the null pointer.
•A list can be initialized to the empty list by the operation list = null.

- Basic Linked List Operations:

The basic operations that can be performed on a list are:

1. Creation: This operation is used to create a node in the linked list.


2. Insertion: This operation is used to insert a new node in the linked list.
● Inserting node at the beginning of the list.
● Inserting node at the middle of the list.
● Inserting node at the end of the list.
3. Deletion: This operation is used to delete node from the linked list.
● Deleting node from the beginning of the list.
● Deleting node from the middle of the list.
● Deleting node from the end of the list.
4. Traversing: It is a process of going through all the nodes of a linked list from one end to the
other end.
5. Display: This operation is used to print all nodes information field.
6. Searching: To search a specific element in given linked list.
7. Count: To count number of nodes present in list.
8. Concatenation: Concatenating two linked lists.
9. Sorting: Sorting of an elements stored in a linked list.
10. Separating a linked list in two linked lists.
- Inserting and Removing Nodes from a List

•A list is a dynamic data structure. The number of nodes on a list may


vary dramatically as elements are inserted and removed.
• The dynamic nature of a list may be contrasted with the static nature
of an array, whose size remains constant.

- Other List
- Circular linked lists,
● Circular linked list is one of the type of linked list.
● Circular linked list is a collection of nodes where last node is connected to first node.
● In circular linked list, next field of last node store the address of first node.
● Circular linked list is a collection of nodes and every node consists of two fields.
Data field - It contains information of data element.
Next - It is a pointer variable which contains address of next node.

Fig. Node structure


● In circular linked list, we can move in forward with circular direction.
● In circular linked list, we can move in forward with circular direction.
● Example:

• Suppose that a small change is made to the structure of a linear list,

so that the next field in the last node contains a pointer back to the

first node rather than the null pointer. Such a list is called a circular

list.

•Note that a circular list does not have a natural 'first" or "last" node.

•We must, therefore, establish a first and last node by convention.

•One useful convention is to let the external pointer to the circular list

point to the last node, and to allow the following node to be the first

node, as illustrated in Figure 4.5.2

- Stack as a Circular List

•A circular list can be used to represent a stack or a queue.

• Let stack be a pointer to the last node of a circular list and let us
adopt the convention that the first node is the top of the stack.

•An empty stack is represented by a null list.

• The following is a C function to determine whether the stack is

empty. It is called by empty(&stack)


● Below structure in ‘C’ can be used to define the node.

typedef struct node

int data;

struct node *next;

}node;

● Below steps are used to create a single node of CLL.

1. node *p;

2. p=(node*)malloc(sizeof(node));

3. p->data=x;

4. p->next=NULL;
❖ Creating a Circular Linked List:
● To create a linked list, we will create a node one by one and add them to the end of the linked
list.
● In CLL, next field of last node store the address of first node.
● All these nodes are created by using the structure, pointer and dynamic memory allocation
function malloc.
● Below structure in ‘C’ can be used to define the node.
typedef struct node
{
int data;
struct node *next;
}node;
● Below steps are used to create a single node of CLL.
1. node *p;
2. p=(node*)malloc(sizeof(node));
3. p->data=x;
4. p->next=NULL;

● C function for creation of CLL.


- Doubly linked lists
•Although a circularly linked list has advantages over a linear list, it still
has several drawbacks.
•One cannot traverse such a list backward, nor can a node be deleted
from a circularly linked list, -given only a pointer to that node.
•In cases where these facilities are required, the appropriate data
structure is a doubly linked list.
• Each node in such a list contains two pointers, one to its predecessor
and another to its successor.
•In fact, in the Context of doubly linked lists, the terms predecessor
and successor are meaningless, since the list is entirely symmetric.
•Doubly linked lists may be either linear or circular and may or may
not contain a header node, as illustrated in Figure 4.5.5.
- Doubly Linked Lists
•Although a circularly linked list has advantages over a linear list, it still
has several drawbacks.
•One cannot traverse such a list backward, nor can a node be deleted
from a circularly linked list, -given only a pointer to that node.
•In cases where these facilities are required, the appropriate data
structure is a doubly linked list.
• Each node in such a list contains two pointers, one to its predecessor
and another to its successor.
•In fact, in the Context of doubly linked lists, the terms predecessor
and successor are meaningless, since the list is entirely symmetric.
•Doubly linked lists may be either linear or circular and may or may
not contain a header node, as illustrated in Figure 4.5.5.

•Note that the available list for such a set of nodes in the array
implementation need not be doubly linked, since it is not traversed
directionally.
• The available list may be linked together by using either the left or
right pointer. Of course, appropriate getnode and freenode routines
must be written.
•We may consider the nodes on a doubly linked list to consist of three
fields: an info field that contains the information stored in the node,
and left and right fields that contain pointers to the nodes on either
side.
•We may declare a set of such nodes using either the array or dynamic
Implementation.
• The following C routine deletes the node pointed to by p from a
doubly linked list and Stores its contents in x, using the dynamic node
implementation. It is called by delete(p, &x)
Operations on Data Structure:
The basic operations that are performed on data structures are as follows:
1. Insertion:
It is adding a new data in data structure.
2. Deletion:
It is removing a data from existing data structure
3. Searching:
It is finding location of data within given data structure
4. Sorting:
It is an arranging data in some logical order, it may be in ascending or descending order.
5. Traversing:
It is an operation that access each and every element.
6. Merging:
It is used to combine the data items of two data structure into single data structure.
❖ Insertion:
In a single linked list, the insertion operation can be performed in three ways. They are as follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Middle of the list
Inserting At Beginning of the list:
● In this operation, new node can be created and added at the beginning of a list.
● New node points to existing first node and after that make new node as head node.
● Algorithm:

- C function for inserting a node at beginning of the list.


Inserting At End of the list:
● In this operation, new node can be created and added at the end of a list.
● Algorithm:

Exaples:
- C function for inserting a node at end of the list.
Inserting at Middle of the linked list:
● In this operation, new node can be created and added in the middle of the linked list on the basis
of the given location.
● Algorithm:

● Example:
- C function for inserting a node at middle of the list.

- Deletion:
In a single linked list, the deletion operation can be performed in three ways. They are as follows...
4. Deleting At Beginning of the list
5. Deleting At End of the list
6. Deleting At Middle of the list
Deleting at Beginning of the list:
● In this operation, first node is deleted from the linked list.
● Algorithm:
● C function for deleting a node at beginning of the list.

 Application Of linked list.


 Comparison Singly Linked,
circularly linked list & doubly linked list.

You might also like