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


 
Thread Tools Search this Thread
Top Forums Programming How to check if something exists in linked list in C?
# 1  
Old 04-02-2011
How to check if something exists in linked list in C?

i have a linked list set up like
Code:
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
Code:
if (client_list==NULL || already_there(client_list, buffer)==0) {
    client_list = add_client(buffer, client_list, i);
}

Code:
int already_there(client *client_list, char *username) {
    if (client_list == NULL) return(0);
    else if (strcmp(client_list->client_name, username)==0) return(1);
    else return already_there(client_list->next, username);
}

client *add_client(char *username, client *client_list, int client_socket_fd) {
    client *new_client = (client *)malloc(sizeof(client)); 
    new_client->client_name = username;
    new_client->client_socket_fd = client_socket_fd;
    new_client->next = client_list;
    return new_client;
}

it adds the client the first time, but then the next time, always detects that the new client is already there...
somehow strcmp is always resulting true after the first time...

Last edited by omega666; 04-02-2011 at 03:13 PM..
# 2  
Old 04-02-2011
What's the original source of the client name you're passing in? Are you reusing that buffer?

Because your "add_client()" code never makes a copy of that string, it just uses the address of the string that gets passed in. If you reuse the original string, you'll change the data in the node on the linked list.
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. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

6. 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

7. Programming

Help with linked list in C

i have this code typedef struct client_list { char *client_name; struct client_list * next; int client_socket_fd; } client; client *current, *head; head = NULL; char *h="test"; add_client(current, h, head, &client_socket_fd); ... (24 Replies)
Discussion started by: omega666
24 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