P3druh77
P3druh77

Reputation: 37

Compare using strcmp on linked-list

I'm not a great understanding on linked-list, i don't know if it's possible, but i need to do it :) I have a linked list that are load to the struct, and i need to compare a all the chars on the struct....It's better with an example:

This is without linked lists

struct

typedef struct x{
char name[100];
}x;

typedef x Info;

typdef struct Elem{
Info node;
struct Elem*next;
}Element;


  for(i=0;i<100;i++){
  if(strcmp(a.name[i],a.name[i+1])==0){
  printf("Same name\n");
  }
  }
  else
  printf("Diff name\n");

Now i need to do something like this but with linked-list

Upvotes: 0

Views: 10585

Answers (3)

antonakos
antonakos

Reputation: 8361

Here is a program that traverses the linked list and compares the names of adjacent elements. I have taken the liberty of renaming a couple of things, but otherwise the code for the data structures is the same as yours.

#include <string.h>
#include <stdio.h>
#include <assert.h>

typedef struct Info_ {
    char name[100];
} Info;

typedef struct Element_ {
    Info info;
    struct Element_* next;
} Element;

void print_comparisons(Element* elm)
{
    assert(elm);

    Element* cur = elm;
    Element* next = cur->next;
    for (; next; cur = next, next = next->next) {
        if (strcmp(cur->info.name, next->info.name) == 0)
            printf("Same name\n");
        else
            printf("Diff name\n");
    }
}

int main()
{
    Info a; a.name[0] = 'A'; a.name[1] = '\0';
    Info b; b.name[0] = 'B'; b.name[1] = '\0';
    Info c; c.name[0] = 'B'; c.name[1] = '\0';
    Info d; d.name[0] = 'D'; d.name[1] = '\0';
    Element na; na.info = a;
    Element nb; nb.info = b;
    Element nc; nc.info = c;
    Element nd; nd.info = d;

    na.next = &nb;
    nb.next = &nc;
    nc.next = &nd;
    nd.next = NULL;

    print_comparisons(&na);
}

The output of the program:

Diff name
Same name
Diff name

Upvotes: 0

codd
codd

Reputation: 612

First of all: int strcmp ( const char * str1, const char * str2 ) compares two C-strings (char pointers). This means that a.name[i] should be a char pointer and not a char! Make sure this is the case (i.e. make sure a.name is an array of c-string arrays, and not an array of chars).

Secondly, if the previous is the case, your code will only compare string i with string i+1. It will not compare all strings with each other.

In any case, it looks like you are not doing whatever it is you want to do the right way. I'm guessing you want a struct that is defined like this:

struct example {
    char * name;
    // other members of choice
    example * next;
}

A placeholder for a name, other members, and a next pointer to enable the linked list data type. That way you can compare names with:

while (list->next != 0 && list->next->next != 0) {
    if (strcmp(list->name, list->next->name) == 0) // do something;
    else // do something else;
}

or with a double loop if you want to compare all strings with each other.

Upvotes: 1

Ayush Sood
Ayush Sood

Reputation: 63

So the first thing you need to do is understand the fundamentals of linked-list. You can read in detail here: http://www.codeproject.com/KB/cpp/linked_list.aspx

NOTE: You really can't undersand linked lists until you understand pointers. http://www.cplusplus.com/doc/tutorial/pointers/

Essentially a linked-list is composed of numerous "nodes" that link to each other. At a minimum each node will have two pieces of data, one being the data (in your case a character) and the other being a pointer to the next node in the list.

Defining a struct would look like (in pseudocode):

LinkedList nodeT {
    char *c; //NOTE: strcmp only compares pointers to chars
    nodeT *nextNode;
}

You would have a pointer to the first node of the linked list. Something like:

nodeT *firstElement;

Then cycling through the entire list is a piece of cake:

nodeT *curElement = firstElement;
while(curElement->next != NULL) { //assuming the last node's next pointer is NULL
    if(strcmp(curElement->c,curElement->next->c)==0){
        printf("Same name\n");
    } else {
         printf("Diff name\n");
    }
    curElement = curElement->nextNode;
}

But again, to understand this you need to understand the fundamentals of pointers.

Upvotes: 0

Related Questions