Ds 2
Ds 2
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.
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.
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.
- 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.
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.
•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
• 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.
int data;
}node;
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;
•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:
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.