Thread: Help with linked list .

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    Help with linked list .

    Hello everybody. I'm making a program which saves a student record and saves them into a .txt file . At the beginning of the program it should load the contents of the txt file and load it into a linked list. (I attached the codes).. Everything else is working fine except for the part where i load the contents of the text file to a linked list. please help me it's taking me ages just to figure it out.
    Attached Files Attached Files

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I will post your code ,because this way it's easier
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<conio.h>
    #include<stdlib.h>
    
    FILE *ftry;
    void displaymenu(void);
    void add(void);
    void del(void);
    void search(void);
    void display(void);
    void save(void);
    
    typedef struct list{
    
    char lastname[20];
    char firstname[20];
    char middlename[20];
    char studentno[15];
    char course[40];
    char contact[20];
    char email[30];
    struct list *next;
    } node;
    
    node *head, *body, *tail;
    
    
    main ()
    {
    system("color BC");
    int choice;
    node *temp3;
    temp3=(node*)malloc(sizeof(node));
    head=NULL;
    tail=NULL;
    if(((ftry=fopen("Directory.txt","r"))!=NULL))
    {
    while(feof(ftry)==0)
    {
    
    fgets(temp3->firstname,20,ftry);
    fgets(temp3->middlename,20,ftry);
    fgets(temp3->lastname,20,ftry);
    fgets(temp3->studentno,15,ftry);
    fgets(temp3->course,40,ftry);
    fgets(temp3->contact,20,ftry);
    fgets(temp3->email,30,ftry);
    
    if(head==NULL)
    {
        head=temp3;
        tail=temp3;
        head->next=tail;
    }
    else
        {
    
        tail->next=temp3;
        tail=temp3;
        }
    }
    }
        do
        {
            displaymenu();
            printf("\nYour choice: ");
            scanf("%d", &choice);
    
                switch(choice)
    
                {
                case 1: system("cls"); //delete
                        printf("You have chosen to add a new record.\n\n");
                        add();
                        break;
                case 2: system("cls");
                        if (head!=NULL)
                        {
                        printf("You have chosen to delete a record.\n\n");
                        del();
                        }
                        else
                        printf("There are no more records to delete!\n");
                        break;
                case 3: system("cls");
                        if(head!=NULL)
                        {
                        printf("You have chosen to search the records.\n\n");
                        search();
                        }
                        else
                        printf("There are no records saved!\n");
                        break;
                case 4: system("cls");
                        if (head!=NULL)
                        {
                        printf("You have chosen to display the records.\n\n");
                        display();
                        }
                        else
                        printf("There are no records to display! \n");
                        break;
                case 5: system("cls");
                        printf("The record has been saved!.\n\n");
                        save();
                        break;
                case 6: system("cls");
                        printf("\n\n\n\n\n\t\t\t  Thank you for using the program!\n\n\n");
                        printf("\t\t\t          Have a nice day!\n\n\n\n\n\n");
                        break;
                default:
                        system("cls");
                        printf("\nWARNING: Invalid input! Choose only from 1-6. Thank you! \n");
                        break;
    
                }
                if (choice!=6)
                {
                    getchar();
                }
    
        } while(choice!=6);
    }
    
    void displaymenu(void)
    {
    
        printf("\n*******************************************************************************\n");
        printf("\t\t\t            M E N U\n");
        printf("*******************************************************************************\n");
        printf("\t[1] Add new record\n");
        printf("\t[2] Delete a record\n");
        printf("\t[3] Search for a record\n");
        printf("\t[4] Display all\n");
        printf("\t[5] Save to file\n");
        printf("\t[6] Exit\n");
    
    }
    
    
    void add(void)
    {
    int choice;
    int x,y,ctr=1;
    int i=0,k=0;
    node *temp, *temp2,*scan;
    body=(node*)malloc(sizeof(node));
    temp=NULL;
    int count=0;
    
    
    strcpy(body->firstname,"N/A");
    strcpy(body->middlename,"N/A");
    strcpy(body->lastname,"N/A");
    strcpy(body->studentno,"-");
    strcpy(body->contact,"-");
    strcpy(body->course,"-");
    strcpy(body->email,"-");
    
    printf("\nPLEASE INPUT THE STUDENT'S INFORMATION YOU WANT TO ADD: \n");
    printf("\n\tFirst name: ");
    getchar();
    scanf("%[^\n]s",body->firstname);
    printf("\n\tMiddle name: ");
    getchar();
    scanf("%[^\n]s", body->middlename);
    printf("\n\tLast name: ");
    getchar();
    scanf("%[^\n]s", body->lastname);
    printf("\n\tStudent number: ");
    getchar();
    scanf("%[^\n]s", body->studentno);
    printf("\n\tCourse: ");
    getchar();
    scanf("%[^\n]s", body->course);
    printf("\n\tE-mail address: ");
    getchar();
    scanf("%[^\n]s",body->email);
    printf("\n\tContact number(You can use +63 and other symbol): ");
    getchar();
    scanf("%[^\n]s",body->contact);
    
    
        if (head==NULL) //insert at beginning if without head
        {
            head=body;
            tail=body;
            tail->next=NULL;
    
        }
        else
        {
    
        while(body->lastname[i]==head->lastname[i])
        {
            x=strlen(body->lastname);
            y=strlen(head->lastname);
    
            if (x>y)
            {
                if(ctr>=y)
                {
    
                    break;
                }
                else
                {
                    i++;
                }
            }
    
            else
            if(x<=y)
            {
                if (ctr>=x)
                {
                    break;
                }
                else
                {
                    i++;
                }
            }
    
            ctr++;
    
        }
            if (body->lastname[i]<head->lastname[i]) //insert at the beginning
            {
    
                body->next=head;
                head=body;
    
            }
    
            else
            {
                if (body->lastname[i]>=tail->lastname[i]) //insert at the end
                {
                    tail->next=body;
                    tail=body;
                }
                else
                {
    
                    temp=head; //insert at the middle
                        while(temp!=NULL)
                        {
    
                            if ((body->lastname[i]>=temp->lastname[i])&&(body->lastname[i] < temp->next->lastname[i]))
                            {
                                                        break;
                            }
    
                            else
                            {
                            temp=temp->next;
                            }
    
                            }
                            body->next=temp->next;
                            temp->next=body;
    
                }
            }
    
    
    
        }
    
    
    do
    {
        printf("\n\n-> Would you like to add another record? [1 for Yes || 0 for No]:  ");
        scanf("%d",&choice);
    
        switch(choice)
        {
            case 1:
                    system("cls");
                    add();
                    break;
            case 0:
                    system("cls");
                    break;
            default:
    
                    printf("\nThere's no such choice in the menu!\n");
                    break;
    }if (choice!=0)
    {
        return;
    }
    
    }
    while (choice!=0);
    }
    
    void display (void)
    {
    int ctr=1;
    node *temp;
    temp=head;
    tail->next=NULL;
    
    while (temp!=NULL)
    {
        printf("%d.)",ctr);
        ctr++;
        printf("\tNAME: \t\t%s, %s %s\n", temp->lastname,temp->firstname,temp->middlename);
        printf("\tSTUDENT NO: \t%s\n", temp->studentno);
        printf("\tCOURSE: \t%s\n",temp->course);
        printf("\tCONTACT #: \t%s\n",temp->contact);
        printf("\tE-MAIL ADDRESS: %s\n\n",temp->email);
        temp=temp->next;
    }
    }
    void del (void)
    {
        if (head==NULL)
        {
            printf("ALERT: \n\nThere are no more records to delete!\n\n");
            return;
        }
    
        node *temp,*temp2,*scan;
        int choice;
        char num[30];
        printf("\nPlease enter the STUDENT NUMBER of the student you want to delete: ");
        getchar();
        scanf("%[^\n]s", num);
        tail->next=NULL;
        temp=head;
        scan=head;
    
        while(scan!=NULL) //scan if record exists
        {
            if(strcmp(num,scan->studentno)==0)
            {
                break;
            }
            else
            {
                if(scan->next==NULL)
                {
                    printf("\nRECORD DOES NOT EXISTS!\n\n");
                    free(scan);
                    return;
                }
                scan=scan->next;
            }
        }
    
    
    
        if(strcmp(num,head->studentno)==0)//delete at beginning
        {
            //temp=head;
            head=head->next;
            free(temp);
        }
        else
        {
            if (strcmp(num,tail->studentno)==0)//delete at the end
            {
                //temp=head;
    
                while (temp!=NULL)
                {
                    if (temp->next==tail)
                    break;
    
                    else
                    temp=temp->next;
                }
    
            free(temp->next);
            tail=temp;
            tail->next=NULL;
            }
    
            else                            //delete at the middle
            {       tail->next=NULL;
                    //temp=head;
                    while (strcmp(num,temp->next->studentno)!=0)
                    {
                    temp=temp->next;
                    }
    
                        temp2=temp->next;
                        temp->next=temp->next->next;
                        free(temp2);
    
            }
    }
    
    printf("\n\nRECORD SUCCESSFULLY DELETED\n\n");
    
    do
    {
    
        printf("\n\n-> Would you like to Delete another record? [1 for Yes || 0 for No]:  ");
        getchar();
        scanf("%d",&choice);
    
        switch(choice)
        {
            case 1:
                    system("cls");
                    del();
                    break;
            case 0:
                    system("cls");
                    break;
            default: printf("\nThere's no such choice in the menu!\n");
                    break;
        }
    
    }
    while (choice!=0);
    
    }
    
    void search (void)
    {
        char key[40];
        int choice;
        int ctr=0;
        node *temp;
        temp=head;
        tail->next=NULL;
    
        printf("\nEnter a keyword to search (Case Sensitive). \n\nIt can be any ONE of Last name, First name, Middle name, Student number, Course,Contact or E-mail address:\n\n");
        getchar();
        scanf("%[^\n]s",key);
        system("CLS");
    
    while(temp!=NULL)
    {
        if (strcmp(key,temp->firstname)==0||strcmp(key,temp->lastname)==0||strcmp(key,temp->middlename)==0||strcmp(key,temp->course)==0||strcmp(key,temp->studentno)==0||strcmp(key,temp->contact)==0||strcmp(key,temp->email)==0)
        {
        printf("SEARCH RESULTS:\n");
        printf("\n\tNAME: \t\t%s, %s %s\n", temp->lastname,temp->firstname,temp->middlename);
        printf("\tSTUDENT NO: \t%s\n", temp->studentno);
        printf("\tCOURSE: \t%s\n",temp->course);
        printf("\tCONTACT #: \t%s\n",temp->contact);
        printf("\tE-MAIL ADDRESS: %s\n\n",temp->email);
        ctr++;
        }
    temp=temp->next;
    }
            if(ctr==0)
            {
    
                printf("SEARCH RESULTS:\n");
                printf("\nNO MATCHES FOUND!\n");
            }
    
    do
    {
        printf("\n\n-> Would you like to search again? [1 for Yes || 0 for No]:  ");
        scanf("%d",&choice);
    
        switch(choice)
        {
            case 1:
                    system("cls");
                    search();
                    break;
            case 0:
                    system("cls");
                    return;
    
            default: printf("\nThere's no such choice in the menu!\n");
                    break;
        }
    if (choice!=0)
    {
        return;
    }
    
    }
    while (choice!=0);
    }
    
    void save(void)
    {
    
    node *temp;
    temp=head;
    tail->next=NULL;
    
    if((ftry=fopen("Directory.txt","r"))!=NULL)
    {
        remove(ftry);
    }
    
    
    ftry=fopen("Directory.txt","w+");
    
    while (temp!=NULL)
    {
        fprintf(ftry,"%s\n",temp->firstname);
        fprintf(ftry,"%s\n",temp->middlename);
        fprintf(ftry,"%s\n",temp->lastname);
        fprintf(ftry,"%s\n",temp->studentno);
        fprintf(ftry,"%s\n",temp->course);
        fprintf(ftry,"%s\n",temp->contact);
        fprintf(ftry,"%s\n\0",temp->email);
        temp=temp->next;
    }
    fclose(ftry);
    
    }

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by imbaweak View Post
    a .txt file .
    You could also post the .txt file and describe what is the behavior of your code

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Rather than posting 500+ lines of code, most of which is irrelevant to the problem, you should instead create another project just to test your ideas.

    Code:
    struct node {
      int num;
      struct node *next;
    };
    
    struct node *append ( struct node *head, int num ) {
        // allocates a new node
        // assigns num
        // links the new node onto the end of the list
    }
    In 20 to 50 lines, you should be able to come up with a facsimile of your underlying question.

    Not only is this a lot easier for us to read, it in itself may guide you to answering your own question as well (because it is clutter free).
    When you understand how this works, you'll be in a better position to apply that new knowledge to your main project.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    sorry for the late reply, here's the behavior of my code. (i'll explain the behavior of my code above)

    My text file contains:
    a
    a
    a
    a
    a
    a
    a
    b
    b
    b
    b
    b
    b
    b

    after i used this code to scan the txt file
    Code:
    main ()
    {
    system("color BC");
    int choice;
    node *temp3;
    temp3=(node*)malloc(sizeof(node));
    head=NULL;
    tail=NULL;
    if(((ftry=fopen("Directory.txt","r"))!=NULL))
    {
    while(feof(ftry)==0)
    {
    
    fgets(temp3->firstname,20,ftry);
    fgets(temp3->middlename,20,ftry);
    fgets(temp3->lastname,20,ftry);
    fgets(temp3->studentno,15,ftry);
    fgets(temp3->course,40,ftry);
    fgets(temp3->contact,20,ftry);
    fgets(temp3->email,30,ftry);
    
    if(head==NULL)
    {
        head=temp3;
        tail=temp3;
        head->next=tail;
    }
    else
        {
    
        tail->next=temp3;
        tail=temp3;
        }
    }
    }
    and display it with this code:

    Code:
    voiddisplay (void){
    int ctr=1;
    node *temp;
    temp=head;
    tail->next=NULL;
    
    while (temp!=NULL)
    {
        printf("%d.)",ctr);
        ctr++;
        printf("\tNAME: \t\t%s, %s %s\n", temp->lastname,temp->firstname,temp->middlename);
        printf("\tSTUDENT NO: \t%s\n", temp->studentno);
        printf("\tCOURSE: \t%s\n",temp->course);
        printf("\tCONTACT #: \t%s\n",temp->contact);
        printf("\tE-MAIL ADDRESS: %s\n\n",temp->email);
        temp=temp->next;
    }
    }
    the only thing will be displayed is
    b
    b
    b
    b
    b
    b
    b
    .
    The a's are not displayed.

    when i try to search them using this code:
    Code:
    voidsearch(void)
    {
      char key[40];
      int choice;
      int ctr = 0;
      node *temp;
      temp = head;
      tail->next = NULL;
    
      printf
          ("\nEnter a keyword to search (Case Sensitive). \n\nIt can be any ONE of Last name, First name, Middle name, Student number, Course,Contact or E-mail address:\n\n");
      getchar();
      scanf("%[^\n]s", key);
      system("CLS");
      while (temp != NULL) {
        if (strcmp(key, temp->firstname) == 0
            || strcmp(key, temp->lastname) == 0
            || strcmp(key, temp->middlename) == 0
            || strcmp(key, temp->course) == 0
            || strcmp(key, temp->studentno) == 0
            || strcmp(key, temp->contact) == 0
            || strcmp(key, temp->email) == 0) {
          printf("SEARCH RESULTS:\n");
          printf("\n\tNAME: \t\t%s, %s %s\n", temp->lastname, temp->firstname,
                 temp->middlename);
          printf("\tSTUDENT NO: \t%s\n", temp->studentno);
          printf("\tCOURSE: \t%s\n", temp->course);
          printf("\tCONTACT #: \t%s\n", temp->contact);
          printf("\tE-MAIL ADDRESS: %s\n\n", temp->email);
          ctr++;
        }
        temp = temp->next;
      }
      if (ctr == 0) {
    
        printf("SEARCH RESULTS:\n");
        printf("\nNO MATCHES FOUND!\n");
    
      }
    the result is "No matches found".. sme for the delete() function. it's like it is saved in a different structure. Am i missing something? how can i read from txt file and then insert it to the same linked list i used before?
    Last edited by Salem; 10-09-2012 at 09:51 AM. Reason: demungled the tags - try harder

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You only allocate memory for "temp3" once, thus it will always point to the same node. With every iteration you overwrite the data from the iteration before. That's why you store only the data from your last iteration.
    You have to create a new node for every node inside the loop.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM

Tags for this Thread