• 0

C - Step linked list analysis


Question

http://pastebin.com/BBHxd0iY

I have to use the the following file shuffle.csv to identify which parish from the most populated county has the least people. It is done line by line, for instance:

county A, parish B, Population B

county A, parish C, Population C

county A -> parish B or C -> Population B or C

If ( pop C < pop B ) county A -> parish C -> Population C

county A -> population + = Pop C

The most populated county is placed in the root of the linked list and then fprintf("output.csv"... ), in each interaction ( line by line ) evaluation til that line.

All the counties are added.

The least populated parish from the corresponding county is updated.

It very easy but I cannot spot the bug. It compiles well but when I run it the output, which is placed in a file named output.csv, is wrong. I have spent at least 5 hours revising the code, debugging, testing, breakpoints and I cant find the little *****r xD. I would appreciate it if you could help me out.

Input file structure:

District , county , parish, population

Grande Porto, Vila do Conde, Rio Mau, 1907

Output file structure:

fprintf( stdout, "%s, %s, %d\n", base->root->minusParish->parishName, base->root->countyName, base->root->minusParish->population);

Arcos, Vila do Conde, 869

...

Ferreiro, Vila do Conde, 660

...

Outeiro Maior, Vila do Conde, 378

...

It just prints Vila do Conde parish and it shouldn't.

HEADER


typedef struct {
int population;
char *parishName;
} parish;

typedef struct nextCounty {
char *countyName;
int population;
parish *minusParish;
struct nextCounty *next;
} county;

typedef struct {
int numberCounties;
county *root;
} core;

int list_assign( core *l, int pos, char **str, int people);

int list_insert( core *l, int pos, county *curr);

county* lista_removeTOSORT(core *l, int pos);

county * list_search( core *l, char* str, int *pos);

int list_sort( core *l, int posiVerifi, county *foundAddr);
[/CODE]

