Respuesta :
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<<"\n\n*****Deleting Element*****\n";
if(nA < 0){
cout<<"\nArray is Empty\nDeletion Not Possible\n";
goto end;
}
cout<<"\nEnter Location of deletion : ";
cin>>location;
location--;
if(location<0 || location>=nA)
{
cout<<"\n\nInvalid Position\n";
goto end;
}
cout<<"\nItem 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<<"\n\n*****Searching Element*****\n";
cout<<"\nEnter 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<<"\nSearch NOT FOUND\n";
else
cout<<"\nSearch is FOUND at "<<found<<" location\n";
getch();
}
void array :: sort(){
clrscr();
int i,j,temp;
cout<<"\n\n*****Sorting Element*****\n";
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<<"\nData are Sorted\n";
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\n",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.