It compiles and run but I'm stuck on the last part - I can't figure out how to iterate through the LinkedList and delete all students whose birthYear is greater than 1995 (print only students whose birthYear is less than 1995).

I have to iterate after inserting all records into the linkedlist and before opening a new text file to print out the results.

This is the link to student.txt file - http://dl.dropbox.com/u/43732846/student.txt

Please help me out!

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

// Define Doubly Linked List ADT
struct Node {
	Node	*pPreviousNode;
	void	*pData;
	Node	*pNextNode;
} ;

struct LinkedList {
	Node	*pFirstNode;
	Node	*pLastNode;
} ;

struct StudentInfo {
	int id;
	char name[30];
	char lastName[30];
	char birthPlace[30];
	short birthYear;
} ;

void insertAfter (Node *newNode, Node *node) { // insert newNode afternode
	newNode->pPreviousNode = node;
	newNode->pNextNode = node->pNextNode;
	(node->pNextNode)->pPreviousNode = newNode;
	node->pNextNode = newNode;

void deleteNode(Node *node) {
	(node->pPreviousNode)->pNextNode = node->pNextNode;
	(node->pNextNode)->pPreviousNode = node->pPreviousNode;

int main() {
	// Step 1: Construct our Linked List with sentinel nodes
	LinkedList *myLinkedList = (LinkedList *) malloc(sizeof(LinkedList));

	Node *firstNode = (Node *) malloc(sizeof(Node)); // sentinel for start node
	Node *lastNode = (Node *) malloc(sizeof(Node)); // sentinel for end node

	firstNode->pPreviousNode = NULL;
	firstNode->pData = NULL;
	firstNode->pNextNode = lastNode;
	myLinkedList->pFirstNode = firstNode;

	lastNode->pPreviousNode = firstNode;
	lastNode->pData = NULL;
	lastNode->pNextNode = NULL;
	myLinkedList->pLastNode = lastNode;

	// Step 2: Open file for reading, and read the records one by one, and insert into LinkedList one by one
	FILE *fileStudent;
	fileStudent = fopen("D:\\student.txt", "r");
	char line[150];
	while (fgets(line, 150, fileStudent)) {
		if(line[0] == 'I') // skip the first line, the header line
		StudentInfo *student = (StudentInfo *) malloc(sizeof(StudentInfo));
		// printf("address of student: %i ", student);
		sscanf(line, "%i %s %s %s %i", &student->id, student->name, student->lastName, student->birthPlace, &student->birthYear);
		Node *newNode = (Node *) malloc(sizeof(Node));
		newNode->pData = student;
		insertAfter(newNode, myLinkedList->pFirstNode);

	// Step 3: Open a new file for writing. Then iterate thorough the nodes of LinkedList, write them to the file one by one
	FILE *fileStudentResult;
	fileStudentResult = fopen("D:\\result.txt", "w"); // open file for writing
	Node *tempNode = myLinkedList->pFirstNode;
	tempNode = tempNode->pNextNode; // skip the first sentinel node
	StudentInfo *info;
	while (tempNode->pNextNode != NULL) {
		info = (StudentInfo *)tempNode->pData; // casting the pData to StudentInfo
		fprintf(fileStudentResult, "%i \t %s \t %s \t %s \t %i \n", info->id, info->name, info->lastName, info->birthPlace, info->birthYear); // print to the file
		tempNode = tempNode->pNextNode; // go to the next node

	printf("\n\n Press any key to continue.");
	getchar(); getchar();
5 Years
Discussion Span
Last Post by Narue


Please don't bump your threads, it's inconsiderate to all of the other people who need help.

I can't figure out how to iterate through the LinkedList and delete all students whose birthYear is greater than 1995

I fail to see the problem. Your main() function has code to iterate through the list, so you clearly know how to do it, and the list library contains a deleteNode() function. All you need to do is iterate and delete at the correct time:

Node *tempNode = firstNode->pNextNode;

/* Delete nodes with an even ID */
while (tempNode->pNextNode != NULL) {
    StudentInfo *info = (StudentInfo *)tempNode->pData;
    Node *next = tempNode->pNextNode;
    if (info->id % 2 == 0)
    tempNode = next;

The only slightly confusing part is making sure that you can safely get to the next node after deleting the current one, but that's a simple matter of a temporary node.

Votes + Comments
Thank you for the great help

Sorry about bumping...

I copied your code to Line 68 before the }.

I don't quite understand though. I'm only supposed to remove anyone whose birthYear is greater than 1995.

The student.txt file has 1000s of these with different name, ethnicity, city, and birthyear. I'm not understanding why I would remove id %2 = 0;

36 Jeannette White Honolulu 1995

I tried various variables but it would not let me remove birthYear > 1995...


I'm not understanding why I would remove id %2 = 0

You wouldn't. My code isn't a solution to your exact problem, read the comment. :icon_rolleyes: To be exactly what you want, the condition would be if (info->birthYear > 1995) , and I kind of assumed you weren't so helpless as to be unable to figure that out.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.