• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

Linked List Question

Hi, I've program this and i couldnt seem to figure out what is wrong with it... I documented the code, and I'm so sure that they all should work fine...I think its in the insert function but still couldnt figure out the error...

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

#define NULL 0

struct list_element {
      char item[40];
      struct list_element *next;
};
typedef struct list_element node;

int menu(void);
void create (node *pt);
node *insert (node *pt);
node *remove (node *pt);
void display (node *pt);

void main ()
{
      node *start;
      int choice;

      do
      {
            choice = menu();
            switch (choice)
            {
            case 1: //create the linked list
                  start = (node *) malloc(sizeof(node));
                  create(start);
                  printf("\n");
                  display(start);
                  continue;
            case 2: //add one component
                  start = insert(start);
                  printf("\n");
                  display(start);
                  continue;
            case 3: //delete one component
                  start = remove (start);
                  printf("\n");
                  display (start);
                  continue;
            default: //terminate computation
                  printf("End of computation\n");
            }
      }while (choice!=4);
}

int menu(void) //generate the main menu
{
      int choice;
      do
      {
            printf("\n Main Menu:\n");
            printf(" 1 - CREATE the linked list\n");
            printf(" 2 - ADD a component\n");
            printf(" 3 - DELETE a component\n");
            printf(" 4 - END\n");
            printf("Please enter your choice (1, 2, 3 or 4) -> ");
            scanf("%d", &choice);
            if (choice <1 || choice > 4)
            {
                  printf("\nERROR - Please try again\n");
            }
      }while (choice <1 || choice > 4);
      printf("\n");
      return (choice);
}
void create (node *record) //create a linked list
//argument points to the current node
{
      printf("Data item (type \'END\' when finished: ");
      scanf("%[^\n]", record->item);
      
      if (strcmp(record->item, "END")==0)
      {
            record ->next = NULL;
      }
      else
      {
            //allocate space for next node
            record ->next = (node *) malloc (sizeof(node));
            //create the next node
            create(record->next);
      }
      return;
}
void display (node *record) // display the linked list
// argument points to the current node
{
      if (record ->next !=NULL)
      {
            printf("%s\n", record->item); // display this data item
            display(record->next); // get the next data item
      }
      return;
}
node *insert(node *first) // add one component to the linked list
                                      // return a pointer to beginning of the modified list
//argument points to the first node
{
      node *locate (node*, char[]);
      node *newrecord;
      node *tag;
      char newitem[40];
      char target[40];

      printf("New data item: ");
      scanf(" %[^\n]", newitem);
      printf("Place before (type \'END\' )if last: ");
      scanf("%[^\n]", target);

      if (strcmp(first ->item, target) == 0)
            //new node if first in list
      {
            //allocate space for the new node
            newrecord = (node *) malloc(sizeof(node));

            //assign the new data item to newrecord -> next
            strcpy(newrecord->item, newitem);

            //assign the current pointer to newrecord->next
            newrecord->next = first;

            //newpointer becomes the beginning of the list
            first = newrecord;
      }
      else
      {
            //insert new node sfter an existing node

            //locate the node PRECEDING the target node
            tag = locate (first, target);

            if (tag == NULL)
            {
                  printf("\nMatch not found - Please try again");
            }
            else
            {
                  // allocate space for the new node
                  newrecord = (node *) malloc(sizeof(node));

                  //assign the new data item to newrecord->item
                  strcpy(newrecord->item, newitem);

                  //assign the next pointer to newrecord->next
                  newrecord->next = tag->next;

                  //assign the new pointer to tag->next
                  tag->next = newrecord;
            }
      }
      return (first);
}

node *locate (node *record, char target[]) // locate node
//return a pointer to the node BEFORE the targer node
//The first argument points to the current node
//The second argmument is the target string
{
      if (strcmp(record->next->item, target)==0) // found match
      {
            return(record);
      }
      else
      {
            if (record ->next->next == NULL) //end of list
            {
                  return (NULL);
            }
            else
            {
                  locate(record->next, target); // try next node
            }
      }

}
node *remove(node *first) //remove (delete) one component from the linked list
                                      //return a pointer to beginning of the modified list
//argument points to the first node
{
      node *locate(node *, char []);
      node *tag;
      node *temp;
      char target[40];

      printf("Data item to be deleted: ");
      scanf(" %[^\n]", target);

      if (strcmp(first->item, target)==0)
      {
            //delete the first node

            //mark the node following the target node
            temp = first->next;

            //mark the node following the target node
            free(first);
            //adjust the pointer to the first node
            first = temp;
      }
      else
      {
            //delete a data item other than the first

            //locate the node PRECEDING the target node
            tag = locate(first, target);

            if(tag==NULL)
            {
                  printf("\nMatch not found - Please try again\n");
            }
            else
            {
                  //mark the node following the target node
                  temp = tag->next->next;

                  //freespace for the target node
                  free(tag->next);

                  //adjust the link to the next node
                  tag->next = temp;
            }
      }
      return (first);
}
0
patrickpaelmo
Asked:
patrickpaelmo
  • 3
1 Solution
 
van_dyCommented:
what error does the compiler report?
0
 
van_dyCommented:
in function


>>void create (node *record) //create a linked list
{

  //you have
          scanf("%[^\n]", record->item);     //wrong

  //change this to
         scanf("%s[^\n]", record->item);




0
 
van_dyCommented:
similarily in insert() function

     >>scanf(" %[^\n]", newitem);
     >> canf("%[^\n]", target);

use %s

0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now