Thread: Structures & Linked List

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    3

    Structures & Linked List

    A buddy and I trying to make a program that can take in records and, by using linked list and a structure, process them in a variety of task.
    I've put several checks in the code to see where it goes wrong.

    Code:
    #include <stdlib.h>#include <string.h>
    #define size 21
    
    
    int check = 0;
    
    
    struct student{
        struct student* back;
        char first[size];
        char last[size];
        double score;
        int zip;
        struct student* next;
    };
    struct student *head;
    struct student *tail;
    struct student *current;
    struct student *temp;
    
    
    void insert(){
        struct student *lead = (struct student*)malloc(sizeof(struct student));
        lead->back = current;
        lead->next = tail;
        temp = tail;
        printf("First name: ");
        scanf("%s",&lead->first);
        printf("Last name: ");
        scanf("%s",&lead->last);
        printf("Score: ");
        scanf("%f",&lead->score);
        printf("Zip: ");
        scanf("%d",&lead->zip);
        tail->back = temp;
        printf("Check 3\n");
        while(&temp->back != &head){
            temp = temp->back;
        }
        head->next = temp;
        printf("Check 4\n");
        current->next = lead;
        printf("Check 5\n");
        current = lead;
        printf("\nRecord inserted successfully!\n\n");
    }
    int main(){
        printf("Check 1\n");
        head = (struct student*)malloc(sizeof(struct student));
        tail = (struct student*)malloc(sizeof(struct student));
        tail->back = head;
        head->next = tail;
        current = head;
        printf("Check 2\n");
        char fTemp[size],lTemp[size];
        int records,i,j,k;
        printf("How many records would you like to enter? (min. 5)");
        scanf("%d",&records);
        for(i = 0;i<records;i++){
            insert();
        }
    For some reason it will stick after Check 3 and go into a infinite while loop. Help please!

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Line 26:
    Code:
    temp = tail;
    Line 35:
    Code:
    tail->back = temp;
    tail->back point to tail, also temp->back points to temp!

    Line 37 is wrong and build the infinity loop:
    Code:
    while(&temp->back != &head){
    The address of temp->back can never be the address of head.

    should be:
    Code:
    while(temp->back != head){
    Line 38 build also a infinity loop because temp and temp->back point to the same address.
    Code:
    temp = temp->back;
    Other have classes, we are class

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should be sure you post the complete code. In the posted code, there is a missing closing semi-colon at the end of "main". Also, be sure to include "stdio.h" when using functions like "printf" and "scanf".

    Are you getting any compiler warnings? If not, you need to increase the warning level of your compiler. Here are some important ones I got:

    Code:
    main.c|29|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[21]'|
    main.c|31|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[21]'|
    main.c|33|warning: format '%f' expects type 'float *', but argument 2 has type 'double *'|
    A couple of other observations:





    I think you need to spend a little more time planning out your program before attempting to write code.

    Code:
    head = (struct student*)malloc(sizeof(struct student));
    tail = (struct student*)malloc(sizeof(struct student));
    tail->back = head;
    head->next = tail;
    What are the values of "head->back" and "tail->next"?

    Code:
    temp = tail;
    So now "temp" is set equal to "tail"...

    Code:
    tail->back = temp;
    ... and "tail->back" is set equal to "temp", which is just "tail". It appears you're tying your list into a knot.

    Code:
    while(&temp->back != &head){
        temp = temp->back;
    }
    You probably want to be comparing the variables here, not their addresses.

    Also, the multiple pointer variables you're using can lead to confusion, making your code difficult to analyze.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly-Linked List and structures
    By Socrates271 in forum C Programming
    Replies: 3
    Last Post: 05-01-2012, 08:52 AM
  2. Write to files a linked list of structures
    By drew99 in forum C Programming
    Replies: 11
    Last Post: 11-22-2011, 12:50 PM
  3. Structures - Linked List
    By Oonej in forum C Programming
    Replies: 14
    Last Post: 07-14-2011, 09:54 PM
  4. Structure of structures + linked list
    By saeculum in forum C Programming
    Replies: 3
    Last Post: 03-06-2009, 08:02 PM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM

Tags for this Thread