Module3 DS
Module3 DS
void create()
{
printf("\n enter the no of elements to be inserted into the list\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
temp = (struct node *) malloc(sizeof (struct node));
printf("Enter the data to be inserted:\n");
scanf("%d",temp->data);
temp->link=NULL;
if(first==NULL)
first=temp;
else
{
temp->link=first;
first=temp;
}
}
}
[Link] to Front
It works same as create function, by inserting new node to front. Here only one node can
be inserted at a time.
Program code for insert front operation
void insert_front()
{
}
[Link] from Front
Suppose if we want to delete a node from front from the Fig.3, (delete node contains data
20), then make ‘temp’ as ‘first’ and delete tempdata and make ‘templink’ as ‘first’. If
first=NULL, then that means there is no element in the list.
free(first);
first=NULL;
}
else
{
while(temp->link!=NULL)
{
last=temp;
temp=temp->link;
}
last->link=NULL;
printf("Deleted element is %d\n",temp->data);
free(temp); // delete last node
}
return;
}
List is unsorted
Consider the linked list in the Fig.6. To do the search operation say for key=30 for the
unsorted list, we need to follow the following steps:
1. Set first as t
2. Repeat step 3 while t≠NULL
3. If key = tdata; search is successful and return
Else set t=tlink
4. Search failed
5. Exit
Program code for Searching a key in an Unsorted Linked List
void search()
{ struct node *temp;
int key;
temp=first;
printf(“Enter key”);
scanf(“%d”, &key);
while (temp!=NULL)
{ if(key==tempdata)
{ printf(“Search successful”);
return;
}
else temp=templink;
}
printf(“Search failed”);
List is sorted
Prepared by Saritha Suvarna, Dept of CSE,CEC Page 13
DATA STRUCTURES AND APPLICATIONS (21CS32)
Consider the linked list in the Fig.6. if we want to search a key element 20, then we need
to follow the step to do the search operation for the sorted list, we need to follow the
following steps:
1. Set temp=first
2. Repeat step 3 while temp≠NULL
3. If key>tempdata then set temp=templink
Else if key= tempdata then display search is success
Else display search is failed
4. Exit
Program code for Searching a key in a Sorted Linked List
void search()
{ struct node *temp;
int key;
temp=first;
printf(“Enter key”);
scanf(“%d”, &key);
while (temp!=NULL)
{ if(key>tempdata)
temp=templink;
else if(key==tempdata)
{ printf(“Search successful”);
return;
}
else
printf(“Search unsuccessful”);
}
}
scanf(“%d”,&node_data);
while(temp1->data != node_data)
{
if(temp1 -> link == NULL){
printf("\nGiven node not found in the list!!!");
}
temp2 = temp1;
temp1 = temp1 ->link;
}
temp2 -> link= temp1 -> link;
free(temp1);
printf("\nOne node deleted!!!\n\n");
}
which is created just now, then first=NULL; then make new node itself as first and end.
We can create ‘n’ number of nodes together. When you create one new node, then first is
not NULL now. So now we have to connect new nodes right link to old nodes left as in
Fig.1and then make first to point to temp as in Fig.1.
{
int n,i=1;
printf("\n enter the no of elements to be inserted into the list\n");
scanf("%d",&n);
while(i<=n)
{
printf(“enter the details of the node %d”,i++);
temp = (NODE)malloc(sizeof (struct node));
printf("Enter the data to be inserted:\n");
scanf("%d",temp->data);
temp->prev = temp->next = NULL;
if (FIRST==NULL)
FIRST = END = temp;
else
{ END->next=temp;
temp->prev=END;
END=temp;
}
}
}
[Link] to end
It works same as create function, by inserting new node to front. Here only one node can
be inserted at a time.
Program code for insert front operation
void insertend()
{ struct node * temp;
else
{ END->next=temp;
temp->prev=END;
END=temp;
}
}
[Link] to front
Here we start from first position. If nothing is there in the list, then ‘first’ is pointing to
NULL. So if first is NULL then the new node created itself will be pointing to last and
first. Then we create a new node temp using malloc and insert a new value=30 to it and
make its left and right link as NULL as in Fig. 3. If first is not equal to NULL, then
connect this temp to the left link of last node as in Fig.3.
Fig.3: Steps to Inserting a new Node to the front of Doubly Linked List
Fig.4: Steps to Deleting a Node from the front of a Doubly Linked List
Fig.5: Steps to Deleting a Node from the end of a Doubly Linked List
END->next=NULL;
free(temp);
}
return ;
} // end of deletionend
[Link] (Display)
If first is pointing to NULL, then print that the list is empty. Else, make temp to point to
first and display tempdata. Then make temp to point to next node and display its data
and so on until temp points to NULL. So we traverse from first node to last node.
void display_count() //Display the status of DLL and count the number of nodes in it
{
temp=FIRST;
int count=0;
{
printf("the list details:\n");
while(temp!=NULL) // display all nodes in the list
{
count++;
printf(“%d”,temp->data);
temp=temp->next;
}
printf("\n node count is %d\n",count);
} // end of else
return;
} // end of display()
7. Search
Enter the search key. Make the first node pointing to temp. So start searching the key
from first node till ‘temp’ becomes NULL. Search if key is equal to tempdata. If yes,
then search is successful else make ‘temp’ pointing to next node and compare again.
Finally if there is no match found, then conclude that search is failed.
Program code for search operation
void search()
{
int key;
printf(“Enter key”);
scanf(“%d”, &key);
temp=FIRST;
while (temp!=NULL)
{
if(key==tempdata)
{
printf(“Search successful”);
return;
}
else
temp=tempnext;
}
printf(“Search failed”);
}
3.6 Circular Linked List
In a circular linked list, the last node contains a pointer to the first node of the list. We can
havea circular singly linked list as well as a circular doubly linked list. While traversing a
circular linked list, we can begin at any node and traverse the list in any direction
forward or backward, until we reach the same node where we started. Thus, a circular
linked list has no beginning and no ending. Figure 3.6 shows a circular linked list.
void insertAtEnd()
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
printf("\n enter the data:");
scanf("%d",&newNode -> data);
if(head == NULL)
{
head = newNode;
newNode -> next = head;
}
else
{
temp = head;
while(temp -> next != head)
Prepared by Saritha Suvarna, Dept of CSE,CEC Page 28
DATA STRUCTURES AND APPLICATIONS (21CS32)
Grounded header linked list which stores NULL in the next field of the last node as in
Fig. 7.20 (a).
Circular header linked list which stores the address of the header node in the next
field of the last node. Here, the header node will denote the end of the list as in Fig.
7.20 (b).
The push operation is used to insert an element into the stack. The new element is
added at the topmost position of the stack. Consider the linked stack shown in Fig.
3.9.2(a). To insert an element with value 9, we first check if TOP=NULL. If this
is the case, then we allocate memory for a new node, store the value in its DATA
part and NULL in its NEXT part. The new node will then be called TOP.
However, if TOP! =NULL, then we insert the new node at the beginning of the
linked stack and name this new node as TOP. Thus, the updated stack becomes as
shown in Fig. 3.9.2(b).
Figure 3.9.3 shows the algorithm to push an element into a linked stack. In Step 1,
memory is allocated for the new node. In Step 2, the DATA part of the new node
is initialized with the value to be stored in the node. In Step 3, we check if the new
node is the first node of the linked list. This is done by checking if TOP = NULL.
In case the IF statement valuates to true, then NULL is stored in the NEXT part of
the node and the new node is called TOP. However, if the newnode is not the first
node in the list, then it is added before the first node of the list (that is, the TOP
node) and termed as TOP.
case TOP! =NULL, then we will delete the node pointed by TOP, and make TOP
point to the second element of the linked stack. Thus, the updated stack becomes
as shown in Fig. 3.9.4 (b).
void pop()
{
if(top == NULL)
printf("\nStack is overflow!!!\n");
else
{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else
{
struct Node *temp = top;
while(temp->next != NULL)
{
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}
3.10 Linked Queue
We have seen how a queue is created using an array. Although this technique of
creating a queue is easy, its drawback is that the array must be declared to have
some fixed size.
If we allocate space for 50 elements in the queue and it hardly uses 20–25
locations, then half of the space will be wasted. And in case we allocate less
Prepared by Saritha Suvarna, Dept of CSE,CEC Page 36
DATA STRUCTURES AND APPLICATIONS (21CS32)
memory locations for a queue that might end up growing large and large, then a lot
of re-allocations will have to be done, thereby creating a lot of overhead and
consuming a lot of time.
In case the queue is a very small one or its maximum size is known in advance,
then the array implementation of the queue gives an efficient implementation.
But if the array size cannot be determined in advance, the other alternative ,i.e.
the linked representation is used.
In a linked queue, every element has two parts, one that stores the data and
another which holds the address of the next element. The START pointer of the
linked list is used as FRONT. Here we will also use another pointer called REAR,
which will store the address of the last element in the queue. All insertions will be
done at the rear end and all the deletions will be done at front end. If
FRONT=REAR=NULL, then it indicates that the queue is empty. The linked
representation of queue is shown in Fig. 3.10.1.
In Step 2, the DATA part of the new node is initialized with the value to be stored
in the node.
In Step 3, we check if the new node is the first node of the linked queue.
This is done by checking if FRONT = NULL. In case the new node is tagged as
FRONT and REAR. Also NULL is stored in the NEXT part of the node.
However, if the new node is not the first node in the list, then it is added at the
REAR end of the linked queue.
Delete Operation
The delete operation is used to delete the element that is first inserted
The delete operation is used to delete the element that is first inserted into the
queue. i.e. the element whose address is stored at FRONT.
However, before deleting the value, we must first check if FRONT=NULL,
because if this is the case, then it means that the queue is empty and no more
deletions can be done. If an attempt is made to delete a value from a stack that is
already empty, an UNDERFLOW message is printed.
Consider the stack shown in Fig. 3.10.4(a). To delete an element, we first check if
FRONT=NULL. If it is false, then we delete the 1st node pointed by the FRONT.
The FRONT will now point to the 2nd element of the linked queue. Thus the
updated queue becomes as shown in Fig. 3.10.4(b).
PTR that points to FRONT. In Step 3, FRONT is made to point to the next node in
sequence. In Step 4, the memory occupied by PTR is given back to the free pool.
char usn[20],name[10],branch[5];
unsigned long long int phno;
int sem;
struct Node *next;
};
typedef struct Node * NODE;
NODE temp,front = NULL,rear = NULL;
void insert();
void delete();
void display();
void main()
{
int choice, value;
switch(choice){
case 1:insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void insert()
{
NODE newNode;
newNode=(NODE)malloc(sizeof(struct Node));
printf("Enter USN: ");
scanf("%s",newNode->usn);
printf("Enter NAME: ");
scanf("%s",newNode->name);
printf("Enter Branch: ");
scanf("%s",newNode->branch);
printf("Enter phone Number: ");
scanf("%llu",&newNode->phno);
printf("Enter Semester: ");
scanf("%d",&newNode->sem);
newNode->next=NULL;
if(front == NULL)
{
else{
rear -> next = newNode;
rear = newNode;
rear->next=NULL;
}
printf("\nInsertion is Success!!!\n");
}
void delete()
{
if(front == NULL)
printf("\nQueue is Underflow!!!\n");
else{
temp = front;
front = front -> next;
printf("\nDeleted node is with usn: %s", temp->usn);
free(temp);
}
}
void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
temp = front;
while(temp->next != NULL){
printf("The Student information in the node is\n");
printf("\nUSN:%s\nNAME:%s\nBRANCH:%s\nPHONE
NO.:%llu\nSEM:%d\n",temp->usn,temp->name,temp->branch,temp-
>phno,temp->sem);
}
printf("\nUSN:%s\nNAME:%s\nBRANCH:%s\nPHONE
NO.:%llu\nSEM:%d\n",temp->usn,temp->name,temp->branch,temp-
>phno,temp->sem);
}
}
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct node // polynomial node
{
int coef;
int x,y,z;
struct node *link;
};
typedef struct node *NODE;
x=(NODE)malloc(sizeof(struct node));
return x;
} // end of getnode
NODE readpoly()
{
NODE temp,head,cur;
char ch;
head=getnode(); // create a head node and set all values to -1 it is similar to
FIRST in SLL program
head->coef=-1;
head->x=-1;
head->y=-1;
head->z=-1;
head->link=head; // self reference
do
{
temp=getnode(); // create a polynomial node
printf("\nEnter the coefficient and exponent in decreasing
order\n");
scanf("%d%d%d%d",&temp->coef,&temp->x,&temp->y,&temp-
>z );
cur=head;
while(cur->link!=head) // find the last node
cur=cur->link;
cur->link=temp; // connect new node to the last node
temp->link=head; // point back to head
printf("\nDo you want to enter more coefficients(y/n)");
fflush(stdin); // to clear the stdin buffer
scanf("%c",&ch);
} while(ch =='y' || ch == 'Y');
return head; // return the polynomial list
} // end of readpoly
void attach(int cf,int x1,int y1, int z1, NODE *ptr) // function to attach the A and
B polynomial node to C Polynomial
{
NODE temp;
temp=getnode();
temp->coef=cf;
temp->x=x1;
temp->y=y1;
temp->z=z1;
(*ptr)->link=temp;
*ptr=temp;
} // end of attach
int sum,done=0;
starta=a;
a=a->link;
b=b->link;
c=getnode(); // create list C to store A+B
c->coef=-1;
c->x=-1;
c->y=-1;
c->z=-1;
lastc=c;
do{
switch(compare(a,b))
{
case -1:attach(b->coef,b->x,b->y,b->z,&lastc);
b=b->link;
break;
case 0:if(starta==a) done=1;
else{
sum=a->coef+b->coef;
if(sum)
attach(sum,a->x, a->y,a->z,&lastc);
a=a->link;b=b->link;
}
break;
case 1: if(starta==a) done=1;
attach(a->coef,a->x, a->y,a->z,&lastc);
a=a->link;
break;
}
}while(!done); // repeate until not done
lastc->link=c; // point back to head of C
return c; // return answer
}
{
NODE cur;
cur=ptr->link;
while(cur!=ptr) // To print from HEAD node till END node
{
printf("%d*x^%d*y^%d*z^%d",cur->coef,cur->x, cur->y,
cur->z);
cur=cur->link; // move to next node
if (cur!=ptr)
printf(" + ");
}
} // end of print
void main(void)
{
int i, ch;
NODE a=NULL,b,c;
while(1)
{
printf("\n1: Represent first polynomial A");
printf("\n2: Represent Second polynomial B");
printf("\n3: Display the polynomial A");
printf("\n4: Display the polynomial B");
printf("\n5: Add A & B polynomials"); // C=A+B
printf("\n6: Evaluate polynomial C");
printf("\n7: Exit");
printf("\n Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the elements of the polynomial A");
a=readpoly();
break;
case 2:printf("\nEnter the elements of the polynomial B");
b= readpoly();
break;
case 3: print(a); // display polynomial A
break;
case 4:print(b); // display polynomial A
break;
case 5: c=addpoly(a,b); // C=A+B
printf("\nThe sum of two polynomials is: ");
print(c); // display polynomial C
printf("\n");
break;
case 6:evaluate(c); // Evaluate polynomial C
break;
Prepared by Saritha Suvarna, Dept of CSE,CEC Page 50
DATA STRUCTURES AND APPLICATIONS (21CS32)
case 7: return;
default: printf("\nInvalid choice!\n");
} //end of switch
} // end of while
} // end of main
{
printf("\n OVERFLOW");
}
else
{
ptr->row = r;
ptr->column = c;
ptr->value = v;
if(HEAD == NULL)
{
ptr->next = NULL;
HEAD = ptr;
}
else
{
temp = HEAD;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
}
}
}
void print()
{
struct list *tmp = HEAD;
printf("ROW NO COLUMN NO. VALUE \n");
while (tmp != NULL)
{
printf("%d \t\t %d \t\t %d \n", tmp->row, tmp->column, tmp->value);
tmp = tmp->next;
}
}
Prepared by Saritha Suvarna, Dept of CSE,CEC Page 53
DATA STRUCTURES AND APPLICATIONS (21CS32)
Question Bank
1. What is linked list? Explain the different types of linked list with examples.
2. Give a node structure to create a linked list of integers and write a C function to
perform the following.
a. Create a three-node list with data 10, 20 and 30
b. Inert a node with data value 15 in between the nodes having data values 10
and 20
c. Delete the node which is followed by a node whose data value is 20
d. Display the resulting singly linked list.
3. With node structure show how would you store the polynomials in linked lists? Write
C function for adding two polynomials represented as circular lists.
4. Write a note on: i. Linked representation of sparse matrix ii. Doubly linked list.
5. Write a function to insert a node at front and rear end in a circular linked list. Write
down sequence of steps to be followed.
6. What is linked list? Explain the different types of linked list with examples.
7. Give a node structure to create a linked list of integers and write a C function to
perform the following.
a) Create a three-node list with data 10, 20 and 30
b) Inert a node with data value 15 in between the nodes having data
values 10 and 20
c) Delete the node which is followed by a node whose data value is 20
d) Display the resulting singly linked list.
8. With node structure show how would you store the polynomials in linked lists? Write
C function for adding two polynomials represented as circular lists.
9. Write a note on: i. Linked representation of sparse matrix ii. Doubly linked list.
10. Write a function to insert a node at front and rear end in a circular linked list. Write
down sequence of steps to be followed.
11. Write a C program to perform the following operations on doubly linked list: i. Insert
a node ii. Delete a node.
12. Write a C function to insert a node at front and delete a node from the rear end in a
circular linked list.
13. Describe the doubly linked lists with advantages and disadvantages. Write a C
function to delete a node from a circular doubly linked list with header node.
14. Write a C function for the concatenation of linked lists.
15. Write a C function to add two-polynomials represented as circular list with header
node.
16. Write a C function to perform the following i. Reversing a singly linked list ii.
Concatenating singly linked list. iii. Finding the length of the circular linked list. iv.
To search an element in the singly linked list
17. Write a node structure of linked stack. Write a function to perform push and pop
operations on linked stack.
18. List out the differences between doubly linked list over singly linked list. Write a C
functions to perform the following i. Inserting a node into a doubly linked circular list
ii. Deletion from a doubly linked circular list.
19. Write a function for singly linked lists with integer data, to search an element in the
list that is unsorted and a list that is sorted.
20. Given 2 singly linked lists. LIST-1 and LIST-2. Write an algorithm to form a new list
LIST-3 using concatenation of the lists LIST-1 and LIST-2.
21. Write a note on header linked list. Explain the widely used header lists with
diagrams. 23. Illustrate with examples how to insert a node at the beginning, INSERT
a node at intermediate position, DELETE a node with a given value
22. List out any 2 differences between doubly linked lists and singly linked list, Illustrate
with example the following operations on a doubly linked list: i. Inserting a node at
the beginning. ii. Inserting at the intermediate position. iii. Deletion of a node with a
given value
23. For the given sparse matrix write the diagrammatic linked list representation