Diese Präsentation wurde erfolgreich gemeldet.

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

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Nächste SlideShare
Data structures stacks
×

1 von 3 Anzeige

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

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

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; }