Unit - 3
Linked Lists
Q1) Explain singly linked list?
A1)
Each node in singly linked list points to next node and it appears to move in one direction. Thus SLL is unidirectional. Fig. shows schematic diagram of singly linked list with 4 nodes.
Fig: Singly Linked List
Q2) Write a program to create a linked list?
A2)
#include <stdio.h>
#include<conio.h>
Typedef struct node
{
Int data;
Struct node *next;
}node;
Void main()
{
Node *HEAD, *P ;
Int n,i;
Printf(“Enter no of nodes to be inserted ”)
Scanf(“%d”, &n);
HEAD = (node*)malloc(sizeof(node));//get the first node
Scanf(“%d”, &(HEAD->data));// get data in first node
HEAD->next= NULL;
P=HEAD;// currently HEAD and P points to same node
For(i=1;i<n;i++)// for n nodes
{
P->next= (node*)malloc(sizeof(node));
P=P->next;
P->next= NULL;
Scanf(“%d”, &(P->data));
}
}
Explanation:
Declare node with self referential structure.
Create HEAD node which is first node in Linked List.
HEAD = (node*)malloc(sizeof(node));
Store data in First node .
Scanf(“%d”, &(HEAD->data));//
Currently there is only one node in Linked List. So *next contains Null value and HEAD and P points to same node.
HEAD next=NULL;
P=HEAD;
Now create remaining nodes with the help of following code.
For(i=1;i<n;i++)// for n nodes
{
P->next= (node*)malloc(sizeof(node));
P=P->next;
P->next= NULL;
Scanf(“%d”, &(P->data));
}
Q3) Explain traversing in linked list?
A3)
Traversing is the most common operation that is performed in almost every scenario of singly linked list. Traversing means visiting each node of the list once in order to perform some operation on that. This will be done by using the following statements.
Ptr = head;
while (ptr!=NULL)
{
ptr = ptr -> next;
}
Algorithm
STEP 1: SET PTR = HEAD
STEP 2: IF PTR = NULL
WRITE "EMPTY LIST"
GOTO STEP 7
END OF IF
STEP 4: REPEAT STEP 5 AND 6 UNTIL PTR != NULL
STEP 5: PRINT PTR→ DATA
STEP 6: PTR = PTR → NEXT
[END OF LOOP]
STEP 7: EXIT
C function
#include<stdio.h>
#include<stdlib.h>
Void create(int);
Void traverse();
Struct node
{
int data;
struct node *next;
};
Struct node *head;
Void main ()
{
int choice,item;
do
{
printf("\n1.Append List\n2.Traverse\n3.Exit\n4.Enter your choice?");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item\n");
scanf("%d",&item);
create(item);
break;
case 2:
traverse();
break;
case 3:
exit(0);
break;
default:
printf("\nPlease enter valid choice\n");
}
}while(choice != 3);
}
Void create(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}
}
Void traverse()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Empty list..");
}
else
{
printf("printing values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}
Q4) Write a program to search an element using linked list
A4)
#include <stdio.h>
#include <stdlib.h>
// Creating node with data and a pointer
Struct node {
Int data;
Struct node *next;
}*head;
Void createList(int n);
Void search_element(int data);
Void displayList();
Int main()
{
Int n, data, element;
Printf("\nEnter the total number of nodes: ");
Scanf("%d", &n);
CreateList(n);
Printf("\nThe List is \n");
DisplayList();
Printf("\nEnter the element to be searched in the list : "); // value of the element to be searched in the list
Scanf("%d",&element);
Search_element(element);
Return 0;
}
Void createList(int n)
{
Struct node *newNode, *temp;
Int data, i;
Head = (struct node *)malloc(sizeof(struct node));
// When the list is empty
If(head == NULL)
{
Printf("Unable to allocate memory.");
}
Else
{
Printf("\nEnter the data of node 1: ");
Scanf("%d", &data);
Head->data = data;
Head->next = NULL;
Temp = head;
For(i=2; i<=n; i++)
{
NewNode = (struct node *)malloc(sizeof(struct node));
If(newNode == NULL)
{
Printf("Unable to allocate memory.");
Break;
}
Else
{
Printf("\nEnter the data of node %d: ", i);
Scanf("%d", &data);
NewNode->data = data;
NewNode->next = NULL;
Temp->next = newNode;
Temp = temp->next;
}}}}
Void search_element(int data)
{
Int count = 0;
Struct node* temp;
Temp = head;
While(temp != NULL) // Start traversing from head node
{
If(temp -> data == data)
{
Printf("\nElement found at position %d",count);
Break;
}
Else
{
Count = count + 1;
Temp = temp -> next;
}}
Printf("\n Element %d is not found in the list\n",data);
}
Void displayList()
{
Struct node *temp;
If(head == NULL)
{
Printf("List is empty.");
}
Else
{
Temp = head;
While(temp != NULL)
{
Printf("%d\t", temp->data);
Temp = temp->next;
}
Printf("\n");
}}
Q5) Explain insertion in linked list
A5)
Insertion is a three- step process −
- Create a new Link with provided data.
- Point New Link to old First Link.
- Point First Link to this New Link.
//insert link at the first location
Void insertFirst(int key, int data){
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
//point it to old first node
link->next = head;
//point first to new first node
head = link;
}
Q6) Explain deletion in linked list?
A6)
Deletion is a two step process −
- Get the Link pointed by First Link as Temp Link.
- Point First Link to Temp Link's Next Link.
//delete first item
Struct node* deleteFirst(){
//save reference to first link
struct node *tempLink = head;
//mark next to first link as first
head = head->next;
//return the deleted link
return tempLink;
}
Q7) Explain double ended queue?
A7)
A Double ended queue is a queue in which elements can be inserted or deleted from either front or rear. New elements can be added at either the front or the rear. Likewise, existing items can be removed from either end. This abstract data structure often abbreviated as deque and is also known as head-tail linked list. Deques are useful when we need both random access, and ability to delete/insert at both the ends. This hybrid linear structure provides all the capabilities of stacks and queues in a single data structure.
DeQueue can be categorized in two types
1) Input restricted DeQueue: It allows insertions at only one end
2) output restricted DeQueue: It allows deletions from only one end
Q8) Write a short note on header node?
A8)
A header node is a special node that is found at the beginning of the list. A list that contains this type of node, is called the header-linked list. This type of list is useful when information other than that found in each node is needed.
For example, suppose there is an application in which the number of items in a list is often calculated. Usually, a list is always traversed to find the length of the list. However, if the current length is maintained in an additional header node that information can be easily obtained.
Q9) Explain types of header linked list
A9)
Grounded Header Linked List
It is a list whose last node contains the NULL pointer. In the header linked list the start pointer always points to the header node. start -> next = NULL indicates that the grounded header linked list is empty. The operations that are possible on this type of linked list are Insertion, Deletion, and Traversing.
Q10) Explain the algorithm analysis of doubly linked list?
A10)
There are three ways to analyze an algorithm which are stated as below.
1. Worst case: In worst case analysis of an algorithm, upper bound of an algorithm is calculated. This case considers the maximum number of operation on algorithm. For example if user want to search a word in English dictionary which is not present in the dictionary. In this case user has to check all the words in dictionary for desired word.
2. Best case: In best case analysis of an algorithm, lower bound of an algorithm is calculated. This case considers the minimum number of operation on algorithm. For example if user want to search a word in English dictionary which is found at the first attempt. In this case there is no need to check other words in dictionary.
3. Average case: In average case analysis of an algorithm, all possible inputs are consider and computing time is calculated for all inputs. Average case analysis is performed by summation of all calculated value which is divided by total number of inputs. In average case analysis user must be aware about the all types of input so average case analyses are very rarely used in practical cases.
Q11) Explain circular linked list complexity?
A11)
Complexity of an algorithm is the function which gives the running time and space requirement of an algorithm in terms of size of input data. Frequency count refers to the number of times particular instructions executed in a block of code. Frequency count plays important role in the performance analysis of an algorithm. Each instruction in algorithm is incremented by one as its execution. Frequency count analysis is the form of priori analysis. Frequency count analysis produces output in terms of time complexity and space complexity.
- Time complexity: Time complexity is about to number of times compiler execute basic operations in instruction. That is because basic operations are so defined that the time for the other operations is much less than or at most proportional to the time for the basic operations. To determine time complexity user need to calculate frequency count and express it in terms of notations. Total time taken by program is the summation of compile time and running time. Out of these two main point to take care about is running time as it depends upon the number and magnitude of input and output.
- Space complexity: Space complexity is about to maximum amount of memory required for algorithm. To determine space complexity user need to calculate frequency count and express it in terms of notations.