Thread: Problem with Link List

  1. #1
    Unregistered
    Guest

    Unhappy Problem with Link List

    Hey, I'm getting 7 errors on this program I wrote for an electronic address book. I'm stuck, I'm not really sure how to fix the errors or even why I'm getting them. I've just started to learn C programming, so keep any fixes simple please!?!

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

    void searchAddress(struct NODE *, char *);
    void enterAddress(struct NODE *);
    int deleteAddress(struct NODE *, char *);
    void printBetween(struct NODE *);
    void printCertain(struct NODE *);
    void print(struct NODE *);

    struct NODE
    {
    int data;
    char fName[20];
    char lName[20];
    char addr[50];
    char phNum[15];
    char conType[10];
    struct NODE *Link;
    struct NODE *Next;
    };

    int main (void)
    {

    struct NODE *pStart;
    struct NODE *pFirst;
    struct NODE *pNow;
    struct NODE *pPre;
    struct NODE *pCur;
    struct NODE *pTemp;
    FILE *pFile;
    char temp[200];
    int mSelect;
    char lastName;
    char cont = 'Y';
    char nameDelete;
    char lNameOne;
    char lNameTwo;
    char contactType;

    pFile = fopen("address.dat", "r");
    if(pFile == NULL)
    {
    printf("\nFailed to open file\n");
    return 0;
    }

    do
    {
    printf("****************************************** *********\n");
    printf("* ADDRESS BOOK MENU *\n");
    printf("****************************************** *********\n");
    printf("* *\n");
    printf("* 1. Search for contact *\n");
    printf("* 2. Add new contact *\n");
    printf("* 3. Delete contact *\n");
    printf("* 4. Find contacts between two last names *\n");
    printf("* 5. Find Family, Friend, or Business Contacts *\n");
    printf("* 6. Quit *\n");
    printf("* *\n");
    printf("****************************************** *********\n");

    if(mSelect == 1)

    printf("Last name of contact to search for: ");
    scanf("%s", lastName);
    searchList(pFirst);
    if(mSelect == 2);
    pStart=pPre=NULL;
    while(cont == 'Y')
    {
    pNow = (struct NODE *)malloc(sizeof(struct NODE));
    if(pNow == NULL)
    printf("\nUnable to allocate memory");
    if(pStart == NULL)
    pStart = pNow;
    else
    pPre->Next = pNow;
    enterAddress(pNow);
    pNow->Next = NULL;
    pPre = pNow;
    printf("\nDo you want to enter another contact? (Y/N) ");
    scanf("%c", &cont);
    }
    if(mSelect == 3)
    printf("\nEnter the last name of contact to be deleted: ");
    scanf("%s", &nameDelete);
    deleteAddress(pStart, nameDelete);
    if(mSelect == 4)
    printf("\nEnter the last name of contact #1: ");
    scanf("%s", &lNameOne);
    printf("\nEnter the last name of contact #2: ");
    scanf("%s", &lNameTwo);
    printBetween(pStart);
    if(mSelect == 5)
    {
    printf("\nEnter the contact type to search for (family, friend, or business): ");
    scanf("%s", &contactType);
    }
    else
    print(pStart);

    }

    void searchAddress(struct NODE *pStart, char *name)
    {
    struct NODE *p;
    char *lastName;
    p = pFirst;

    while(p != NULL)
    {
    if(strcmp(lastName, p->lName) == 0)
    {
    return p;
    }
    else
    p = p->Next;

    }

    return NULL;


    }


    void enterAddress(struct NODE *pNow)
    {
    fflush(stdin);
    printf("Enter new contacts last name: ");
    gets(pNow->lName);
    printf("\nEnter new contacts first name: ");
    gets(pNow->fName);
    printf("\Enter new contacts address: ");
    gets(pNow->addr);
    printf("Enter new contacts phone number: ");
    gets(pNow->phNum);
    printf("This contact is family, friend, or business: ");
    gets(pNow->conType);
    }

    int deleteAddress(struct NODE *pStart)
    {
    struct NODE *pPre;
    struct NODE *pCur;
    char *nameDelete;

    if(pStart == NULL)
    {
    printf("No contact to delete\n");
    return pStart;
    }

    if(pStart->data == nameDelete)
    {
    pCur = pStart;
    pStart = pCur->Link;
    free(pCur);
    }
    else
    {
    pPre = pStart;
    pCur = pStart;

    while(pCur && pCur->data < nameDelete)
    {
    pPre = pCur;
    pCur = pCur->Link;
    }

    if(pCur && pCur->data == nameDelete)
    {
    pPre->Link = pCur->Link;
    pCur->Link = NULL;
    free(pCur);
    }
    else
    printf("This contact isn't in the list\n");
    }
    return pStart;
    }

    void printBetween(struct NODE *pStart)
    {
    struct NODE *pTemp;
    struct NODE *pProne;
    struct NODE *pPrtwo;
    char viewNext = 'Y';
    char fName[20];
    char lName[20];
    char *lNameOne;
    char *lNameTwo;

    if(pStart == NULL)
    {
    printf("\nNo contacts found\n");
    return;
    }
    else
    pProne = pStart;
    while((strcmp(lNameOne, pProne->lName) < 0) && pProne->Next != NULL)
    {
    pProne = pProne->Next;
    }

    pPrtwo = pStart;
    while((strcmp(lNameTwo, pPrtwo->lName) < 0) && pPrtwo->Next != NULL)
    {
    pPrtwo = pPrtwo->Next;
    }

    pTemp = pProne;
    if((strcmp(lNameOne, pProne->lName) <= 0) && (strcmp(lNameTwo, pPrtwo->lName) <= 0))
    {
    do
    {
    printf("\n%s, %s, %s, %s, %s", pTemp->lName, pTemp->fName, pTemp->addr, pTemp->phNum, pTemp->conType);
    pTemp = pTemp->Next;

    if(pTemp)
    {
    printf("\nView the next name? (Y/N) ");
    scanf("%c", &viewNext);
    }
    }
    while((viewNext != 'Y' || viewNext != 'y') && pTemp != pPrtwo->Next);
    }
    else
    printf("\nNo contacts found between those names\n");
    return;
    }


    void printCertain(struct NODE *pStart)
    {
    struct NODE *pTemp;
    char *contactType;
    char comp[10];
    int found = 0;
    char viewNext = 'Y';

    if(pStart == NULL)
    {
    printf("\nNo contacts found\n");
    return;
    }
    else
    pTemp = pStart;

    while((viewNext == 'Y' || viewNext == 'y') && pTemp)
    {
    if(strcmp(pTemp->conType, contactType) == 0)
    {
    printf("\n%s, %s, %s, %s, %s", pTemp->lName, pTemp->fName, pTemp->addr, pTemp->phNum, pTemp->conType);
    found = 1;

    if(pTemp->Next != NULL)
    {
    printf("\nView the next contact? (Y/N) ");
    scanf("%c", &viewNext);
    }
    }

    pTemp = pTemp->Next;

    }
    if(!found)
    printf("\nNo contacts were found\n");
    else
    printf("\nEnd of contacts\n");
    return;
    }

    void print(struct NODE *pStart)
    {
    printf("\nHere are your contacts\n");

    do
    {
    printf("%s, %s, %s, %s, %s", pStart->lName, pStart->fName, pStart->addr, pStart->phNum, pStart->conType);
    pStart = pStart->Next;
    }
    while(pStart != NULL);

    return;
    }

  2. #2
    Unregistered
    Guest
    Just a guess, should structure definition be before prototypes?

  3. #3
    Unregistered
    Guest
    I'm not sure, I'm so frustrated with this right now!?!

  4. #4
    Unregistered
    Guest
    What lines are your errors appearing at?

  5. #5
    Unregistered
    Guest

    sorry about that

    (112) : error C2059: syntax error : 'type'
    (115) : error C2143: syntax error : missing ';' before 'type'
    (116) : error C2065: 'p' : undeclared identifier
    (120) : error C2223: left of '->lName' must point to struct/union
    (120) : error C2198: 'strcmp' : too few actual parameters
    (125) : error C2223: left of '->Next' must point to struct/union

  6. #6
    Unregistered
    Guest
    if(mSelect == 3)
    printf("\nEnter the last name of contact to be deleted: ");
    scanf("%s", &nameDelete);
    deleteAddress(pStart, nameDelete);
    if(mSelect == 4)
    printf("\nEnter the last name of contact #1: ");
    scanf("%s", &lNameOne);
    printf("\nEnter the last name of contact #2: ");
    scanf("%s", &lNameTwo);
    printBetween(pStart);

    should be brackets around the then statements, or only first one will execute. By the way, I've never done one of these.

  7. #7
    Unregistered
    Guest
    You should go through your whole program, putting braces around all the multiple THEN statements. I've never had the guts to try a linked list, so you are way ahead of me.

  8. #8
    Unregistered
    Guest
    The smart guys will be back in the morning.

  9. #9
    Unregistered
    Guest
    In cases 3 and 4 (haven't checked others), you seem to be scanning characters with %s, rather than %c.

  10. #10
    Unregistered
    Guest
    printf("\Enter new contacts address: ");

    It looks like there should be something after the \.

  11. #11
    Unregistered
    Guest

    thanks

    I think its fixed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. Linked List Problem
    By Brew in forum C Programming
    Replies: 6
    Last Post: 03-22-2003, 02:39 AM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM