Module 1 To 5 - (Data Structure)
Module 1 To 5 - (Data Structure)
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Module 3:Linked List
Definition: The linked list is a linear data structure consists of nodes which are created dynamically. Each node of the linked list
is having 2 parts one is info part(data) and another is link(next) part.
In the linked list there exists a special pointer called START (HEAD) which holds the address of the first node.
Example:
A linked list is a dynamic data structure. The number of nodes in a list is not fixed and can grow and shrink on demand. Any
application which has to deal with an unknown number of objects will need to use a linked list.
Arrays:
Advantages
Disadvantage
1. The size of the array is fixed. The upper limit on the array size should be known.
2. Array elements are stored contiguously.
3. Insertion and deletion operation in an array is expensive.
1. In linked list, nodes are created dynamically so the list can grow and shrink during the execution of program.
2. Insertion and deletion operations are easier and efficient.
1. For storing a given information it requires the space for the information and the space for the link part as well. For ex: if
you want to store 5 floating point kind information in a linked list, then the total number of bytes we required is (5 X 6) +
2 = 32 bytes, whereas if we store the same in an array it requires (5 X 4) = 20 bytes which is less than 32 bytes.
Here 102,501,203,105 are the memory location of nodes. For representing the above link list memory, two linear arrays are
required. i)info ii)link
START INFO LINK
5 1 1
2 E 2 NULL
3 C 3 11
4 4
5 A 5 9
6 6
7 7
8 8
9 B 9 3
10 10
11 D 11 2
12 12
Declaration of Nodes
struct node{
int info;
struct node *next;
};
Garbage collection:
The memory allocation for the objects is done from the heap. If some object is created and which is not been in use from a long time
then such an object is called garbage. The garbage collection is a technique in which all such garbage is collected and recycled. The
garbage collector cleans up the heap so that the memory occupied by the unused objects can be freed and can be used allocating for
the New objects. The garbage collection algorithm works in two steps as follows:
1.MARK- In the marking process all the live object are located and marked them as non-collected objects.
2.SWEEP- In this step, all the unmarked object are swept from the heap and the space that has been allocated by these objects can
be used for allocating new objects.
But the drawback of this mark and sweep algorithm is that multiple fragments of memory get created hence the technique called
compaction is used. In compaction technique the allocated space is moved up to form a contiguous block of free space in the heap.
/* write a program for the creation of node having one information part */
#include <stdio.h>
#include<conio.h>
#include<alloc.h>
Struct node
{
int info;
struct node *link;
};
Struct node *start=NULL;
Struct node *new1;
void main()
{
int i,n;
printf(“Enter the no of nodes you want to create in the link list”);
scanf(“%d”,&n);
void create_list();
for(int i=0;i<n;i++)
create_list();
getch();
}
void create_list()
{
new1 = (struct node*) malloc (sizeof(struct node));
if(new1 = = NULL)
{
printf(“memory is not available”);
exit();
}
printf(“Enter the information part of the node”);
scanf(“%d”,&new1→info);
new1→link = NULL;
new1→link=start;
start = new1;
}
/* write a program for the creation of node having more than one information part */
#include <stdio.h>
#include<conio.h>
#include<alloc.h>
Struct node
{
int age;
int sal;
char name[15];
struct node *link;
};
Struct node *start=NULL;
Struct node *new1;
void main()
{
int I,n;
clrscr();
printf(“Enter the no of nodes you want to create in the link list”);
scanf(“%d”,&n);
void create_list();
for(int i=0;i<n;i++)
create_list();
getch();
}
void create_list()
{
int ch;
int c;
do{
new1 = (struct node*) malloc (sizeof(struct node));
if(new1 = = NULL)
{
printf(“memory is not available”);
exit();
}
printf(“Enter the information part of the node”);
printf(“Enter the age, salary”);
scanf(“%d %d”,&new1→age, &new1→sal);
printf(“Enter the name”);
scanf(“%n”, name);
new1→link = NULL;
new1→link=start;
start = new1;
c++;
printf(“Enter your choice”);
printf(“1. For creation of another node\n”);
printf(“2 Enter ‘0’ to stop the creation of node\n”);
scanf(“%d”,&ch);
}while(ch!=0);
printf(“The no of nodes created is %d”,c);
}
void main(){
clrscr();
create_list();
getch();
}
void reverse_traverse()
{
int a[30],i=0;
struct node *start;
struct node *temp;
for(temp= start;temp!=NULL;temp = temp->link)
{
a[i] = temp->info;
i++;
}
i--;
while(i>=0){
printf("%d",a[i]);
i = i+1;
}
}
Searching an element in link list
void searching()
{
if(start = = NULL)
{
printf("list is empty");
exit();
}
temp = start;
while(temp!=NULL && temp->info!=item)
temp = temp->link;
if(temp->info = = item)
{
printf("item is found");
}
if(temp = = NULL){
printf("Item is not found");
}
}
Write an algorithm/program for counting the number of nodes in the link list
C-Procedure
void count()
{
int c;
struct node * temp;
if(start = = '\0')
printf("underflow");
else{
for(temp=start;temp!=NULL;temp = temp->link)
c++;
}
printf("the no of nodes present in the list is=%d", c);
}
void concatenate()
{
//start1, start2, link, list1, list2 has been declared as global variables
struct node *temp;
for(temp = start1;temp->link!='\0'; temp= temp->link);
temp->link = start2;
start2 = "NULL";
}
void insert_atpos(){
int i, c=0, pos;
struct node *temp;
printf("Enter the position where the node has to be inserted\n");
scanf("%d,&pos);
new1= (struct node*) malloc(sizeof( struct node));
if(new1 == NULL)
{
printf("overflow");
}
else{
printf("enter the item to be inserted as list node");
scanf(%d",&new1-->info);
new1-->link = NULL;
}
// code for checking validity of position
for(temp=start;temp-->link != '\0';temp = temp-->link)
c++;
if(pos>c+1)
{
printf("invalid position");
exit();
}
if(pos = = 1)
{
new1->link = start;
start = new;
}else if(pos = = c+1)
{
for(temp=start; temp->link!='\0';temp=temp->link);
temp->link = new1;
}else{
i=1;
temp=start;
while(i<pos-1)
{
temp = temp->link;
i = i+1;
}
new1->link = temp->link;
temp->link = new1;
}
}
Deletion
Deletion of 1st Node
void delete_1st_node(){
struct node *temp;
if(start = =NULL)
{
printf("underflow");
exit();
}
temp = start;
if(temp->link = = NULL)
{
start = NULL;
free(temp);
}else{
start = temp->link;
temp->link = NULL;
free(temp);
}
}
Deletion of Last Node
void delete_last(){
struct node *temp, *prev;
if(start = =NULL)
printf("underflow");
if(start->link = = '\0')
{
temp = start;
start = '\0';
free(temp);
}else{
for(temp = start;temp->link!='\0';temp=temp->link)
{
prev=temp;
}
prev->link = NULL;
free(temp);
}
}
void delete_at_pos(){
int i, c=0, pos;
struct node *prev, *temp;
printf("Enter the position where the node has to be deleted\n");
scanf("%d,&pos);
if(start == '\0')
{
printf("overflow");
exit();
}
// code for checking validity of position
for(temp=start;temp-->link != '\0';temp = temp-->link)
c++;
if(pos>c)
{
printf("invalid position");
exit();
}
if(pos = = 1)
{
if(start->link = ='\0')
{
temp = start;
start = NULL;
free(temp);
}
else{
temp = start;
start = temp->link;
temp_.link = '\0';
free(temp);
}
}
}else if(pos = = c)
{
for(temp=start; temp->link!='\0';temp=temp->link);\{
prev = temp;
}
prev->link = '\0';
free(temp);
}
}else{
prev = '\0';
temp = start;
for(i=1;i<pos;i++)
{
prev = temp;
temp = temp->link;
}
prev->link = temp->link;
temp->link = '\0';
free(temp);
}
C Program:
void delete_fnode()
{
//start, link are declared as a global variable
struct node *temp;
if(start== NULL)
{
printf(“undeflow”);
exit();
}
else if(start→link==start)
{
temp=start;
start→link=NULL;
start=NULL;
free(temp);
}
else
{
temp=start;
while(temp→link!=start)
{
temp=temp→link;
}
temp→link=start→link;
temp=start;
start=start→link;
temp→link=NULL;
free(temp);
}
}
B) Deletion of last node:
C Program:
void delete_lnode()
{
//start, link are declared as global variable.
struct node *temp, *prev;
if(start==NULL)
{
printf(“underflow”);
exit();
}
else if(start→link==start)
{
temp=start;
temp→link=NULL;
start=NULL;
free(temp);
}
else
{
for(temp=start, temp→link!=start;temp=temp→link)
prev→link=temp→link;
temp→link=NULL;
free(temp);
}
}
Searching:
void search()
{
// start, link and info are declared as global variable.
int item, c=0, nd_node=0;
struct node *temp;
printf(“Enter the item for searching”);
scanf(“%d”,&item);
if(start==NULL)
{
printf(“underflow”);
exit();
}
else
{
temp=start;
do
{
nd_node=nd_node+1;
if(temp→info==item)
{
c=c+1;
printf(“Item is found in node %d”,nd_node);
}
temp=temp→link;
}while(temp!=start);
if(c==0)
printf(“searching is unsuccessful”);
}
}
Sorting
C Program
void sort()
{
//start, link, info are declared as global variables.
int num;
struct node *temp1, *temp2;
for(temp1=start;temp→link!=start; temp1=temp1→link)
{
for(temp2=temp1→link; temp2!=start; temp2=temp2→link)
{
if(temp1→info>temp2→info)
{
num=temp1→info;
temp1→info= temp2→info;
temp2→info=num;
}
}
}
}
Concatenation in single circular link list
void concatenation()
{
//start1,start2, link, list1, list2, temp are global variables.
for(temp=start1;temp→link!=start1;temp=temp→link);
temp→link=start2;
for(temp=start1;temp→link!=start2;temp=temp→link);
temp→link=start1;
start2=NULL;
}
Forward Display:
C Program:
void display_forward()
{
//start,info,next are declared as global variable.
struct node *temp;
if(start==NULL)
{
printf(“List is empty”);
exit();
}
else
{
temp=start;
do
{
printf(“%d”,temp→info);
temp=temp→next;
}while(temp!=start);
}
}
Backward Display:
C Program:
void display_backward()
{
//start,info,next and prev are declared as global variable.
struct node *temp, *temp1;
if(start==NULL)
{
printf(“List is empty”);
exit();
}
else
{
temp=start;
while(temp→next!=start)
temp=temp→next;
temp1=temp;
do
{
printf(“%d”,temp→info);
temp=temp→next;
}while(temp!=start);
}
}
NOTE: In circular link list, the last node can be accessed directly if we have the address of 1 st node or the value of start. Also we
can access the 1st node if the address of last node is known.
HEADER LINKED LIST
Definition : Header link list is a link list where a special node 'HEAD' is there, which keep the information about the entire list.
This header node is the first node of the list. Start keeps the address of head node.
The information kept in the header node are:
1.How many nodes are there.
2.which type of list it is.
3.What type data are there in the list.
Note: To perform all the operation in single link list, change the underflow condition, single node checking condition and set
temp=start-->link in place of start.
PROCEDURE create_SHLL(new1,h,link,info,start,c,type,node_no,list_type)
Step 1: set c=0
Step 2: do
i) new1=allocate memory
ii)if(new1=NULL) then
a)print overflow
b)exit
iii) else
a)new1-->link=NULL
b)read new1-->info
new-->link=start
start=new1
c=c+1
Step 3: printf Enter your choice
i)creation
ii)stop
Step 4: While(ch!=0)
[end of step 2 do...while loop]
Step 5: h=allocate a memory.
Step 6:if(h=NULL ) then
i)print malloc can't allocate memory
ii)exit
Step 7: else
i)h-->type="integer"
ii)h-->node_no=c
iii)read h-->list_type
iv)h-->link=start
v)start=h
[end of step 6 if...else structure]
Step 8: Exit
C Program:
void Create_SHLL()
{
struct node *new1;
struct head *h;
int ch,c=0;
do
{
new1=(struct node *)malloc(sizeof(struct node));
if(new1==NULL)
{
printf("overflow");
exit();
}
else
{
new1-->link=NULL;
printf("Enter the information part:");
scanf("%d",&new1-->info);
new1-->link=start;
c=c+1;
}
printf("Enter your choice \n 1:creation\n 2.stop\n");
scanf("%d",&ch);
} while(ch!=0);
h=(struct head *)malloc(sizeof(struct head));
if(h==NULL)
{
printf("malloc cannot allocate memory for head node");
exit();
}
else
{
h-->type="integer";
h-->node_no=c;
printf("Enter the type of the list");
scanf("%s", &h-->list_type);
h-->link=start;
start=h;
}
}
Traversal of single header link list:Visiting all the elements in the list exactly once is called traverse.
Algorithm:
PROCEDURE Traverse(start,link,list,temp,info)
Step 1: if(start-->link=NULL) then
i)print the list is empty
ii)exit
Step 2: else
i)temp=start-->link.
ii)while temp!=NULL
a)print temp-->info
b)temp=temp-->link
[end of while]
[end of if...else loop]
Step 3:exit
C Program:
void traverse()
{
struct node *temp;
if(start-->link==NULL)
{
printf("list is empty");
exit();
}
else
{
for(temp=start-->link; temp!=NULL;temp=temp-->link)
printf("%d",temp-->info);
}
}
2) DOUBLE HEADER LINK LIST:The double link list containing head node as its first node is called double header list.
Representation of double header link list
Condition for underflow:
if(start-->next==NULL)
then underflow or the list is empty.
Note: For traversal operation change the underflow condition and replace the temp=start by temp=start-->next
Traversal of double header link list:Visiting all the elements in the list exactly once is called traverse.
Backward traversal:
Algorithm:
PROCEDURE Traverse_backward(start,prev,next,list,temp,info)
Step 1: if(start-->next=NULL) then
i)print the list is empty
ii)exit
Step 2: else
i)temp=start-->next.
ii)while temp-->next!=NULL
a) temp=temp-->next
[end of while]
iii)while (temp!=start)
a)print temp-->info
b)temp=temp-->prev
[end of while]
[end of if...else loop]
Step 3:exit
C Program
void traverse_backward()
{
if(start-->next==NULL)
{
printf("list is empty");
exit();
}
else
{
while (temp-->next!=NULL)
temp=temp-->next
while (temp!=start)
{
printf("%d",temp-->info);
temp=temp-->prev
}
}
}
The single header link list in which the last node points to the first node is called single circular header link list.
Representation of single circular header link list
Condition for underflow:
if(start-->link==start)
then underflow or the list is empty.
Traversal of single header link list:Visiting all the elements in the list exactly once is called traverse.
Algorithm:
PROCEDURE Traverse(start,link,list,temp,info)
Step 1: if(start-->link=start) then
i)print the list is empty
ii)exit
Step 2: else
i)temp=start-->link.
ii)while( temp!=start)
a)print temp-->info
b)temp=temp-->link
[end of while]
c)print the address of 1st node i.e., start
[end of if...else loop]
Step 3:exit
C Program:
void traverse()
{
if(start-->link==start)
{
printf("list is empty");
exit();
}
else
{
for(temp=start-->link; temp!=start;temp=temp-->link)
printf("%d",temp-->info);
printf("%u",start);
}
}
Traverse:
Forward traverse
Algorithm:
C program
void forward_traverse()
{
if(start-->next==NULL)
{
printf("The list is empty");
exit();
}
else
{
for(temp=start-->next;temp!=start;temp-->next)
printf("%d",temp-->info);
}
}
Backward traverse
Algorithm:
Step 1: if(start-->next=start) then
a)print list is empty
b)exit
Step 2: else
a) temp=start-->next
b)while(temp-->next!=start)
temp=temp-->next
[end while]
c)while(temp!=start)
i)print temp-->info
ii)temp=temp-->prev
[end of while]
[end of if..else structure]
Step 3: exit
C program
void backward_traverse()
{
if(start-->next==start)
{
printf("The list is empty");
exit();
}
else
{
for(temp=start-->next;temp-->next!=start;temp-->next);
for(;temp!=start; temp=temp-->prev)
printf("%d",temp-->info);
}
}
LINKED STACKS
Declaration of stack:
struct node
{
int info;
struct node *next;
};
struct node *top=NULL;
Note:
1. The terminology we used here is top in place of head.
2.Any element we want to push into stack must be inserted as first node, as top is the list pointer variable.
3.Top should hold the address of the node that we want to insert.
Push:Push operation is exactly similar to insertion of a node at first position.
Algorithm:
PROCEDURE PUSH(top,info,next,list,new1)
C-Program:
void push()
{
struct node *new1;
new1=(struct node *)malloc(sizeof(struct node));
if(new1==NULL)
{ printf("overflow");
exit();
}
else
{
printf("Enter the information part:");
scanf("%d",&new1-->info);
new1-->next=NULL;
new1-->next=top;
top=new1;
}
}
Pop: we can pop the node whose address is holded by list pointer variable top. So, as top holds the address of 1st node. We can
delete 1st node which has been inserted lastly.
Algorithm:
PROCEDURE POP(top,next,temp)
Step 1: set temp=NULL
Step 2: [check for underflow]
if(top==NULL)
a.print underflow
b.exit
Step 3: [single node]
else if(top->next==NULL)
a.temp=top
b.top=NULL
c.free the temp.
Step 4: else
a.temp=top
b.top=temp-->next
c.temp-->next=NULL
d.free the temp using free() function
Step 5: Exit
C-Program:
void pop()
{
temp=NULL;
if(temp==NULL)
{ printf("underflow");
exit();
}
else if(top-->next=='\0')
{ temp=top;
top=NULL;
free(temp);
}
else
{
temp=top;
top=temp-->next;
temp-->next=NULL;
free(temp);
}
}
Display:
Algorithm:
Step 1: set temp=NULL.
Step 2: [check for underflow]
if(top==NULL)
a.print the list is empty
b.exit
Step 3:else
for(temp=top; temp!=NULL; temp=temp-->next)
a.print(temp-->next)
b.prnit NULL
Step 4:exit
C-Program:
void display()
{
temp=NULL;
if(top==NULL)
{ printf("list is empty");
exit();
}
else
{ for(temp=top;temp!=NULL;temp=temp-->link)
printf("%d-->",temp-->info);
printf("NULL");
}
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
} *top=NULL,*temp;
void main()
{
int ch;
printf(“\n 1. PUSH \n 2.POP\n 3.DISPLAY \n 4.EXIT”);
while(1)
{
printf(“Enter the choice:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}
Declaration:
struct node
{
int info;
struct node *next;
} *front=NULL,*rear=NULL;
NOTE:
1) In linked list representation of queue, front acts as the list pointer variable and holds the address of node that is first inserted.
2) Rear is the pointer variable which holds the address of node which has been inserted at last.
3)Here we can insert node/element after last node.
4)We can delete the node whose address is holed by front that is the first node.
Algorithm:
PROCEDURE Insert_Q(next,info,front,rear,new1)
C Program
void Qinsert()
{
new1=(struct node *)malloc(sizeof(struct node));
if(new1==NULL)
{
printf("overflow");
exit();
}
else
{
printf("Enter the information part:");
scanf("%d",&new1-->info);
new1-->next=NULL;
if(rear==NULL)
{
front=new1;
rear=new1;
}
else
{
rear-->next=new1;
rear=new1;
}
}
}
Deletion:
Algorithm:
PROCEDURE Delete_Q(next,info,front,rear)
C-Program:
void delete_Q()
{ struct node *temp;
if(front==NULL)
{ printf("underflow");
exit();
}
else if(front-->next==NULL)
{ temp=front;
free(temp);
front=NULL;
rear=NULL;
}
else
{
temp=front;
front=temp-->next;
temp-->next=NULL;
free(temp);
}
}
Display:
Algorithm:
Step 1: set temp=NULL.
Step 2: [check for underflow]
if(front==NULL)
a.print the list is empty
b.exit
Step 3:else
for(temp=front; temp!=NULL; temp=temp-->next)
a.print(temp-->next)
b.prnit NULL
Step 4:exit
C-Program:
void display_Q()
{
temp=NULL;
if(front==NULL)
{ printf("list is empty");
exit();
}
else
{ for(temp=front;temp!=NULL;temp=temp-->link)
printf("%d-->",temp-->info);
printf("NULL");
}
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
} *front=NULL,*rear=NULL,*temp;
void main()
{
int ch;
printf(“\n 1. INSERT \n 2.DELETE\n 3.DISPLAY \n 4.EXIT”);
while(1)
{
printf(“Enter the choice:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: insert_Q();
break;
case 2: delete_Q();
break;
case 3: display_Q();
break;
case 4: exit(0);
}
}
}
POLYNOMIALS
Polynomial Representation Through Link List
A polynomial is an expression that contains more than two terms. A term is made up of coefficient and exponent. An example of
polynomial is
P(x) = 4x3+6x2+7x+9
A polynomial thus may be represented using arrays or linked lists. Array representation assumes that the exponents of the given
expression are arranged from 0 to the highest value (degree), which is represented by the subscript of the array beginning with 0.
The coefficients of the respective exponent are placed at an appropriate index in the array. The array representation for the above
polynomial expression is given below:
A polynomial may also be represented using a linked list. A structure may be defined such that it contains two parts- one is the
coefficient and second is the corresponding exponent.
Thus the above polynomial may be represented using linked list as shown below:
struct node
{
int coeff;
int expo;
struct node *link;
};
5x3y2+6x2y2+7x-6y+15
Note:
1) Total no. of nodes in the list == Total no. of terms in the polynomial.
2) If total number of variables in the polynomial = n , then each node will have n+2 parts.
For adding two polynomials using arrays is straightforward method, since both the arrays may be added up element wise
beginning from 0 to n-1, resulting in addition of two polynomials. Addition of two polynomials using linked list requires
comparing the exponents, and wherever the exponents are found to be same, the coefficients are added up. For terms with
different exponents, the complete term is simply added to the result thereby making it a part of addition result. The complete
program to add two polynomials is given in subsequent section.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct sparse
{
int row;
int col;
int val;
struct sparse *next;
} *head=NULL;
void main()
{
int r,n,c,val;
clrscr();
struct sparse *head=NULL;
printf(“\n Enter the number of non-zero terms in the sparse matrix\n”);
scanf(“%d”,&n);
while(n>0)
{
printf(“Enter the row number, column number, and value\n”);
scanf(“%d%d%d”, &r,&c,&val);
head=create(head,r,c,val);
n--;
}
printf(“The linked sparse matrix is\n”);
display(head);
getch();
}
Questions
1)Write the following algorithm for singly linked list. (Jan-19,10M)
i)Inserting ITEM as the first node in the list.
ii)Deleting the node with the given ITEM of information.
iii)To insert a node at the specified position.(Jul-08,6M)
2)Write the node structure for linked representation of polynomial. Write the function to add two polynomials
represented using linked list (Jan-19,10M)
3)Write the functions to perform the following: (Jan-19,10M)
i)Inverting a singly linked list.
ii)Concatenating the singly linked list.
iii)Finding the length of a circular list.
4) Write a note on header linked list .Explain the widely used header lists with diagrams(Jan-19,5M)(Jul-18,5M)
5) For the given sparse matrix, write the diagrammatic linked list representation.( Jan-19,5M)
2 0 0 0
4 0 0 3
0 0 0 0
8 0 0 1
0 0 6 0
6) Write a function(Jul-11,12M)
i)to find GCD of two number.
ii)to find factorial of a number.
iii)to reverse positive integer number.
iv) to search an element in the list that is unsorted and a list that is sorted.
7)Enlist the advantages and disadvantages of Doubly Linked List(DLL) over Single Linked List(SLL). (Jul-11,8M)
8)Write a function to insert and delete a node, in Doubly Linked List, with respect to given position. (Jul-11,10M)
9) What is dynamic memory allocation? Write a function to delete a node from a circular linked list. Proper error
message should be displayed. (Jul-11,10M)
10)Write the following functions for singly linked list: (Jan-18, 8M)
(i) Reverse the list (ii) Concatenate two lists. (Jan-10, 5M)
11)Write function insert_front and delete_front using doubly linked list. (Jan-18, 8M) (Dec-10,10M)
12)Write an algorithm to add two polynomials. (Jan-18,8M)
13)Define sparse matrix. Give sparse matrix representation of linked list for given matrix. (Jan-18, 8M)
A= 0 0 4 0 0
6 5 0 0 0
0 3 0 1 0
0 0 0 0 2
14)Briefly explain the structures of different types of linked lists. Write a C function to count number of elements
present in a singly linked list. (Dec-10,12M)
15)How can an ordinary queue be represented, using a singly linked list? Write C functions for linked implementation
of ordinary queue insertion and deletion. (Dec-10, 10M)(Jul-08,10M)
16) What is a linear linked list? Write the algorithm to add an element to the front of the list. (Jun-10,5M)
17) What are the advantages and disadvantages of representing a group of items as an array versus linear list?
(Jun-10,5M) (Jan-09,4M)
18) Write the following C routines for the dynamic implementation of a linked list. NODEPTR is of type pointer to a
node. (Jun-10,10M)
i) void insertafter(NODEPTR p, int x) which insert a node with information x after a node pointed to by p.
ii) void place(NODEPTR *plist, int x) which insert a node with information x at a proper position within the
linear linked list pointed to by *plist. The list is assumed to contain information in the increasing order. (Jun-10,10M)
20) Compare linear linked list and doubly linked list, with diagrams. (Jun-10,5M)(Jul-09,4M)
21) Give the C implementation of stack as circular list. (Jun-10,10M)
22)Explain how the linked list can be represented using arrays(Jan-09,4M)
23)Write a C function to merge two ordered linked list.(Jan-09,6M)
24)Write a C program to perform the operation on stack using singly linked list(Jan-09,10M)(Jan-10,10M)
25)Explain the following: i) circular list ii) doubly linked list. Using suitable diagrams.(Jan-09,6M)
26)Write a C routine to perform following operations using circular linked list.(Jan-09,10M)
i)To place the elements of a list in increasing order.
ii)To find the sum of integers and the number of elements in a list.
27)What are the advantages and disadvantages of representing a stack or queue by a linked list?(Jan-10,4M)
28)Write a note on noninteger and nonhomogeneous list.(Jan-10,6M)
29) Explain with figure circular list with a header node.(Jan-10,5M)
30) Assume that first and last are external pointers to the first and last nodes of a doubly linked list. Write an algorithm
to implement the following: (Jan-10,10M)
i)Insert a node to the list at the front end.
ii)Delete a node from the front end.
31) What is linked list? With a neat diagram show how an element is added and removed from the front end of the
list.(Jul-09,10M)
32) What is a Header node? Give example with neat diagram.(Jul-09,4M)
33)Write a C function insend(plist,x) to insert the element ‘x’ at the end of the list ‘list’. (Jul-09,6M)
34) Write a program to insert a given value into an ordered doubly linked list into its proper position. (Jul-09,6M)
35) Write a C program to perform following operation (jul-09, 10M)
i) Create a list adding nodes at front.
ii)Delete a node at given position.
36) List out any two applications of linked list and any two advantages of doubly linked list over singly linked list
(Jul-08, 4M)
37) Write a C program to perform the following operations on a doubly linked list(Jul-08, 10M)
i) To create the list by adding each node at the front.
ii) To display all the elements in reverse order.
38) Write a C program to create a linked list and interchange the element to the list at position m and n and display
contents of the list before and after interchanging the elements.(Jul-08, 10M)
39)Give the node structure to create a linked list of integers and write C function to perform the following:
(Jul-17,10M)
i) Create a three node list with data 10,20 and 30.
ii) Insert a node with the data value 15 in between the nodes having the data values 10 and 20.
iii) Delete the node whose data is 20.
iv) Display the resulting singly linked list.
40)Write a node structure for linked representation of polynomial. Explain the algorithm to add two polynomials
represented using linked list. (Jul-17,6M)
41)Write C function to perform the following: (Jul-17,6M)
i) Reversing a singly linked list.
ii) Concatenating singly linked list.
iii) Finding the length of the list.
42) List out the difference between the doubly linked list and singly linked list. Illustrate with example the following
operations on a doubly linked list: (Jul-17,10M)
i) Inserting a node the beginning
ii) Inserting at the intermediate position.
iii) Deletion of a node with a given value.
iv) Search a key element.
43) Given an ordered linked list whose first node is denoted by ‘START’ and node is represented by ‘key’ as
information and ‘link’ as link field. Write a C program to implement deleting number of nodes (consecutive) whose
‘key’ values are greater than or equal to ‘kmin ’ and less than ‘kmax’ (Jan-08,12M)
44) Write a C program to implement the insert and delete operation on queue using linked list. (Jan-15, 10M)
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner