Insert string into list position 
Author Message
 Insert string into list position

I have modified the code below somewhat to accept and int position & a char
data[MAX]. I am trying to input the string & input the position of users
choice. Then insert or delete the string into or out of that position. On
deleting, the string must match the position i.e: compare the string with
position if no match print current list & statement : string is not in that
position. After each operation display the list of strings & their position
in the list.

I'm more concerned with functionality than nitty gritty.

Thankyou

/* Operating and maintaining a list */
#include <stdio.h>
#include <stdlib.h>

#define MAX 15

struct listNode {
  char data[MAX];
  int position;
  struct listNode *nextPtr;

Quote:
};

typedef struct listNode LISTNODE;
typedef LISTNODE *LISTNODEPTR;

void insert (LISTNODEPTR *, int, const char *);
char delete (LISTNODEPTR *, int, const char *);
int isEmpty (LISTNODEPTR);
char *getstr(char *str, size_t size);
void printList (LISTNODEPTR);
void instructions (void);

main () {
  LISTNODEPTR startPtr = NULL;
  int choice, posn;
  char item[MAX];

  instructions();
  printf("? ");
  scanf("%d",&choice);

  while (choice != 3) {
    switch (choice) {
      case 1:
 printf("Enter a string: ");
 fflush(stdout);
 getstr(item, sizeof item);
 printf("Enter the position: ");
 fflush(stdout);
 scanf("\n%d", &posn);
 insert(&startPtr, posn, item);
 printList(startPtr);
 break;

      case 2:
 if (!isEmpty(startPtr)) {
     printf("Enter string to be deleted: ");
        fflush(stdout);
        getstr(item, sizeof item);
  printf("Enter Position of character to be deleted: ");
  scanf("\n%d",&posn);

     if (delete(&startPtr, posn, item)) {
  printf("%c in position %d deleted.\n", item, posn);
  printList(startPtr);
     } else
       printf("%c in position %d not found.\n\n",item, posn);
 } else
   printf("List is empty.\n\n");

 break;

      default:
 printf("Invalid choice.\n\n");
 instructions();
 break;
    }

    printf("? ");
    scanf("%d", &choice);
  }

  printf("End of run.\n");
  return 0;

Quote:
}

/* Print the instructions. */
void instructions (void) {
  printf("Enter your choice:\n"
  "   1 to insert an string into the list.\n"
  "   2 to delete an string from the list.\n"
  "   3 to end.\n");

Quote:
}

/* Insert a new value into the list in sorted order. */
void insert (LISTNODEPTR *sPtr,int index, const char *value)
{
  LISTNODEPTR newPtr, previousPtr, currentPtr;

  newPtr = malloc(sizeof(LISTNODE));

  if (newPtr != NULL) {
      strncpy(newPtr->data, value,MAX);
   newPtr->position = index;
      newPtr->nextPtr = NULL;

      previousPtr = NULL;
      currentPtr = *sPtr;

      while (currentPtr != NULL && index > currentPtr->position) {
 previousPtr = currentPtr;
 currentPtr = currentPtr->nextPtr;
      }

      if (previousPtr == NULL) {
 newPtr->nextPtr = *sPtr;
 *sPtr = newPtr;
      } else {
 previousPtr->nextPtr = newPtr;
 newPtr->nextPtr = currentPtr;
      }
  } else
    printf("%c not insert. No memory available.\n", value);

Quote:
}

/* Delete a list element */
char delete (LISTNODEPTR *sPtr,int index, const char *value)
{
  LISTNODEPTR previousPtr, currentPtr, tempPtr;

  if (value == (*sPtr)->data && (*sPtr)->position) {
    tempPtr = *sPtr;
    *sPtr = (*sPtr)->nextPtr;
    free(tempPtr);
    return value;
  } else {
    previousPtr = *sPtr;
    currentPtr = (*sPtr)->nextPtr;

    while (currentPtr != NULL && currentPtr->data != value
   && currentPtr->position != index) {
      previousPtr = currentPtr;
      currentPtr = currentPtr->nextPtr;
    }

    if (currentPtr != NULL) {
      tempPtr = currentPtr;
      previousPtr->nextPtr = currentPtr->nextPtr;
      free(tempPtr);
      return value;
    }
  }
  return value;

Quote:
}

/* Return 1 is the list is empty. 0 otherwise. */
int isEmpty (LISTNODEPTR sPtr) {
  return sPtr == NULL;
Quote:
}

/*********Added this**********/

char *getstr(char *str, size_t size)
{
    char *s;
    int ch;

    fgets(str,size,stdin);
    if((s = strrchr(str,'\n')) != NULL) *s = '\0';
    else while((ch = getchar()) != '\n');
    return str;

Quote:
}

/********To here************/

/* Print the list */
void printList (LISTNODEPTR currentPtr) {
  if (currentPtr == NULL)
    printf("List is empty.\n\n");
  else {
    printf("The list is: \n");

    while (currentPtr != NULL) {
      printf("%c --> ", currentPtr->data);
      currentPtr = currentPtr->nextPtr;
    }
    printf("NULL\n\n");
  }

Quote:
}



Fri, 21 Oct 2005 10:58:32 GMT  
 Insert string into list position

Quote:

>I have modified the code below somewhat to accept and int position & a char
>data[MAX]. I am trying to input the string & input the position of users
>choice. Then insert or delete the string into or out of that position. On
>deleting, the string must match the position i.e: compare the string with
>position if no match print current list & statement : string is not in that
>position. After each operation display the list of strings & their position
>in the list.

>I'm more concerned with functionality than nitty gritty.

>Thankyou

...

>main () {
>  LISTNODEPTR startPtr = NULL;
>  int choice, posn;
>  char item[MAX];

>  instructions();
>  printf("? ");
>  scanf("%d",&choice);

>  while (choice != 3) {
>    switch (choice) {
>      case 1:
> printf("Enter a string: ");
> fflush(stdout);
> getstr(item, sizeof item);

Before the getstr() call there may still be characters in the input buffer,
that is why your fgets() call in getstr() fails. For example, if a user
types 1<enter> as choice input only the 1 is taken from the input buffer
by scanf().
A better solution than scanning stdin directly with scanf() would be:

#define MAX_INPUT       256
char input[MAX_INPUT];
fgets(input, MAX_INPUT, stdin);
choice = sscanf(input, "%d", choice);

Quote:
>/*********Added this**********/

>char *getstr(char *str, size_t size)
>{
>    char *s;
>    int ch;

>    fgets(str,size,stdin);
>    if((s = strrchr(str,'\n')) != NULL) *s = '\0';
>    else while((ch = getchar()) != '\n');
>    return str;
>}
>/********To here************/

Regards,
Rob van der Leek
--
Rob van der Leek                    | rob(at)ricardis(dot)tudelft(dot)nl

2614 JE Delft, The Netherlands      
+31 (0)6 155 244 60


Fri, 21 Oct 2005 15:34:11 GMT  
 Insert string into list position

Quote:

> I have modified the code below somewhat to accept and int position & a char
> data[MAX]. I am trying to input the string & input the position of users
> choice. Then insert or delete the string into or out of that position. On
> deleting, the string must match the position i.e: compare the string with
> position if no match print current list & statement : string is not in that
> position. After each operation display the list of strings & their position
> in the list.

> I'm more concerned with functionality than nitty gritty.

> /* Operating and maintaining a list */
> #include <stdio.h>
> #include <stdlib.h>

You need to include string.h.

You are having a problem using scanf to get the int input. Although
you can fix the scanf where it will work, a more robust approach is
to write a function that uses function strtol.

add includes
#include <errno.h>
#include <limits.h>

int getint(void)
{
    char *s,value[36];
    long ln;

    for( ; ; )
    {
       getstr(value,sizeof value);
       errno = 0;
       ln = strtol(value,&s,10);
       if(errno != ERANGE && s != value && *s == '\0' &&
                         ln >= INT_MIN && ln <= INT_MAX) break;
       puts("Invalid number. Try Again.");
    }
    return (int)ln;

Quote:
}

> #define MAX 15

> struct listNode {
>   char data[MAX];
>   int position;
>   struct listNode *nextPtr;
> };

> typedef struct listNode LISTNODE;
> typedef LISTNODE *LISTNODEPTR;

> void insert (LISTNODEPTR *, int, const char *);
> char delete (LISTNODEPTR *, int, const char *);

char *delete(LISTNODEPTR *, int, const char *);

Quote:
> int isEmpty (LISTNODEPTR);
> char *getstr(char *str, size_t size);
> void printList (LISTNODEPTR);
> void instructions (void);

> main () {
>   LISTNODEPTR startPtr = NULL;
>   int choice, posn;
>   char item[MAX];

>   instructions();
>   printf("? ");
>   scanf("%d",&choice);

    choice = getint();

Quote:
>   while (choice != 3) {
>     switch (choice) {
>       case 1:
>  printf("Enter a string: ");
>  fflush(stdout);
>  getstr(item, sizeof item);
>  printf("Enter the position: ");
>  fflush(stdout);
>  scanf("\n%d", &posn);

posn = getint();

Quote:
>  insert(&startPtr, posn, item);
>  printList(startPtr);
>  break;

>       case 2:
>  if (!isEmpty(startPtr)) {
>      printf("Enter string to be deleted: ");
>         fflush(stdout);
>         getstr(item, sizeof item);
>   printf("Enter Position of character to be deleted: ");
>   scanf("\n%d",&posn);

posn = getint();

Quote:

>      if (delete(&startPtr, posn, item)) {
>   printf("%c in position %d deleted.\n", item, posn);

item is a string:
printf("\"%s\" in position %d deleted.\n",item,posn);

Quote:
>   printList(startPtr);
>      } else
>        printf("%c in position %d not found.\n\n",item, posn);
>  } else
>    printf("List is empty.\n\n");

>  break;

>       default:
>  printf("Invalid choice.\n\n");
>  instructions();
>  break;
>     }

>     printf("? ");
>     scanf("%d", &choice);

choice = getint();

- Show quoted text -

Quote:
>   }

>   printf("End of run.\n");
>   return 0;
> }

> /* Print the instructions. */
> void instructions (void) {
>   printf("Enter your choice:\n"
>   "   1 to insert an string into the list.\n"
>   "   2 to delete an string from the list.\n"
>   "   3 to end.\n");
> }

> /* Insert a new value into the list in sorted order. */
> void insert (LISTNODEPTR *sPtr,int index, const char *value)
> {
>   LISTNODEPTR newPtr, previousPtr, currentPtr;

>   newPtr = malloc(sizeof(LISTNODE));

>   if (newPtr != NULL) {
>       strncpy(newPtr->data, value,MAX);

For safety add the following statement:
newPtr->data[MAX-1] = '\0';

- Show quoted text -

Quote:
>    newPtr->position = index;
>       newPtr->nextPtr = NULL;

>       previousPtr = NULL;
>       currentPtr = *sPtr;

>       while (currentPtr != NULL && index > currentPtr->position) {
>  previousPtr = currentPtr;
>  currentPtr = currentPtr->nextPtr;
>       }

>       if (previousPtr == NULL) {
>  newPtr->nextPtr = *sPtr;
>  *sPtr = newPtr;
>       } else {
>  previousPtr->nextPtr = newPtr;
>  newPtr->nextPtr = currentPtr;
>       }
>   } else
>     printf("%c not insert. No memory available.\n", value);

value is not a char. It represents a string.
printf("\"%s\" not inserted. No memory available.\n",value);

Quote:
> }

> /* Delete a list element */
> char delete (LISTNODEPTR *sPtr,int index, const char *value)

char *delete(LISTNODEPTR *sPtr,int index, const char *value)

Quote:
> {
>   LISTNODEPTR previousPtr, currentPtr, tempPtr;

>   if (value == (*sPtr)->data && (*sPtr)->position) {

if(strcmp(value,(*sPTR)->data == 0 && index == (*sPtr)->position) {

Quote:
>     tempPtr = *sPtr;
>     *sPtr = (*sPtr)->nextPtr;
>     free(tempPtr);
>     return value;
>   } else {
>     previousPtr = *sPtr;
>     currentPtr = (*sPtr)->nextPtr;

>     while (currentPtr != NULL && currentPtr->data != value
>    && currentPtr->position != index) {

while(currentPtr != NULL && strcmp(currentPtr->data,value) != 0 &&
         currentPtr->position != index) {

- Show quoted text -

Quote:
>       previousPtr = currentPtr;
>       currentPtr = currentPtr->nextPtr;
>     }

>     if (currentPtr != NULL) {
>       tempPtr = currentPtr;
>       previousPtr->nextPtr = currentPtr->nextPtr;
>       free(tempPtr);
>       return value;
>     }
>   }
>   return value;
> }

> /* Return 1 is the list is empty. 0 otherwise. */
> int isEmpty (LISTNODEPTR sPtr) {
>   return sPtr == NULL;

return NULL;

- Show quoted text -

Quote:
> }
> /*********Added this**********/

> char *getstr(char *str, size_t size)
> {
>     char *s;
>     int ch;

>     fgets(str,size,stdin);
>     if((s = strrchr(str,'\n')) != NULL) *s = '\0';
>     else while((ch = getchar()) != '\n');
>     return str;
> }
> /********To here************/

> /* Print the list */
> void printList (LISTNODEPTR currentPtr) {
>   if (currentPtr == NULL)
>     printf("List is empty.\n\n");
>   else {
>     printf("The list is: \n");

>     while (currentPtr != NULL) {
>       printf("%c --> ", currentPtr->data);

printf("\"%s\" --> %d\n", currentPtr->data,currentPtr->position);

Quote:
>       currentPtr = currentPtr->nextPtr;
>     }
>     printf("NULL\n\n");
>   }
> }

--
Al Bowers
Tampa, FL. USA

http://www.geocities.com/abowers822
comp.lang.c


Fri, 21 Oct 2005 19:35:17 GMT  
 Insert string into list position

Quote:

>I have modified the code below somewhat to accept and int position & a char
>data[MAX]. I am trying to input the string & input the position of users
>choice. Then insert or delete the string into or out of that position. On
>deleting, the string must match the position i.e: compare the string with
>position if no match print current list & statement : string is not in that
>position. After each operation display the list of strings & their position
>in the list.

>I'm more concerned with functionality than nitty gritty.

>Thankyou

snip ~180 lines of uncompilable and haphazardly indented code

And you changed the subject making it difficult to follow the thread
from the earlier messages.

If you want help, you should at least show that you tried to solve the
obvious problems.

<<Remove the del for email>>



Sat, 22 Oct 2005 13:17:27 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Insert into a file at seek position?

2. I can only insert an item in position 0 in CListView

3. Insert into a file at seek position?

4. Insert into CRichEditCtrl at Current cursor position

5. Problem with ordered insert into linear list -part 2-

6. Problem of insert and sort of Linked list

7. Insert Function for an ordered linked list

8. Insert in a linked list

9. List of structs and insert

10. sorted list of emails -- Insert function

11. Inserting date item to List Control

12. Efficient insert into List Control

 

 
Powered by phpBB® Forum Software