Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


[Q] Is there any one good at linked lists - C filter_list
Author
Message
[Q] Is there any one good at linked lists - C #1
This is a beginning of my homework but I don't know what is the problem in my code.

I need to store every word separately in a linked list.
Splitting process is just working fine, but problem in the add() function I think.

In the main func. I send every word to add() function to add in linked list's  end. I assign new word into current->data and It seems working fine when I output it inside the add() function, but the problem is also head node which is my root node is also changing, it shouldn't be changed.

Help me please

here is the sample Input1.txt
sample text Wrote:Term Frequency is a feature representation technique. Term frequency is often used in
text mining field. Term Frequency measures how frequently a term occurs in a
document.

As seen on output below, all my current->data and head->data are same in every loop. But head->data have to stay as first word('Term' in this case)
[Image: ZOZWrg.png]
My codes: http://codepad.org/aGtLDUja
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

struct liste
{
 char * data;
 struct liste * next;
};
typedef struct liste Node;
Node *current,*head;

void add(char *chr)
{
   current->data = chr;
   Node *tmp = (Node*)malloc(sizeof(Node));
   tmp->next = NULL;
   current->next = tmp;
   printf("word added: %s \t\t head: %s\n",current->data,head->data);
   current = current->next;
}

int main() {
   current = (Node *)malloc(sizeof(Node));
   current->next = NULL;
   head = current;

   FILE *dosya = fopen("C:/Users/omerf/CLionProjects/untitled/Input1.txt","r");
   if (dosya == NULL )
   {
       printf("dosya acilamadi\n");
       exit(1);
   }

   char c[20];

       while (!feof(dosya))
       {
           fscanf(dosya,"%s",c);
           int i=0;
           for(i=0 ; i< strlen(c) ; i++)
           {
               if(!isalpha(c[i])) c[i] = '\0';
           }

           add(c);
       }

   fclose(dosya);

   return 0;


}

[Image: xh31A6W.gif]

Reply

RE: [Q] Is there any one good at linked lists - C #2
Looks fine like this: https://ideone.com/a7QjtT

Perhaps check your file reading.

Reply

RE: [Q] Is there any one good at linked lists - C #3
(10-20-2017, 06:33 PM)Altair Wrote: Help me please

I took a whirl at it, here's my code. I just modified what you had, and I didn't test it out, but anywhore, give this a shot. I made comments to help guide you.

I actually wrote a tutorial on making linked lists in C a while ago. You can find that tutorial at: https://sinister.ly/Thread-Tutorial-C-Linked-list

Alternatively, I have a list of all my tutorials that you can browse through at https://sl.ipseitysoftware.com/ (it's also in my signature).

Feel free to ask my any questions.

I also added a pastebin link here with syntax highlighting on

Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct liste
{
char * data;
struct liste * next;
};
// don't fucking typedef this shit, do it right
struct liste *head, *tail; //I renamed this to tail, since that's what it is, current sounds confusing
void add(char *); // add your function prototypes, even in small programs. Get into the habit
void clear(struct liste *) // recursive free memory

void add(char *chr)
{
    struct liste *tmp = malloc(sizeof(struct liste));
    tmp->data = strdup(chr); // don't forget that this is a c-string, so = is pointer equality
    tmp->next = NULL;
    if (tail == NULL)
    {
         // if the list has no elements yet, then this should be
         // both the head, and our tail.
         tail = head = tmp;
    }
    else
    {
         // the list already has elements
         tail->next = tmp; // create the link in the list
         tail = tmp; // advance our tail position
    }
    printf("word added: %s \t\t head: %s\n",tail->data,head->data);
}
void clear(struct liste *node)
{
    if (node->next != NULL)
         clear(node->next)
    free(node->data);
    free(node);
}

