Help with linked list in C


 
Thread Tools Search this Thread
Top Forums Programming Help with linked list in C
# 8  
Old 03-31-2011
Linked List Basics

This should be a really detailed explanation of linked lists, code is written in C as well
# 9  
Old 03-31-2011
Quote:
Originally Posted by omega666
if this makes a difference, the way i am doing the list is backwards, so when i want to add something to it, i create a new one in the function, then add the given one (which could be NULL or an actual list) to the next, then return the new one
It's not that. Your very first line of code crashed it -- it never had a chance to crash anywhere else Smilie

Your linked list function should actually work if you just set the first node to NULL. You don't need pointers to pass client_socket_fd though, just a plain int will be fine.
# 10  
Old 03-31-2011
ok i got it to work now
i did
Code:
    client *current;
    current = (client *)malloc(sizeof(client));
    current->next=NULL;
    current = add_client("tttttttt", current, &client_socket_fd);
    current = add_client("ttttttttt", current, &client_socket_fd);
    printf("HERE\n");
    write(1,current->client_name,9);
    printf("\n");
    write(1,current->next->client_name,8);
    printf("\nHERE\n");

Code:
client *add_client(char *username, client *head, int *client_socket_fd) {
    client *current = (client *)malloc(sizeof(client)); 
    current->client_name = username;
    current->client_socket_fd = *client_socket_fd;
    current->next = head;
    return current;
}


and the results are

HERE
ttttttttt
tttttttt
HERE

i dont suppose theres anything still wrong here?

---------- Post updated at 04:23 PM ---------- Previous update was at 04:17 PM ----------

hmm, the above is ok, but when i use it with this function
Code:
int already_there(client *current, char *username) {
    
    while(current) {
        if (strcmp(current->client_name, username)==0) return(1);
        current = current->next;
    }

    return(0);
}

i get a segmentation fault...
# 11  
Old 03-31-2011
Quote:
Originally Posted by omega666
ok i got it to work now
That's wrong. Now there's no way to tell when the list's empty, and have a garbage blank node at the end of your list that might print garbage or crash.

Could you try what I suggested before instead? Sometimes it feels like I'm talking to a wall here Smilie
# 12  
Old 03-31-2011
ok so now i have
Code:
    client *current, *head=NULL;
    current = add_client("tttttttt", current, client_socket_fd);
    current = add_client("ttttttttt", current, client_socket_fd);
    printf("HERE\n");
    write(1,current->client_name,9);
    printf("\n");
    write(1,current->next->client_name,8);
    printf("\nHERE\n");

i dont understand how i am supposed to use head here
Code:
client *add_client(char *username, client *head, int client_socket_fd) {
    client *current = (client *)malloc(sizeof(client)); 
    current->client_name = username;
    current->client_socket_fd = client_socket_fd;
    current->next = head;
    return current;
}

# 13  
Old 03-31-2011
The exact same way you were using it in the first place. Pointers don't do anything mystical. If you pass NULL into your add_client function, you'll get NULL in your add_client function. When you set current->next to it, you're setting it to NULL.

They're just variables. You can copy them around just like you would a float or an int or a structure. The only time pointers do anything special is when you use their contents with the -> operator, the * operator, or the [] operator.

Last edited by Corona688; 03-31-2011 at 05:45 PM..
# 14  
Old 03-31-2011
the use of head was from an example, but i don't understand why its being done, and why my function isn't working the same way (without using head)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to reverse a linked list by traversing only once.?

Program to reverse a linked list by traversing only once. (1 Reply)
Discussion started by: VSSajjan
1 Replies

2. Programming

How to delete the last node in a linked list.?

How to delete the last node in a single linked list given only the pointer to last node ? Head node will not be given. (5 Replies)
Discussion started by: VSSajjan
5 Replies

3. Programming

Help with linked list.

#include<stdio.h> #include<stdlib.h> struct LinkedList { int val; struct LinkedList *next; }node; /*Creating a structure variable*/ typedef struct LinkedList Node; Node *start = NULL; int create(int i) { Node *temp = NULL; if (start == NULL) ... (5 Replies)
Discussion started by: prinsh
5 Replies

4. UNIX for Advanced & Expert Users

Unix linked-list placement

Hi, I am programming in kernel, and I want to use a double linked list that holds infos that every process could access and modify THIS list. So, I suppose it is a 'global' variable since every process(thread) can reach it, I am wondering where to put it? by changing some of the kernel files? (1 Reply)
Discussion started by: louisTan
1 Replies

5. Programming

how to check if something exists in a struct linked list?

can someone provide an example of a struct linked list, where it has strings as its values, and then how do I check if a specific string (say called buffer) exists in the list of structs? i dont understand how to make a copy of it to check with this is what i have ... (0 Replies)
Discussion started by: omega666
0 Replies

6. Programming

How to check if something exists in linked list in C?

i have a linked list set up like typedef struct client_list { char *client_name; int client_socket_fd; struct client_list *next; } client; client *client_list=NULL; before adding to the list i check if it already exists, only if it does not then i add if (client_list==NULL... (1 Reply)
Discussion started by: omega666
1 Replies

7. Programming

I need C++ Code for single linked list

I need C++ Code for single linked list With operations as 1)insert at any position 2)delete any 3)change the data of any position (2 Replies)
Discussion started by: girija
2 Replies

8. Programming

shared memory with linked list??

is this possible, if so plz please share with me.. Correct English please, not Cyber-/Leetspeak (11 Replies)
Discussion started by: vijay_manpage
11 Replies

9. UNIX for Dummies Questions & Answers

List linked files

A perl script that displays the list of files which have multiple links..! ls -l shows number of links in a field. (0 Replies)
Discussion started by: aadi_uni
0 Replies

10. Programming

Reverse single linked list

Can any one help me in reversing the single linked list and at the same time i want to print the reversed links. (2 Replies)
Discussion started by: dhanamurthy
2 Replies
Login or Register to Ask a Question