Linked List Phonebook

This is a discussion on Linked List Phonebook within the C Programming forums, part of the General Programming Boards category; Hello everyone. I am back again with another programming assignment. It's the last one of the semester, and it's a ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    26

    Linked List Phonebook

    Hello everyone. I am back again with another programming assignment. It's the last one of the semester, and it's a doozie. I have to make a phonebook with a linked list.

    I'm going to post the code that I have, and I know that it's broken. I'm looking for some fixes/tips that will help me get along this assignment. Thanks so much guys.

    Code:
    //Ronald Hanifen
    //HW5
    
    
    #include<stdio.h>
    #include<stdlib.h>
    
    
    
    
    
    
    struct entry{
        char firstname[20];
        char lastname[20];
        char number[11];
        struct entry *next;
    };
    typedef struct entry Entry;
    typedef Entry *Entryptr;
    
    
    void insertsomething();
    void insert(Entryptr);
    void printitout();
    void deletesomething();
    void Delete(Entryptr);
    void servesomething();
    Entryptr serve(Entryptr);
    
    
    Entryptr head = NULL;
    
    
    int main()
    {
        int choice;
        while(choice != 5)
        {
    
    
        printf("What would you like to do?\n");
        printf("1 - Insert something in the phonebook?\n");
        printf("2 - Delete something from the phonebook?\n");
        printf("3 - Print out one of the contacts?\n");
        printf("4 - Return the element selected?\n");
        printf("5 - Nothing at all\n");
        printf("Enter 1 through 5: ");
        scanf("%d", &choice);
    
    
        switch(choice){
            case 1:
                insertsomething();
                printitout();
                break;
            case 2:
                deletesomething();
                break;
            case 3:
                printitout();
                break;
            case 4:
                servesomething();
                break;
            default:
                printf("\nThank you for using the phonebook\n");
                return 0;
        }
        }
    
    
        return 0;
    
    
    }
    
    
    void insertsomething()
    {
        char first_name_string[20];
        char last_name_string[20];
        char number[13];
        int value;
        Entryptr ptr;
        printf("Enter the first name of the contact to be entered.\n");
        scanf("%s", first_name_string);
        printf("Enter the last name of the contact to be entered.\n");
        scanf("%s", last_name_string);
        printf("Enter the telephone number of the contact to be entered in the format (xxx)xxx-xxxx:\n");
        scanf("%s", number);
    
    
        ptr = malloc(sizeof(Entry));
    
    
        strcpy(ptr->firstname, first_name_string);
        strcpy(ptr->lastname, last_name_string);
        ptr->next=NULL;
    
    
        insert(ptr);
    
    
        printf("%s %s", ptr->firstname, ptr->lastname); /* Just to check */
    
    
    
    
    }
    
    
    void insert(Entryptr newnodeptr)
    {
        Entryptr trailing = head, target = head;
        if (head == NULL) //List is empty
            head = newnodeptr;
        else if (strcmp(newnodeptr->lastname, target->lastname)>0)
        {
            newnodeptr->next = head;
            head = newnodeptr;
        }
        else
        {
            target = previous->next;
            while(target != NULL)
            {
                if (strcmp(newnodeptr->lastname, target->lastname)>0)
                {
                    newnodeptr->next = target;
                    trailing->next = newnodeptr;
                }
                else
                {
                    trailing = target;
                    target = trailing->next;
                }
            }
    
    
        }
        printitout();
    }
    
    
    void printiout()
    {
        int n = 1;
        Entryptr ptr = head;
        if (head==NULL)
            printf("List is empty\n");
        else
        {
            while (ptr != NULL)
            {
                printf("Entry %d: %s, %s\t\t%s", n, ptr->lastname, ptr->firstname, ptr->number);
                printf("\n\n");
                ptr=ptr->next;
                n++;
            }
        }
    }
    
    
    void deletesomething()
    {
        Entryptr target = head, trailing = head, ptr = NULL;
        char *fstring, *lstring;
        printf("What task do you want to delete (LAST-NAME FIRST-NAME)?\n");
        scanf("%s %s", lstring, fstring);
            if (head == NULL)
                printf("The list is empty\n");
            else if (strcmp(head->lastname, lstring)==0)
            {
                ptr = head;
                head = head->next;
                free(ptr);
            }
            else
            {
                target = trailing->next;
                while (target != NULL)
                {
                    if((strcmp(target->lastname, lstring)==0))
                    {
                        trailing->next = target->next;
                        free(target);
                    }
                    else
                    {
                        trailing=target;
                        target = trailing->next;
                    }
                }
            }
    }

  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    26
    By the way, forgot to mention this. I am inputting a list of alphabetically organized contacts from a file and then I have to be able to add to that file in alphabetical order. Alphabetical by last name, then first name. I am not too worried about the file aspect of it right now, but I just need it to add, delete, and serve something from the list. I haven't written the function servesomething() yet, because I'm not exactly sure how to do it. It's just a part of the spec. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-01-2010, 11:19 PM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 09:04 PM
  3. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  4. bi-linked list based on one-way linked list
    By ronenk in forum C Programming
    Replies: 1
    Last Post: 03-04-2005, 08:16 AM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21