int main()
{ // always give the brace it's own line...it helps finding missing ones
    /* variable definitions */ // always put these BEFORE any code or initializations, this is C
    FILE *doysa;
    char *c;
    int i, slen;
    /* initialize variables */
    head = tail = NULL; // set up our empty list
    /* program code */
    if ((dosya = fopen("C:/Users/omerf/CLionProjects/untitled/Input1.txt","r")) == NULL) // file failed
    {
         printf("dosya acilamadi\n");
         exit(-1); // i like to use negative failure numbers, -1 is also EOF
    }
    c = malloc(20); // allocate character buffer
    while (!feof(dosya))
    {
         fscanf(dosya,"%s",c);
         slen = strlen(c); // this will speed up your program by reducing memory operations
         for(i = 0; i < slen; ++i)
         {
              if(!isalpha(c[i]))
                   c[i] = '\0'; // try to avoid same line if bodies..
         }
         add(c);
    }
    fclose(dosya);
    /* deallocate memory */
    free(c); // no longer need buffer
    if (head != NULL) // if we have a list with entries
         clear(head); // our recursive function will clean everything up for us nicely.
    return 0;
}
(This post was last modified: 10-21-2017, 09:00 AM by phyrrus9.)

[+] 1 user Likes phyrrus9's post
Reply

RE: [Q] Is there any one good at linked lists - C #4
@phyrrus9 Thank you so much very well explained.

I see my all mistakes, I wasn't actually using typedef but I saw in every tutorial on the net they're using it so I thought I should also use.

Freeing up memory also didn't come to my mind, good to learn, and strdup() also.

" c = malloc(20); " this one is really useful, what a shame I didn't know it before

I learned much from your corrections, very much appreciated master Smile

Reply

RE: [Q] Is there any one good at linked lists - C #5
(10-21-2017, 09:36 PM)Altair Wrote: @phyrrus9 Thank you so much very well explained.

I see my all mistakes, I wasn't actually using typedef but I saw in every tutorial on the net they're using it so I thought I should also use.

Freeing up memory also didn't come to my mind, good to learn, and strdup() also.

" c = malloc(20); " this one is really useful, what a shame I didn't know it before

I learned much from your corrections, very much appreciated master Smile

Any time man. Feel free to ask me if you need any help, I lurk the forums 15ish hours a day and am always idling in the discord.

As for the typedefs, I was just giving you shit, it doesn't really make any difference programatically if you use them or not. I choose not to use them because in C, the following isn't legal
Code:
struct something
{
   int var;
};

something variable;
but rather, you must put the word struct before it, so it becomes this:
Code:
struct something
{
   int var;
};

struct something variable;

While it makes no difference between doing it this way and with a typedef, I feel like having to add the word struct adds valuable information to the programmer. Knowing it's a structure and not a builtin or POD type is useful information in my eyes.

Malloc is very useful. What you had:
Code:
char c[20];
allocates it's data at compile time, rather than run time, so you need to use fixed sizes. Say for instance you wanted to concatenate two strings. Using the bracket style (static) allocation, you'd have to do something like this:
Code:
char *str1 = "Hello ";
char *str2 = "World!";
char buf[200]; // note, we have to use some insane number!
strcpy(buf, str1); // copy first string into buf
strcpy(buf + strlen(buf), str2); // copy second string into buf
This wastes a bunch of memory, and causes issues if it's longer than 200 bytes. With dynamic allocation (malloc), we can get exactly the right amount:
Code:
char *str1 = "Hello ";
char *str2 = "World!";
char *str3 = malloc(strlen(str1) + strlen(str2) + 1); // size of both strings, plus one for the null terminator at the end
strcpy(str3, str1);
strcpy(str3 + strlen(str3), str2);

Malloc is an extremely useful tool, but don't forget that you need to use free for anything you've malloc'd. You don't need to free things you define with brackets.


I'm curious, did my answer solve your problems?

[+] 1 user Likes phyrrus9's post
Reply

RE: [Q] Is there any one good at linked lists - C #6
(10-21-2017, 09:48 PM)phyrrus9 Wrote: I'm curious, did my answer solve your problems?

Actually, my HW's deadline was an hour ago( That wasn't the whole problem, I sent it. Not perfect but at least it's working Biggrin ). I did static allocation with brackets, unfortunately, I didn't use what I learned from your solution because I didn't have the time. I was want to use dynamic allocation but I wasn't know when I wrote. But I think I learned well, next time I will do my best.
(This post was last modified: 10-21-2017, 10:50 PM by Altair.)

Reply

RE: [Q] Is there any one good at linked lists - C #7
(10-21-2017, 10:49 PM)Altair Wrote:
(10-21-2017, 09:48 PM)phyrrus9 Wrote: I'm curious, did my answer solve your problems?

