Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,062 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,590 people online right now. Registration is fast and FREE... Join Now!




linked list add function

 
Reply to this topicStart new topic

linked list add function, linked list functions

crazifoom
22 Apr, 2008 - 07:08 AM
Post #1

New D.I.C Head
*

Joined: 20 Nov, 2007
Posts: 5


My Contributions
I am having a difficult time with my AddNode function. It takes in a head and data and makes a node, but when i run the program, it prints memory addresses or something.I just have no idea why its not working.
.


Main Function
CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "LinkedFunctions.c"

int
main()
{
    int numStu;  // number of students enrolled in classes
    int initStu;
    int ID;
    char classN[6];
    int sect;
    int cred;
    char code[3];
    int mod;
    
    FILE *infilep, *outfilep;
    infilep = fopen("Lab11InputData.txt", "r");
    outfilep = fopen("Lab11OutputReport.txt", "w");
      
      typedef struct class_node_s
      {
            char className[6];
            int section;
            int credits;
            struct class_node_s * next;
      }class_node_t;
      
      typedef struct
      {
            int SID;
            class_node_t * head;
      }student_t;

    fscanf(infilep, "%d", &numStu);
    student_t students[numStu];
    
    int i;
    for(i = 0; i < numStu; i++)
    {
        fscanf(infilep, "%d", &students[i].SID);
        students[i].head = ( class_node_t* )malloc( sizeof(class_node_t) );
    }
    
    fscanf(infilep, "%d", &initStu);
    fscanf(infilep, "%d", &ID);
    fscanf(infilep, "%s", classN);
    fscanf(infilep, "%d", §);
    fscanf(infilep, "%d", &cred);
    for(i = 0; i < initStu; i++)
    {
        
        if(ID == (students[i].SID))
        {
            AddNode(students[i].head, ID, classN, sect, cred);
        }
        
    }
    
    fprintf(outfilep, "%d : %s %d %d", students[2].SID, students[2].head->className, students[2].head->section, students[2].head->credits);
      
    return(0);    
}


"AddNode"
CODE

void AddNode( struct class_node_t *head, int ID, char classN[], int sect, int cred)
{
    typedef struct class_node_s
      {
            char className[6];
            int section;
            int credits;
            struct class_node_s * next;
      }class_node_t;
      
class_node_t *newNode;
newNode = (class_node_t*)malloc(sizeof(class_node_t));
strcpy(newNode->className, classN);
newNode->section = sect;
newNode->credits = cred;
newNode->next = 0;

head = newNode->className;
}



Also i don't quite understand how the node is being created if the code in AddNode is all local. i know it has to do with memory dynamically allocated to the heap
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:35PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month