Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

circular array implementation

This is my implementation of a circular array so far. It is supposed to store the last 5 commands entered, by entering the 6th command in the place of the 5th and discarding the 1st. What I have managed to do so far is, to able to store the 5 commands and print them out. On the 6th command, I noticed that it goes in the 2nd position (k=1) of the historyArray, but when debugging, k was equal to 0 which would at least push the last command at the top. If you can put me in the right track again, I would appreciate it. Here is part of the code.

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

int main (int argc, char *argv[])
{
    int i=0; 
    int j=0; 
    int k=0;
    int tempIndex = 0;
    int elementCounter = 0;

    char inputString[100];
    char *result=NULL;
    char delims[] = " ";
    char historyArray[5][20] = {0};
    char tokenArray[20][20] ;
    char hCommand[1][20];

    do
    {
         j = 0;

         printf("hshell>");
         gets(inputString);

         //skip writing "history" in historyArray
         if (strcmp(inputString,"history")!= 0)
         {
             strcpy (historyArray[k], inputString);
         }

         k = (k+1) % 5;
         if (elementCounter <= 5)
             elementCounter++;

         // Break the string into parts
         result = strtok(inputString, delims);

         while (result!=NULL)
         {
             strcpy(tokenArray[j], result);
             j++;
             result= strtok(NULL, delims);                  
         }

         if (strcmp(tokenArray[0], "exit") == 0)
             return 0;

         if (strcmp(tokenArray[0], "history") ==  0)
         {
             if (j>1)
             {
                 tempIndex = atoi(tokenArray[j]);
                 puts(tempIndex);
             }
             else
             {
                 for (i=0; i<elementCounter-1;i++)
                     printf("%i. %s\n", i+1, historyArray[i]);
             }
         }
         else
         {
             printf("Command not found\n");
         }
    } while (1);
}

After suggestions (still incomplete):

         j = 0;
         //elementCounter = 0;
         printf("327>");
         gets(inputString);

         strcpy (historyArray[k], inputString);
         k = (k+1) % 5;

        if (elementCounter <= 5)
         {         
          elementCounter++;                
         }
like image 651
serge Avatar asked Feb 25 '13 20:02

serge


People also ask

What is circular array implementation?

An array is called circular if we consider the first element as next of the last element. Circular arrays are used to implement queue (Refer to this and this). An example problem : Suppose n people are sitting at a circular table with names A, B, C, D, …

What is implementation circular queue?

A Circular Queue is a special version of queue where the last element of the queue is connected to the first element of the queue forming a circle. The operations are performed based on FIFO (First In First Out) principle. It is also called 'Ring Buffer'.

What is the need for circular array to implement queue?

By treating the array as a circular buffer, pointing the head of the queue to the next item when one is removed becomes as simple as a single assignment, which is obviously much more performant.

Can we implement circular queue using array?

Implementation of Circular Queue using an Arrayint rear = -1; After this, you will begin with the implementation of circular queue operations. Later, you will check if the queue is full. If it's full, you will return an overflow error (i.e., no empty space for insertion).


2 Answers

The bug you describe is occurring because of the lines:

k = (k + 1) % 5;
elementCounter++;

What I see happening:

k initial | calculation | k result  | elementCounter
0           (0 + 1) % 5   1 % 5 = 1   1
1           (1 + 1) % 5   2 % 5 = 2   2
...
4           (4 + 1) % 5   5 % 5 = 0   5
0           (0 + 1) % 5   1 % 5 = 1   5

k is behaving as it's supposed to, as far as I can see. However, when elementCounter is 5, k = 1.

EDIT: The problem that I see is that the latest command is being added at position k, not position 0, which based on your implementation is the most recent command entered (based on the various if clauses, like the one that processes the "exit" and "history" commands). Try this set of commands, using your current algorithm. I expect that the contents of the [Command List] column are what you'll see...

Command # | Command Text | [Command List]
0           (null)         []
1           Login          [Login]
2           History        [Login,History]
3           Skynet         [Login,History,Skynet]
4           ps -al         [Login,History,Skynet,ps -al]
5           Skynet         [Login,History,Skynet,ps -al,Skynet]
6           Exit           [Exit,History,Skynet,ps -al,Skynet]

What you would want to do, is copy elements 0-3, and move them to elements 1-4. Then, insert the new command at position 0 in the historyArray. Thus, your history should look like this after adjusting your algorithm appropriately:

Command # | Command Text | [Command List]
0           (null)         []
1           Login          [Login]
2           History        [History,Login]
3           Skynet         [Skynet,History,Login]
4           ps -al         [ps -al,Skynet,History,Login]
5           Skynet         [Skynet,ps -al,Skynet,History,Login]
6           Exit           [Exit,Skynet,ps -al,Skynet,History]
like image 183
Andrew Gray Avatar answered Sep 23 '22 12:09

Andrew Gray


This is I tried and it seems to be working as expected:

             j = 0;
             //elementCounter = 0;
             printf("hshell>");
             gets(inputString);

             strcpy (historyArray[k], inputString);
             k = (k+1) % 5;

            if (elementCounter <= 5)
             {         
              elementCounter++;                
             }

             if (elementCounter ==6)
             {
                k = 5;
                for (i=0; i<5; i++)
                {
                    strcpy(historyArray[i], historyArray[i+1]);
                }
                 strcpy (historyArray[4], inputString);                 
             }

This, basically, checks if the elementCounter becomes 6 (meaning that a 6th command has been entered). If so, it sets k=5 so that the command will be entered at the last position of the array, and then shift the first 4 values up one position leaving index 4 blank. The final step fills the position with the command. It is not the most elegant piece of code but seems to do the trick.

like image 41
serge Avatar answered Sep 26 '22 12:09

serge