Actually, my HW's deadline was an hour ago( That wasn't the whole problem, I sent it. Not perfect but at least it's working Biggrin ). I did static allocation with brackets, unfortunately, I didn't use what I learned from your solution because I didn't have the time. I was want to use dynamic allocation but I wasn't know when I wrote. But I think I learned well, next time I will do my best.

Well that's sad. Hope you get a decent grade anyways. Is this just a high school level class or uni? Give me some more details on the class, I'm interested. It's becoming increasingly rare that any Uni teaches any non-Java language, let alone C. It's practically nonexistent here in the US for a high school to teach programming, and the ones that do try to pass off basic HTML as programming. You don't know how lucky you are to be getting a real world education.

Reply

RE: [Q] Is there any one good at linked lists - C #8
(10-21-2017, 10:56 PM)phyrrus9 Wrote:
(10-21-2017, 10:49 PM)Altair Wrote:
(10-21-2017, 09:48 PM)phyrrus9 Wrote: I'm curious, did my answer solve your problems?

Actually, my HW's deadline was an hour ago( That wasn't the whole problem, I sent it. Not perfect but at least it's working Biggrin ).  I did static allocation with brackets, unfortunately, I didn't use what I learned from your solution because I didn't have the time.  I was want to use dynamic allocation but I wasn't know when I wrote.  But I think I learned well, next time I will do my best.

Well that's sad. Hope you get a decent grade anyways. Is this just a high school level class or uni? Give me some more details on the class, I'm interested. It's becoming increasingly rare that any Uni teaches any non-Java language, let alone C. It's practically nonexistent here in the US for a high school to teach programming, and the ones that do try to pass off basic HTML as programming. You don't know how lucky you are to be getting a real world education.

Unis usually teach 2 + other languages other than the intro one as electives. For example, C and python were options at my school at the time. If it's HS though, very impressive. Especially with learning linked lists. Puts you well above the curve going into uni if that is the case.
"If you look for the light, you can often find it. But if you look for the dark, that is all you will ever see.”


Reply

RE: [Q] Is there any one good at linked lists - C #9
(10-21-2017, 10:56 PM)phyrrus9 Wrote: Well that's sad. Hope you get a decent grade anyways. Is this just a high school level class or uni? Give me some more details on the class, I'm interested. It's becoming increasingly rare that any Uni teaches any non-Java language, let alone C. It's practically nonexistent here in the US for a high school to teach programming, and the ones that do try to pass off basic HTML as programming. You don't know how lucky you are to be getting a real world education.
I hope, thanks Smile

I'm studying CSE at University 2nd grade. Class name is called "Data Structures", java was in the previous year but not now, we are studying this class with C only as far as for now. There are much more topics actually, linked lists is the beginning, we learned trees etc. etc. I don't remember what we learned actually, I couldn't join clases recently ./
I'm from Turkey btw.
(This post was last modified: 10-21-2017, 11:14 PM by Altair.)

Reply

RE: [Q] Is there any one good at linked lists - C #10
(10-21-2017, 11:14 PM)Altair Wrote:
(10-21-2017, 10:56 PM)phyrrus9 Wrote: Well that's sad. Hope you get a decent grade anyways. Is this just a high school level class or uni? Give me some more details on the class, I'm interested. It's becoming increasingly rare that any Uni teaches any non-Java language, let alone C. It's practically nonexistent here in the US for a high school to teach programming, and the ones that do try to pass off basic HTML as programming. You don't know how lucky you are to be getting a real world education.
I hope, thanks Smile

I'm studying CSE at University 2nd grade. Class name is called "Data Structures", java was in the previous year but not now, we are studying this class with C only as far as for now. There are much more topics actually, linked lists is the beginning, we learned trees etc. etc. I don't remember what we learned actually, I couldn't join clases recently ./
I'm from Turkey btw.

Well kudos to Turkey for having a useful University program. I wish the US wasn't falling so far behind.

Data structures and algorithms are always fun to play around with. I think it's key that you learn how to do these in C, it teaches you how everything works under the hood, so when you move to C++, Java, and C# you understand how they work and know how to treat them with respect.

[+] 1 user Likes phyrrus9's post
Reply







Users browsing this thread: 1 Guest(s)