Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

1-Implement an array-based sorted list with the following functions- a.docx

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Nächste SlideShare
Data structures   stacks
Data structures stacks
Wird geladen in …3
×

Hier ansehen

1 von 3 Anzeige

1-Implement an array-based sorted list with the following functions- a.docx

Herunterladen, um offline zu lesen

1.Implement an array-based sorted list with the following functions:
a.bool insert(T item)
b.bool remove(T item)
c.int find(T item)
find(itm) will return the position of itm if it is present in the list or the negative of its rightful position if it\'s not.
your implementation has to be as fast as possible.
Solution
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int val;
node *next;
}node;
node *root;
bool insert(node item)
{
node*temp=root,*hat;
while(1)
{
if(temp->next=NULL)
{
temp->next = &item;
node->next=NULL;
break;
}
if(temp->next->num>=item.num)
{
hat = temp->next;
temp->next = &item;
node->next=hat;
break;
}
temp = temp->next;
}
return true;
}
bool remove(T item)
{
node*temp=root,*hat;
while(1)
{
if(temp->next=NULL)
{
return false;
break;
}
if(temp->next->num==item.num)
{
hat = temp->next;
temp->next = hat->next;
free(hat);
return true;
break;
}
temp = temp->next;
}
}
int find(T item)
{
int pos=0;
node*temp=root,*hat;
while(1)
{
if(temp->next=NULL)
{
return -1;
}
if(temp->next->num==item.num)
{
return pos;
}
temp = temp->next;
pos++;
}
}
int main()
{
root = (node *)malloc(sizeof(node));
root->next=NULL;
return 0;
}
.

1.Implement an array-based sorted list with the following functions:
a.bool insert(T item)
b.bool remove(T item)
c.int find(T item)
find(itm) will return the position of itm if it is present in the list or the negative of its rightful position if it\'s not.
your implementation has to be as fast as possible.
Solution
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int val;
node *next;
}node;
node *root;
bool insert(node item)
{
node*temp=root,*hat;
while(1)
{
if(temp->next=NULL)
{
temp->next = &item;
node->next=NULL;
break;
}
if(temp->next->num>=item.num)
{
hat = temp->next;
temp->next = &item;
node->next=hat;
break;
}
temp = temp->next;
}
return true;
}
bool remove(T item)
{
node*temp=root,*hat;
while(1)
{
if(temp->next=NULL)
{
return false;
break;
}
if(temp->next->num==item.num)
{
hat = temp->next;
temp->next = hat->next;
free(hat);
return true;
break;
}
temp = temp->next;
}
}
int find(T item)
{
int pos=0;
node*temp=root,*hat;
while(1)
{
if(temp->next=NULL)
{
return -1;
}
if(temp->next->num==item.num)
{
return pos;
}
temp = temp->next;
pos++;
}
}
int main()
{
root = (node *)malloc(sizeof(node));
root->next=NULL;
return 0;
}
.

Anzeige
Anzeige

Weitere Verwandte Inhalte

Ähnlich wie 1-Implement an array-based sorted list with the following functions- a.docx (20)

Weitere von todd991 (20)

Anzeige

1-Implement an array-based sorted list with the following functions- a.docx

  1. 1. 1.Implement an array-based sorted list with the following functions: a.bool insert(T item) b.bool remove(T item) c.int find(T item) find(itm) will return the position of itm if it is present in the list or the negative of its rightful position if it's not. your implementation has to be as fast as possible. Solution #include<stdio.h> #include<stdlib.h> typedef struct node { int val; node *next; }node; node *root; bool insert(node item) { node*temp=root,*hat; while(1) { if(temp->next=NULL) { temp->next = &item; node->next=NULL; break; } if(temp->next->num>=item.num) {
  2. 2. hat = temp->next; temp->next = &item; node->next=hat; break; } temp = temp->next; } return true; } bool remove(T item) { node*temp=root,*hat; while(1) { if(temp->next=NULL) { return false; break; } if(temp->next->num==item.num) { hat = temp->next; temp->next = hat->next; free(hat); return true; break; } temp = temp->next; } } int find(T item) { int pos=0; node*temp=root,*hat; while(1) { if(temp->next=NULL) { return -1; } if(temp->next->num==item.num) { return pos; } temp = temp->next; pos++;
  3. 3. } } int main() { root = (node *)malloc(sizeof(node)); root->next=NULL; return 0; }

×