[b][u][color=#b22222]MAIN[/color][/u][/b]

[CODE]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lista.h"

county* createCounty( char *name, int people ) {

county *ptr;

if ( ( ptr = ( county *) malloc( sizeof(county) * 1 ) ) == NULL ) return NULL;

ptr->countyName = name;
ptr->population = people;
ptr->next = NULL;

return ptr;
}

parish* createParish ( char *name, int people ) {

parish *ptr;

if ( ( ptr = ( parish *) malloc( sizeof(parish) * 1 ) ) == NULL ) return NULL;

ptr->parishName = name;
ptr->population = people;

return ptr;
}

FILE* readlnVeriPlace( FILE *input, core *listCore) {

int i;
int people;
county *countyList, *foundAddress;
parish *parishList;
char *countyMalloc;
char *parishMalloc;
char ch;
int position;


if ( input == NULL || listCore == NULL ) return NULL;

parishMalloc = (char *) malloc ( sizeof(char) * 50 );
countyMalloc = (char *) malloc ( sizeof(char) * 50 );

if ( parishMalloc == NULL || countyMalloc == NULL ) return input;

// READLINE

while ( ( ch = fgetc(input) ) != ',' );

i = 0;
while ( ( ch = fgetc(input) ) != ',' ) {
countyMalloc[i] = ch;
i++;
}
countyMalloc[i] = '\0';

i = 0;
while ( ( ch = fgetc(input) ) != ',' ) {
parishMalloc[i] = ch;
i++;
}
parishMalloc[i] = '\0';

fscanf(input, "%d", &people);

while ( fgetc(input) != '\n' && !feof(input)) ;

//ENDREADLINE

foundAddress = list_search( listCore, countyMalloc, &position);

if ( foundAddress != NULL ) {
list_assign( listCore, position, &parishMalloc, people);
free(countyMalloc);
}
else {
parishList = createParish ( parishMalloc, people );
countyList = createCounty ( countyMalloc, people );
countyList->minusParish = parishList;
foundAddress = countyList;
if ( listCore->numberCounties == 0 ) list_insert( listCore, -1, countyList);
}

list_sort( listCore, position, foundAddress);

return input;
}


int main( int argc, const char *argv[] ) {

FILE *input;
FILE *output;
core *base;

if ( argc != 2 ) {
fprintf( stdout, "%s shuffle.csv\n", argv[0] );
exit(1);
}

if ( ( input = fopen( argv[1], "r") ) == NULL ) {
fprintf( stdout, "Not possible to open the file.\n");
exit(2);
}

if ( ( output = fopen ( "output.csv", "w") ) == NULL ) exit(3);

if ( ( base = (core *) malloc( sizeof(core) * 1) ) == NULL ) exit (4);

base->numberCounties = 0;
base->root = NULL;

while ( !feof(input) ) {
input = readlnVeriPlace( input, base );
fprintf( stdout, "%s, %s, %d\n", base->root->minusParish->parishName, base->root->countyName, base->root->minusParish->population);
}

return 0;
}
[/CODE]

[color=#b22222][b][u]Function Library[/u][/b][/color]

[CODE]
#include "lista.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int list_assign( core *l, int pos, char **str, int people)
{
int i = 0;
county *aux;

if (l == NULL || pos < 0)
return -1;

aux = l->root;

for (i = 0; i < pos && aux != NULL; i++)
aux = aux->next;

if (aux == NULL)
return -1;

aux->population += people;

if ( people < aux->minusParish->population ) {
aux->minusParish->population = people;
free(aux->minusParish->parishName);
aux->minusParish->parishName = *str;
} else free(*str);

return pos;
}


int list_insert( core *l, int pos, county *curr)
{
int i = 0;
county *temp;

if (l == NULL || pos < -1 || pos >= l->numberCounties) return -1;

temp = l->root;

if (curr == NULL) return -1;

l->numberCounties++;

if(pos == -1)
{
if (temp == NULL) l->root = curr;
else
{
while (temp->next != NULL) temp = temp->next;
temp->next = curr;
}

return l->numberCounties-1;
}

if (pos == 0)
{
curr->next = temp;
l->root = curr;
return pos;
}

for (i = 0; i < pos-1 && temp != NULL; i++) temp = temp->next;

curr->next = temp->next;
temp->next = curr;

return pos;
}


county* lista_removeToSort(core *l, int pos)
{
int i = 0;
county *prev, *curr;

if (l == NULL || pos < 0 || pos >= l->numberCounties) return NULL;

curr = l->root;

l->numberCounties--;

if(pos == 0)
{
l->root = curr->next;
curr->next = NULL;
return curr;
}

for( i = 0; i < pos && curr->next; i++)
{
prev = curr;
curr = curr->next;
}

prev->next = curr->next;

curr->next = NULL;
return curr;
}

county * list_search( core *l, char* str, int *pos)
{

int i = 0;
county *aux;

if( l == NULL ) return NULL;

for (aux = l->root; aux != NULL; aux = aux->next, i++)
{
if ( strcmp( aux->countyName, str) == 0) {
*pos = i;
return aux;
}
}

*pos = -1;
return NULL;
}

int list_sort( core *l, int posiVerifi, county *foundAddr)
{
county *ptr;
int i = 0;

if ( l == NULL ) return -1;
if ( l->root == NULL || l->numberCounties == 1) return 0;

if ( posiVerifi != -1 ) foundAddr = lista_removeToSort( l, posiVerifi);

for ( ptr = l->root; ptr != NULL; ptr = ptr->next, i++ ) {
if ( ptr->population < foundAddr->population ) {
list_insert( l, i, foundAddr);
}
}
list_insert( l, -1, foundAddr);

return 0;
}[/CODE]

Link to comment
https://www.neowin.net/forum/topic/1078483-c-step-linked-list-analysis/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

        for ( ptr = l->root; ptr != NULL; ptr = ptr->next, i++ ) {
if ( ptr->population < foundAddr->population ) {
list_insert( l, i, foundAddr);
return 0;
}
}
[/CODE]

Well spotted :woot:, however it doesn't produce the correct output so there must be another bug somewhere :|.

Thanks a lot.

  • 0

I think there's a problem with your list insertions in function readlnVeriPlace:

if ( listCore-&gt;numberCounties == 0 ) list_insert( listCore, -1, countyList);

Unless I'm mistaken, your code will only insert the first county because afterwards listCore->numberCounties will be non-zero, therefore list_insert doesn't get executed again.

And personally, I would read a whole line at a time, then use strtok to parse it using ',' as a delimiter.

list_search is going to get expensive as well towards the end of the file.

  • 0

Nice, thanks for your help :rofl: . The sort function does insert. What it does is see if the county is in the list or not with the help of list_search, if it was there (in the list) it removesToSort ( connects the neighbours but does not free )and searches for the correct place and then inserts it there.

The problem was that after the first run, a new item could not be inserted because list_sort had a control statement that was absurd :blush: ,

 if ( l->root == NULL || l->numberCounties == 1) return 0;[/CODE]

So, nothing would ever be added because l->numberCounties would never increment.

:pinch: I removed that instruction

[CODE]if ( listCore->numberCounties == 0 ) list_insert( listCore, -1, countyList);[/CODE]

And replaced,

[CODE] if ( l->root == NULL || l->numberCounties == 1) return 0;[/CODE]

with:

[CODE]if ( l->numberCounties == 0 ) { list_insert( l, -1, foundAddr); return 0; }[/CODE]

  • 0

Nice, thanks for your help :rofl: . The sort function does insert. What it does is see if the county is in the list or not with the help of list_search, if it was there (in the list) it removesToSort ( connects the neighbours but does not free )and searches for the correct place and then inserts it there.

The problem was that after the first run, a new item could not be inserted because list_sort had a control statement that was absurd :blush: ,

 if ( l->root == NULL || l->numberCounties == 1) return 0;[/CODE]

So, nothing would ever be added because l->numberCounties would never increment.

:pinch: I removed that instruction

[CODE]if ( listCore->numberCounties == 0 ) list_insert( listCore, -1, countyList);[/CODE]

And replaced,

[CODE] if ( l->root == NULL || l->numberCounties == 1) return 0;[/CODE]

with:

[CODE]if ( l->numberCounties == 0 ) { list_insert( l, -1, foundAddr); return 0; }[/CODE]

I see. I knew there was a problem with the insertion somewhere, because numberCounties was always 1 after the first insert in readlnVeriPlace.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • RevPDF 4.5.0 by Razvan Serea RevPDF is a free, fully offline PDF editor for Windows, macOS, and Linux that lets you edit text and images directly inside PDF files — no internet connection, no account, and no cloud uploads required. Unlike bloated alternatives that demand subscriptions and constant connectivity, RevPDF fits in under 60MB on desktop while delivering a complete editing toolkit: annotate, redact, sign, compress, split, merge, convert, and reorganize pages, all processed locally on your device. Smart font matching ensures edited text blends seamlessly with the original, and multi-language support includes RTL scripts such as Arabic and Hebrew. Where most PDF editors force you to choose between features and simplicity, RevPDF manages both. You can build interactive forms from scratch with text fields, checkboxes, and dropdowns, permanently redact sensitive data before sharing, draw freehand on contracts and diagrams, and add custom watermarks — all without a single file leaving your machine. Edit Text and Images Directly Inside PDFs RevPDF supports true inline PDF editing — not just annotation layers on top of a document, but actual modification of existing text and images within the file. A smart font-matching engine identifies the font used in the original document and applies it automatically when you make edits, so changes blend naturally with the surrounding content. You can reposition elements, resize images, and update text across single pages or entire documents. RevPDF 4.5.0 release notes: This is one of the biggest updates to RevPDF yet. A lot of things people have been asking for are finally here. New Features Auto Redaction Permanently redact sensitive text and areas from your PDFs before sharing. Clean, irreversible, and fully offline. Comments, Links & Bookmarks Add comments for review, insert clickable links, and create bookmarks to jump around long documents without scrolling forever. Find & Replace Search across the whole document and replace text in one go. Long overdue. Split Pages Vertically or Horizontally Split any page down the middle, vertically or horizontally. Perfect for scanned books or double-page spreads. New Drawing Tools More tools for freehand drawing and markup, better for annotations, sketches, and detailed notes. Continuous Scrolling in Editor The editor now scrolls continuously through pages instead of jumping between them. Working through long documents is a lot smoother now. PDF Metadata Editor View and edit the metadata stored inside your PDFs, including title, author, subject, and keywords. Better Font Matching Text edits now blend in more naturally by doing a better job of matching the original font. Tabbed PDF Viewer Open multiple PDFs at once in tabs and switch between them without going back to the home screen. Add Links Insert hyperlinks anywhere in your PDF, to external URLs or to other pages within the document. Share & Print Shortcuts Share or print directly from the editing screen, home screen, and viewer. No extra steps. Minor Updates Paste images directly from clipboard into your PDF New image editing tools for more control over images inside documents Bug Fixes Fixed file saving issues on Windows and Linux Everything still works fully offline. No login, no cloud, no account. Your files stay on your device. Download: RevPDF 4.5.0 | 58.0 MB (Open Source) Links: RevPDF Home Page | Github | Screenshots 1 | 2 Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Interesting. I'm not using a VPN with my phone. I tried though my home internet (Rogers) and my cellular internet (Telus) and both trigger the dialog above.
    • Three days after Anthropic launched Claude Fable 5 as the most capable AI model it had ever released to the public, the United States government ordered it switched off — and now the company is refunding customers who paid to use a product that vanished almost overnight https://www.techtimes.com/articles/318342/20260613/us-government-pulls-anthropics-fable-5-offline-now-come-refunds-vanished-ai.htm  
    • Microsoft fired the team and replaced them with AI and this is what you get.
    • iPhone 18 Pro Camera Goes Mechanical: Variable Aperture, 2nm Chip, Dark Cherry Due September https://www.techtimes.com/arti...rk-cherry-due-september.htm Apple Liquid Glass iOS 27: WWDC 2026 Brings Refinements Developers Must Adopt Today https://www.techtimes.com/arti...lopers-must-adopt-today.htm Apple WWDC 2026: Siri Rebuilt on Gemini, homeOS Previewed in Cook Farewell Keynote https://www.techtimes.com/arti...d-cook-farewell-keynote.htm
  • Recent Achievements

    • Week One Done
      agatameier earned a badge
      Week One Done
    • One Month Later
      agatameier earned a badge
      One Month Later
    • Week One Done
      ssd21345 earned a badge
      Week One Done
    • Contributor
      MarkHughes4096 went up a rank
      Contributor
    • Dedicated
      jordanspringer earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      507
    2. 2
      +Edouard
      175
    3. 3
      PsYcHoKiLLa
      139
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      76
  • Tell a friend

    Love Neowin? Tell a friend!