0

I have created a program to add 2 types of items in to a system. I have created 2 structures for the 2 different items. Currently i have created a method to add items to the system and i store each item in an array. However i encountered a problem when i was gonna implement the delete feature, the problem is if i have a record at array memory index 2, if i delete it there will be an unused space, between memory index 1 and 3. How can i overcome this ? In java , there is arraylist which dynamically allocates space. In C i know that there is dynamic memory allocation , but how can i implement it work with the delete feature ?

Here is what i have done so far :-

#include <stdio.h>
#include <string.h>

struct routers
   {
    int Device_No;
  char Device_Name[30];
  int No_of_items;
  int Price;
  char Description[30];


  };

    /**declared an of struct routers to store the structure objects
   **/

   struct routers routerlist[5];

struct controllers
   {
   int Device_No;
   char Device_Name;
   int No_of_items;
  int Price;
  char Description[30];
  };

   void AddNewItem();


   int main()
   {
       AddNewItem();

      return 0;
  }

   void AddNewItem(){
       int item;
      int choice=0;
      int arraysize=0;


  do{

   printf("Press 1 to add a Router \nPress 2 to add a controller \n");
   scanf("%d",&item);

    printf("%d",item);

    if(item==1){
       printf("\nEnter Device No:\n");
       scanf("%d",&routerlist[arraysize].Device_No);

    printf("Enter Device Name\n");
    fflush(stdin);  //flush the buffer
    gets(routerlist[arraysize].Device_Name);

     printf("Enter Number of Items\n");
      scanf("%d",&routerlist[arraysize].No_of_items);

      printf("Enter price\n");
      scanf("%d",&routerlist[arraysize].Price);

   printf("Enter description\n");
   fflush(stdin);
   gets(routerlist[arraysize].Description);


     }
     arraysize++;
printf("Do you want to add another item? \nPress 1 to add \nPress 2 to Cancel\n");
   scanf("%d",&choice);

    }while(choice==1);


   }

Thank you for your time.

3
  • fflush(stdin) is undefined behavior !! Also don't use gets its a kingkong of problems. Commented Aug 8, 2013 at 5:41
  • oh well i used it because it skips the string taking part and goes to the next input. Commented Aug 8, 2013 at 5:43
  • No , fflush(stdin) might work for some systems but its undefined ...... use getchar(); instead after the input it will eatup the newline which remains in your buffer. Commented Aug 8, 2013 at 5:45

3 Answers 3

1

Depending on your time complexity requirements, there are basically two approaches:

  1. Use a list. A list is a data structure where every item knows where the next item is stored. This is usually implemented by the data structure holding a pointer to two objects of its own kind (the previous one and the next one). That way, when an item is deleted, the pointers of the next and previous items can be adjusted so that the gap is closed.

    This means, deleting an element is very fast, but accessing an element by position requires searching for the element from the beginning, which is very slow.

  2. Use an array. An array is a data structure where items are stored consecutively. When an item is deleted, the gap is filled by shifting the following elements.

    This means, accessing an element by position is very fast, because only arithmetic operations are involved, but deleting an element is very slow, because a possibly large number of elments have to be copied.

Sign up to request clarification or add additional context in comments.

2 Comments

I guess ill go with the first option , because second option seems expensive. Thank you for answering.
Which one is better really depends on how you use the data structure. If the order of the elements does not matter, you can save a lot of unecessary copying in the array approach by filling the gap with the last element of the array.
1

Deleting a gap means moving what's after it, so if you delete index 2 the item at index 3 is moved to index 2, then item at index 4 is moved to index 3, and so on.

In C you can use the memmove function for this, or write a loop.

Comments

1

To implement the similar behavior like in arrayList you need to dynamically allocate memory each time the array is full. Then copy all the members of the current list to that memory

In case of deletion you will have to move the elements one space to the deleted element location. This is similar to how vector are implemented in c++. Please refer this answer of mine Vector implementation, this implementation is in c++. The concept you have to look into this code is how a memory is allocated dynamically when required.

So your approach should be as follows:

  1. Initially allocate some memory spaces
  2. Use them until you reach the final allocated block
  3. In case more memory is required allocate new space . Please use realloc here you will find the advantage of it
  4. free the previously allocated memory and let the previous pointer point to this new location.

Similarly when deleting the elements just move all the elements one space from end towards that element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.