Home C C++ Java Python Perl PHP SQL JavaScript Linux Online Test Forum

Home » Forum » x-Forum » Question

I dont know how to code in delete part, it is hard for me

#include<stdio.h>
#define MAX 50

void insert(int*,int,int*);
void display(int*, int);
int search(int *a, int, int );
int update(int *a,int,int,int);
int delete(int *,int,int*);


int main(){
int A[MAX],lastIndex=-1, num, val,i;
char ch;

    start:
    system("CLS");
    printf("\t\t---==== Command List  ====---\n");
    printf("[I] Insert an item\t\t[j] insert group of items\n[P] Display items\t\t[U] Update an item\n[S] Search an item\t\t[D] Delete an item\n");
    printf("Press the respective key of choice , otherwise:");
    ch=getch();
    printf("%c\n",ch);
    switch(ch)

    {
    case 'I': case 'i':
      printf("Input item value: ");
      scanf("%d", &val);
      insert(A,val,&lastIndex);
      goto start;
      break;

    case 'J': case 'j':
      printf("How many items do you wish to input? ");
      scanf("%d", &num);
      for(i=0;i<num; i++){
        printf("Input #%d: ",i+1);
        scanf("%d", &val);
        insert(A,val,&lastIndex);
      }
      goto start;
      break;

    case 'U': case 'u':
      printf("\nItem value to be replaced: ");
      scanf("%d", &val);
      printf("New item value: ");
      scanf("%d", &num);
      if(update(A,val,num,lastIndex))
        printf("Successfully updated..\n");
      else printf("No update..");
      getch();
      goto start;
      break;

    case 'P': case 'p':
      printf("\n\n");
      display(A,lastIndex);
      getch();
      goto start;
      break;

    case 'D': case 'd': /// case for CHOICE Delete
      printf("Enter Number You want to delete:");
      scanf("%d",&val);
      goto start;
      break;

    case 'S': case 's':
      printf("\nSearch a value: ");
      scanf("%d", &val);
      printf("%sFound",(search(A,val,lastIndex)>-1)?"":"Not ");
      getch();
      goto start;
      break;
    default:
      goto end;
    }
  end:
    return 0;
}

int delete(int *a, int deleteKey, int *s){

  return 0;
}

int update(int *a, int searchKey, int newVal, int s){
    int loc=search(a,searchKey,s);
   if(loc>-1)
    {
        a[loc]=newVal;
        return 1;
    }
    return 0;
}

int search(int *a, int v, int s){
int i;
for(i=0; i<=s;i++)
if(v==a[i])
return i;
return -1;
}




void display(int *a, int s){
    int i;
    for(i=0;i<=s;i++)
    printf("%d",a[i]);

}

void insert(int *a, int v, int *s){
   if(*s<MAX){
       *s=*s+1;
       a[*s]=v;
   }
   else printf("Array is full.");
}

Posted by Ravelo 2018-11-01 00:53:40

No Ans Posted

Your Answer


Ask New Question