I came by this problem where we are asked to store a string and a no. associated with it and we are supposed to remove the minimum no.(and its string) from the list and also remove the no.s(and strings) stored after it.The input is a stream of no.,string pair and an input of -1 means we need to remove the samllest from the list and the pairs above it.Output should be the count of items above the minimum numbered item.
e.g. 2 abcd
1 aabb
3 dbbb
-1
o/p 1 (since minimum is 1 aabb and there is just one item after it i.e. 3 dbbb;our list now contains just 2 abcd).
another -1 would produce as o/p 0.
I have tried this using linked list but it seems to take more time than expected.I need a better data structure or algorithm for the same.
Here’s my code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct data
{
int no,pos;
char *st;
struct data *next;
}data;
void insert(data *start,int n,char *s);
void minimum(data *start);
int total=0,min=100001,posn=0;//total=total no of nodes,rem=nodes after minimum
data *temp;
int main()
{
int N,n;
data *start;
start=(data*)malloc(sizeof(data));
start->pos=0;
start->no=100002;
start->next=NULL;
char c,s[16];
scanf("%d",&N);
while(N)
{
scanf("%d",&n);
if(n!=-1)
{
scanf("%c",&c);
scanf("%s",s);
total++;
posn++;
insert(start,n,s);
}
else
{
printf("%d %s\n",total-(temp->next->pos),temp->next->st);
posn=temp->pos;
total=temp->pos;
temp->next=NULL;
minimum(start);
}
N--;
}
}
void insert(data *start,int n,char *s)
{
while(start->next!=NULL)
start=start->next;
if(n<=min)
{
temp=start;
min=n;
}
start->next=(data*)malloc(sizeof(data));
start=start->next;
start->no=n;
start->st=(char*)malloc(sizeof(char)*(strlen(s)));
strcpy(start->st,s);
start->pos=posn;
start->next=NULL;
return;
}
void minimum(data *start)
{
min=100001;
while(start->next!=NULL)
{
if(start->next->no<=min)
{
min=start->next->no;
temp=start;
start=start->next;
}
}
return;
}
Any help would be appreciated.
Well here are some issues with your code:
start=start->next;should be done in every iteration and not only when a new min is found.You are always missing the head of the list, iterate(It seemswhile(start != NULL)(and checkstart->noinstead of the next node).startis a dummy variable, if it is indeed the case – you are only missing it – which is perfectly fine).You are missing a return type for the(Note it is a bad practice to store the answer in a global variable, a better approach is to return it from the function)minimum()function, it shouldn’t bevoid. If your list containsints, the return type should beint, and when you exhaust the loop you shouldreturn min;. (If you are interested in returning the min string attached, store an extra variablechar* minElement, modify it when you modifymin, and return it (minelement) when the loop is exhausted.INT_MAXrather an arbitrary number.Regarding the optimization issue:
If you are interested only in finding the minimum element, a heap is a data structure designed exactly for it! It is simple and very efficeint Data Structure for retrieving minimum.
Also note, if your Data structure does not support deletions, you can cache min in each insertion, something along the lines of this pseudo code:
add to insertion function:
and min is simple as retrieving the cached
min.