3.8k views
0 votes
Write programs to insert, delete, and locate an element on a sorted list using a. array, b. pointer, and c. cursor implementations of lists. What is the running time of each of your programs?

User Aratata
by
7.4k points

1 Answer

3 votes

Answer:

a) with Array

class array{

int arrA[MAX],location,item,nA;

int arrB[MAX],nB;

int arr_merge[MAX+MAX],nM;

public:

array(){

location=0;item=0;nA=0;

nB=0;nM=0;

}

void init(); //initial data assignment

void traverse(); //process is display (assumed)void insert();

void del();

void search();

void sort();

void merge();

};

void array :: del(){

clrscr();

int i;

cout<<"\\\\*****Deleting Element*****\\";

if(nA < 0){

cout<<"\\Array is Empty\\Deletion Not Possible\\";

goto end;

}

cout<<"\\Enter Location of deletion : ";

cin>>location;

location--;

if(location<0 || location>=nA)

{

cout<<"\\\\Invalid Position\\";

goto end;

}

cout<<"\\Item deleted is : "<<arrA[location];

for(i=location;i<nA;i++){

arrA[i] = arrA[i+1];

}

arrA[nA-1]=0;

nA--;

end:

getch();

}

void array :: search(){

clrscr();

int i,found=-1;

cout<<"\\\\*****Searching Element*****\\";

cout<<"\\Enter Item value to be search : ";

cin>>item;

for(i=0;i<nA;i++){

if(arrA[i] == item){

found=i+1;

break;

}

}

if(found==-1)

cout<<"\\Search NOT FOUND\\";

else

cout<<"\\Search is FOUND at "<<found<<" location\\";

getch();

}

void array :: sort(){

clrscr();

int i,j,temp;

cout<<"\\\\*****Sorting Element*****\\";

for(i=0;i<nA;i++){

for(j=i;j<nA;j++){

if(arrA[i] > arrA[j]){

temp = arrA[i];

arrA[i] = arrA[j];

arrA[j] = temp;

}

}

}

cout<<"\\Data are Sorted\\";

getch();

}

// b) with pointer

#include <stdio.h>

#include <conio.h>

#include <math.h>

#include <alloc.h>

void main()

{

char *a[10],dum[10],s;

int i,k,j,n;

clrscr();

printf("enter the no of std....");

scanf("%d",&n);

printf("enter the name of students ");

for(k=0;k<n;k++)

scanf("%s",a[k]);

for(i=1;i<n;i++)

{

for(j=1;j<n-i;j++)

{if(strcmp(a[j-1],a[j])>0)

{strcpy(*dum,*a[j-1]);

strcpy(*a[j-1],*a[j]);

strcpy(*a[j],*dum);

}

} }

for(i=0;i<n;i++)

printf("%s",a[i]);

getch();

}

c) with cursor implementations of lists.

#include<stdio.h>

#include<stdlib.h>

typedef struct Node

{

int data;

struct Node *next;

}node;

void insert(node *pointer, int data)

{

/* Iterate through the list till we encounter the last node.*/

while(pointer->next!=NULL)

{

pointer = pointer -> next;

}

/* Allocate memory for the new node and put data in it.*/

pointer->next = (node *)malloc(sizeof(node));

pointer = pointer->next;

pointer->data = data;

pointer->next = NULL;

}

int find(node *pointer, int key)

{

pointer = pointer -> next; //First node is dummy node.

/* Iterate through the entire linked list and search for the key. */

while(pointer!=NULL)

{

if(pointer->data == key) //key is found.

{

return 1;

}

pointer = pointer -> next;//Search in the next node.

}

/*Key is not found */

return 0;

}

void delete(node *pointer, int data)

{

/* Go to the node for which the node next to it has to be deleted */

while(pointer->next!=NULL && (pointer->next)->data != data)

{

pointer = pointer -> next;

}

if(pointer->next==NULL)

{

printf("Element %d is not present in the list\\",data);

return;

}

/* Now pointer points to a node and the node next to it has to be removed */

node *temp;

temp = pointer -> next;

/*temp points to the node which has to be removed*/

pointer->next = temp->next;

/*We removed the node which is next to the pointer (which is also temp) */

free(temp);

/* Beacuse we deleted the node, we no longer require the memory used for it .

free() will deallocate the memory.

*/

return;

}

void print(node *pointer)

{

if(pointer==NULL)

{

return;

}

printf("%d ",pointer->data);

print(pointer->next);

}

}

Running Time:

a) 2 minute

b) 1 minutes

c) 1 & half minutes

Explanation:

a) Initialize the array and traverse it by using for loop. Sort the array with the help of a temporary variable.

b) Declare a pointer to the array and take the input from user using for loop.

c) Declare a Structure containing an integer and a pointer. Iterate through the list, allocate memory for the new node and insert the data in it.

User Saturnian
by
7.8k points

Related questions

asked Aug 15, 2022 56.5k views
Davidbrcz asked Aug 15, 2022
by Davidbrcz
8.7k points
2 answers
5 votes
56.5k views
asked Oct 22, 2022 179k views
SP Sandhu asked Oct 22, 2022
by SP Sandhu
7.6k points
1 answer
5 votes
179k views