DS LinkedList Notes
DS LinkedList Notes
Linked list
Data next
What is linked list? State its advantage, Explain its types [3M DEC-18]
By Nitin Sir
2|VAISHNAVI ACADEMY
2. No Random Access
In array we can access nth element easily just by using a[n].
In Linked list no random access is given to user, we have to access
each node sequentially.
3 . Time Complexity
Array can be randomly accessed , while the Linked list cannot be
accessed Randomly.
Linked list is a self referential structure in “c”. each node has variable to store
data and next field store the address of next field.
By Nitin Sir
3|VAISHNAVI ACADEMY
• In this type of linked list each node holds two pointer field.
• In doubly linked list address of next as well as preceding element
are linked with current node.
Head
What is the difference between Singly Linked List and Doubly Linked List?
• Each element in the singly linked list contains a reference to the next
element in the list, while each element in the doubly linked list contains
references to the next element as well as the previous element in the
list.
By Nitin Sir
4|VAISHNAVI ACADEMY
• Doubly linked lists require more space for each element in the list and
elementary operations such as insertion and deletion is more complex
since they have to deal with two references.
• But doubly link lists allow easier manipulation since it allows traversing
the list in forward and backward directions.
• Each Node will store the data and address of the next node to be
followed.
Head
By Nitin Sir
5|VAISHNAVI ACADEMY
1. Traversing
2. Creating
3. Inserting
4. Deleting
5. Merging
6. Sorting
7. Searching
Algorithm
1. Obtain space for new node.
2. Assign data to the data field of the new node.
3. Set the next field of the new node to the beginning of the linked list.
By Nitin Sir
6|VAISHNAVI ACADEMY
4. Change the reference pointer of the linked list to point to the new node.
By Nitin Sir
7|VAISHNAVI ACADEMY
q->next=p;
}
}
}
5 10 15 x
Algorithm:
1. Allocate memory for new node.
2. Assign value to data field of new node.
3. If (head==NULL) then head=p; got step 6.
4. Position a pointer q on last node by traversing the linked list.
5. Store the address of new node in next field of node q.
6. Stop.
By Nitin Sir
8|VAISHNAVI ACADEMY
}
q→next=p;
return(head);
}
By Nitin Sir
9|VAISHNAVI ACADEMY
p→next=q→next;
q→next=p;
return(head);
}
}
5 10 15
Algorithm
– Store the address of the first node in a pointer variable, say p.
– Move the head to the next node.
– Free the node whose address is stored in the pointer variable P.
By Nitin Sir
10 | V A I S H N A V I A C A D E M Y
p=head;
head=head-->next;
free(p);
}
else
{
while(x!=(p→next)→data && p→next!=null)
{
p=p→next;
}
if(p→next!=null)
{
q=p→next;
p→next=(p→next)→next;
free(q);
}
}
return(head);
}
Deleting of last node in linked list
5 10 15
Algorithm
2. If the first node itself is the last node then [make the linked list empty]
3. Position a pointer q on first node
4. Traverse in the list till q reach to previous node of last node
5. Position a pointer ‘p’ on last node
6. Free node whose address is store in pointer ‘p’
7. Set q→ next to null.
By Nitin Sir
11 | V A I S H N A V I A C A D E M Y
{
free(head);
return(head);
}
q=head;
While(q→next→next !=NULL)
{
q=q→next;
}
p=q→next;
free(p);
q→next=NULL;
return(head);
}
Algorithm
1. p=head
2. If the data in node p is x then
End search with success i.e. return(1)
3.Continue searching p=p→next.
4.If linked list end then end with failure i.e. return(0)
5.go to step 2
6.stop.
By Nitin Sir
12 | V A I S H N A V I A C A D E M Y
{
p=p→next;
while(p!=null)
{
if(p-->data=x)
return(1);
p=p-->next;
}
return(0);
}
}
5 10 15
By Nitin Sir
13 | V A I S H N A V I A C A D E M Y
#include<stdio.h>
#include<conio.h>
typedef struct node
{
Int data;
Struct node *next;
By Nitin Sir
14 | V A I S H N A V I A C A D E M Y
}node;
node* create();
node* insert_b(node * head, intX);
node* insert_e(node * head, intx);
node* delete(node * head);
void print(node* head);
void main()
{
int op, x;
node * head=null;
do
{
printf(“1-create\n 2-insert in beginning \n 3-Insert at
end\n 4-delete a specific\n 5-display\n 6-quit\n”);
printf(“enter your choice\n”);
scanf(“%d”,&top);
switch(op)
{
case1:
head=create();
break;
case2:
printf(“enter the data to be inserted\n);
scanf(“%d”,&x);
head=insert_b(head, x);
break;
case3:
printf(“enter data to be inserted\n”);
scanf(“%d”,&x);
head=insert_e(head, x);
break;
case4:
printf(“enter the data to be deleted\n”);
scanf(“%d”,&x);
head=delete(head,x);
break;
case5:
print(head);
By Nitin Sir
15 | V A I S H N A V I A C A D E M Y
break;
default:
}
}while(op!=6);
getch();
}
void create ()
{
node *head, *p;
int n, x, i;
printf(“ Enter no of items”);
scanf(“%d”, &n);
head=(node*)malloc(sizeof(node));
//read the data in 1st node
scanf(“%d”, &(head->data));
head->next=null;
p=head;
for(i=1;i<n;i++)
{
p->next=(node*)malloc(sizeof(node));
p=p->next;
p->next=NULL;
scanf(“%d”, &(p->data));
}
}
By Nitin Sir
16 | V A I S H N A V I A C A D E M Y
By Nitin Sir
17 | V A I S H N A V I A C A D E M Y
{
p=p→next;
}
if(p→next!=null)
{
q=p→next;
p→next=(p→next)→next;
free(q);
}
}
return(head);
}
By Nitin Sir
18 | V A I S H N A V I A C A D E M Y
5 10 15
By Nitin Sir
19 | V A I S H N A V I A C A D E M Y
{
q=q→next;
}
q→next=p;
p→next=head;
p=head;
return(head);
}
}
5 10 15
By Nitin Sir
20 | V A I S H N A V I A C A D E M Y
p→next=head;
return(head);
}
}
5 10 15
p
void delete_b(node * head)
{
node *p;
if(head→data==head);
{
free(p);
head=null
return (head);
}
else
{
p=head;
head =head→next;
free(p);
return (head);
}
}
By Nitin Sir
21 | V A I S H N A V I A C A D E M Y
5 10 15
By Nitin Sir
22 | V A I S H N A V I A C A D E M Y
By Nitin Sir
23 | V A I S H N A V I A C A D E M Y
}
printf(“%d”,p→data);
}
}
node* create();
node* insert_b(node * head, intX);
node* insert_e(node * head, intx);
node* delete(node * head);
void print(node* head);
void main()
{
int op, x;
node * head=null;
do
{
printf(“1-create\n 2-insert in beginning \n 3-Insert at
end\n 4-delete a specific\n 5-display\n 6-quit\n”);
printf(“enter your choice\n”);
scanf(“%d”,&top);
switch(op)
{
case1:
head=create();
break;
case2:
printf(“enter the data to be inserted\n);
scanf(“%d”,&x);
By Nitin Sir
24 | V A I S H N A V I A C A D E M Y
Head=insert_b(head, x);
break;
case3:
printf(“enter data to be inserted\n”);
scanf(“%d”,&x);
head=insert_e(head, x);
break;
case4:
printf(“enter the data to be deleted\n”);
scanf(“%d”,&x);
head=delete(head,x);
break;
case5:
print(head);
break;
default:
}
}while(op!=6);
getch();
}
void create ()
{
node *head, *p;
int n, x, i;
printf(“ Enter no of items”);
scanf(“%d”, &n);
head=(node*)malloc(sizeof(node));
//read the data in 1st node
scanf(“%d”, &(head->data));
head->next=head;
p=head;
for(i=1;i<n;i++)
{
p->next=(node*)malloc(sizeof(node));
p=p->next;
p->next=head;
scanf(“%d”, &(p->data));
By Nitin Sir
25 | V A I S H N A V I A C A D E M Y
}
}
By Nitin Sir
26 | V A I S H N A V I A C A D E M Y
else
{
q=head;
while(q→next != head)
{
q=q→next;
}
q→next=p;
p→next=head;
return(head);
}
}
By Nitin Sir
27 | V A I S H N A V I A C A D E M Y
By Nitin Sir
28 | V A I S H N A V I A C A D E M Y
By Nitin Sir
29 | V A I S H N A V I A C A D E M Y
{
head =p;
head→next=head→prev=NULL;
}
else
{
p→next=head;
head→prev=p;
p→prev=NULL;
head=p;
}
return (head);
}
By Nitin Sir
30 | V A I S H N A V I A C A D E M Y
By Nitin Sir
31 | V A I S H N A V I A C A D E M Y
(p→prev)→next=p;
}
return(head);
}
Insert at end
By Nitin Sir
32 | V A I S H N A V I A C A D E M Y
Deletion of a node
By Nitin Sir
33 | V A I S H N A V I A C A D E M Y
#include<stdio.h>
#include<conio.h>
typedef struct dnode
{
Int data;
struct dnode *next, *prev;
}dnode;
dnode* create();
dnode* insert_b(dnode * head, intX);
dnode* insert_e(dnode * head, intx);
dnode* delete(dnode * head);
void print(dnode* head);
void main()
{
int op, x;
dnode * head=null;
do
{
printf(“1-create\n 2-insert in beginning \n 3-Insert at
end\n 4-delete a specific\n 5-display\n 6-quit\n”);
printf(“enter your choice\n”);
scanf(“%d”,&top);
switch(op)
{
Case 1:
head=create();
By Nitin Sir
34 | V A I S H N A V I A C A D E M Y
break;
case 2:
printf(“enter the data to be inserted\n);
scanf(“%d”,&x);
Head=insert_b(head, x);
break;
case 3:
printf(“enter data to be inserted\n”);
scanf(“%d”,&x);
head=insert_e(head, x);
break;
case 4:
printf(“enter the data to be deleted\n”);
scanf(“%d”,&x);
head=delete(head,x);
break;
case 5:
print(head);
break;
default:
}
}while(op!=6);
getch();
}
void create ()
{
dnode *head, *p;
int n, x, i;
printf(“ Enter no of items”);
scanf(“%d”, &n);
head=(dnode*)malloc(sizeof(dnode));
//read the data in 1st node
scanf(“%d”, &(head->data));
head→next=head→prev=NULL;
p=head;
for(i=1;i<n;i++)
{
q=p;
By Nitin Sir
35 | V A I S H N A V I A C A D E M Y
p→next=(dnode*)malloc(sizeof(dnode));
p=p→next;
p→next=NULL;
p→prev=q;
scanf(“%d”, &(p->data));
}
}
By Nitin Sir
36 | V A I S H N A V I A C A D E M Y
By Nitin Sir
37 | V A I S H N A V I A C A D E M Y
{
p→next→prev=p→prev;
}
free(p);
return(head);
}
void print(node * head)
{
node *p;
If(head!=null)
{
p=head;
while(p→next!=head);
{
printf(“%d”,p→data);
p=p→next;
}
printf(“%d”,p→data);
}
}
Initilize a stack
By Nitin Sir
38 | V A I S H N A V I A C A D E M Y
}
Insert data in stack
stack *P;
P→data= X;
P→next= *T;
*T= P;
Int x;
Stack *p;
P= *T;
*T= P→next;
x= P→data;
free(p);
return (X);
if (Top= = Null)
By Nitin Sir
39 | V A I S H N A V I A C A D E M Y
{
return (1);
}
else
{
return (0);
}
}
# include<stdio.h>
#include<conio.h>
typedef struct stack
{
char data;
struct stack *next;
}
void main()
{
Stack *top;
char x;
init(&top);
printf(“enter the spring\n”)
While ((x=getchar())!=’\n’)
By Nitin Sir
40 | V A I S H N A V I A C A D E M Y
{
Push(&top, x);
}
while(!empty (top))
{
x =pop(&top);
printf(“%c”, x);
}
}
void init(stack**T)
{
*T=NULL;
}
Int empty (stack *top)
{
If(top= = NULL)
return(1);
return(0);
}
void push(stack**T, char x)
{
stack *p;
p=(stack*)malloc(sizeof(stack));
P→data= x;
P→next= *T;
*T=P;
}
char pop (stack**T)
{
char x;
Stack *P;
P=*T;
*T= P→next;
X= P→data;
free(p);
By Nitin Sir
41 | V A I S H N A V I A C A D E M Y
return(x);
}
Write a program, for various operations on queue using circular linked list.
#include<stdio.h>
#include<conio.h>
typedef struct node
{
int data;
struct node*next;
}node;
Void int(node **R);
Void enqueuer(node **R,int x);
Int dequeue(node **R);
Int empty(node **R);
Void print(node **R);
Void main()
{
Int x,ch;
Int n=0,i;
node *rear;
init(&rear);
do
{
Printf(“1.Insert\n2.deletion\3.print\n4.Quit”);
Printf(“Enter your option\n’);
Scanf(“%d”,&ch);
Switch(ch)
{
Case 1:
By Nitin Sir
42 | V A I S H N A V I A C A D E M Y
}
}while(ch!=4);
getch();
}
Void enqueuer(node**R, intX)
{
node*P;
P→data=X;
If(empty(*R))
{
P→next=p;
*R=P;
}
else
{
By Nitin Sir
43 | V A I S H N A V I A C A D E M Y
P→next=(*R)→next;
(*R)→next)=P;
*R=P;
}
}
Int dequeue (node**R)
{
Int X;
node*P;
P=(*R)→next;
P→data=x;
If(P→next==P)
{
*R=NULL;
free(p);
return(X);
}
(*R)→next= P→next;
Free(P);
Return(X);
}
Void print(node*rear)
{
node*P;
If (!empty(rear))
{
P=rear→next;
While(P!=rear)
{
Printf(“%d”,P→data);
P=P→next;
}
Printf(”%d”,P→data);
}
}
Int empty(node*P)
{
If (P→nexrt=== -1)
{
return(1);
By Nitin Sir
44 | V A I S H N A V I A C A D E M Y
}
return(0);
}
Important Questions
• Write a c program to implement singly list which supports the following
operation.
• Insert node in the beginning.
• Insert a node in the end
• Insert a node after a specific node.
• Deleting a specific node.
• Displaying the list.
• State difference between singly and doubly linked list along with
application.
• What is circular queue? Write a program in c to implement it. [M-10
May-2015]
• Write a c program in ‘C’ to implement doubly link-list with methods
insert, delete and search. [M-10 May-14]
• What is linked list and explain its types [3M May-18]
• Insert the following elements in AVL tree 44,17,32,78,50,88,48,62,54
explain different types of rotations that can be used. [10M May-18]
• Explain different types of tree traversal techniques with example, also
write recursive function for each traversal technique. [10M May-18]
• Write a function in c to implement binary search [5M Dec-18]
• What are expression trees ? what are its advantages ? Derive the
expression tree for following algebraic expression ?
(a + (b/c) )*((d/e)-f) [8M DEC-18]
• Give postorder and inorder traversal of a binary tree , construct the tree
[10M DEC-18]
Postorder D E F B G L J K H C A
Inorder D B F E A G C L J H K
• Write a short not on AVL tree and expression tree [10M DEC-17]
• Explain AVL tree, Insert the following elements in AVL search tree
63, 52,49,83, 92, 29, 23, 54, 13, 99 [10M DEC-17]
• Explain Threaded Binary Tree [5M DEC-17]
By Nitin Sir
45 | V A I S H N A V I A C A D E M Y
• IT
What is linked list? State its advantage, Explain its types [3M DEC-18,
May-18]
• Define minimum spanning tree. List techniques to compute minimum
spanning tree. [3M May-18]
• Define expression tree with example. [3M DEC-18, May-18]
• Write an algorithm to create doubly linked list and display it [10M May-
18]
• Define binary search tree ? Explain different operation on binary search
tree with example. [10M May-18]
• Define binary search tree ? find inorder, preorder and postorder of
following binary tree. [10M DEC-18]
•
• Define binary search tree ? write an algorithm for following operation
• Insertion
• Deletion
• What is minimum spanning tree? Draw MST using kruskal’s and prims
algorithm. [10M May-18]
• What is minimum spanning tree? Explain kruskal’s algorithm with an
example. [10M May-18]
• Explain binary search tree. Construct binary search tree for following
elements 47,12,75, 88, 90, 73, 57, 1, 85, 50, 62 [10M DEC-18]
By Nitin Sir