I'm new to C and I i have an assignment where I have to build a dictionary (Linked List). Basically the user inputs several words,year and their definitio likn this:

Example:

love_#2004#_LOVING

trade_#_2001_#_INVEST
etc...

Now what I need is to have a function that will scan the definition which is in UPPER CASE and give me back the word it relates to.

Example:
What word would you like to search?
Input: INVEST
Output: Found "INVEST" in the word "trade".

I was kind of answered to that question. It helped understanding what was done but it's unfortunately not enough.
The string i'm inputting is a full string that takes(word)__#__(year)__#__(DEFINITION)

I have an idea but i don't know how to code it exactly as I want it. But let me know what you think on the idea as well.

since every section is seperated by the "#" why not have a pointer to every word of my string before the "#" and print it seperately?

Example: trade_#_2001_#_INVEST
"INVEST" and "2001" are pointing to "trade"

Any ideas?

Recommended Answers

All 16 Replies

just a minute editing to my post

can anybody help?
would really appreciate it

How was my previous answer to this question insufficient?

because it doesn't help research the word since i'm getting only one strin. In your case which was very helpful to learnyou assigned 3 different charsfrom what i'm guessing and trying to understand i need a pointer to point to the word of my string.

Ex:
Enter a 3 words in the disctionary:
Input: love_#_2001_#_AFFECTION
Input: trade_#_2005_#_INVEST
Input: pleased_#_1997_#_SATISFIED

Enter a definition to search:
Input: INVEST
Output: Found "INVEST" in the word "trade"

because it doesn't help research the word since i'm getting only one strin

Your example contradicts what you're saying. Each line in the dictionary is a single defintion (ie. one string). The search word is only one string. I'm guessing that you simply don't know how to incorporate the snippet I gave you into the framework you have, but it's difficult to help when all you do is say it doesn't work and ask the same question again.

Please post all of your code as it stands right now.

I'm not saying your code doesn't work at all please forgive me if you understood otherwise. Your code is awsome. Let megather better explanationfor you and BTW i really appreciate your help.
Sorry for any misunderstanding.

Okay so here's the issue and your code is actually key for it.
I have my struct as you saw before. I'll paste is here:

struct node
{
    char data[100];
    struct node *previous;  // Points to the previous node
    struct node *next;   // Points out to the next node
}*head, *last;

and in your code it seperate my string based on the "_#_"

So let's say i have :

Enter a 3 words in the disctionary:
Input: love_#_2001_#_AFFECTION
Input: trade_#_2005_#_INVEST
Input: pleased_#_1997_#_SATISFIED

It should give me:
love 2001 AFFECTION
trade 2005 INVEST
pleased 1997 SATISFIED

Now in my function I want to be able to search a DEFINITION and print the word it relates to.
Exactly like the above posts.
Is that better explained? Hope it is please let me know

I'm still not seeing the issue. It should be a straightforward traversal of your list (assuming it->def is a definition instance as I showed in your other thread):

printf("Input: ");

if (fgets(line, sizeof line, stdin) != NULL) {
    struct node *it;

    line[strcspn(line, "\n")] = '\0'; // Trim the newline, if any

    for (it = head; it->next != NULL; it = it->next) {
        if (strcmp(line, it->def.value) == 0) {
            printf("Found '%s' in the word '%s'\n", line, it->def.key);
        }
    }
}

would you prefer i paste my whole code and tell you exactly where i need this function?

//
//  main.c
//  Double Linked List
//
//  Created by Yann Bohbot on 12/22/13.
//  Copyright (c) 2013 Yann Bohbot. All rights reserved.
//

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

struct node
{
    char data[100];
    struct node *previous;  // Points to the previous node
    struct node *next;   // Points out to the next node
}*head, *last;

void insert_beginning(char words[99]);
void insert_end(char words[99]);
char insert_after(char words[99], char loc[99]);
char delete_from_end();
char delete_from_middle(char words[99]);
void display();
struct node *merge(struct node *head_one, struct node *head_two);




void insert_beginning(char words[99])
{
    struct node *var, *temp;
    var=(struct node *)malloc(sizeof(struct node)); //explination about the (node *)
    strncpy(var->data, words,99);

    if (head==NULL)
    {
        head=var;
        head->previous=NULL;
        head->next=NULL;
        last=head;
    }
    else
    {
        temp=var;
        temp->previous=NULL;
        temp->next=head;
        head->previous=temp;
        head=temp;
    }
}

void insert_end(char words[99])
{
    struct node *var, *temp;
    var=(struct node *)malloc(sizeof(struct node));
    strncpy(var->data, words,99);

    if (head==NULL)
    {
        head=var;
        head->previous=NULL;
        head->next=NULL;
        last=head;
    }
    else
    {
        last=head;
        while (last!=NULL)
        {
            temp=last;
            last=last->next;
        }
        last=var;
        temp->next=last;
        last->previous=temp;
        last->next=NULL;
    }
}

char insert_after(char *words, char *loc)
{
    struct node *temp, *var;
    var=(struct node *)malloc(sizeof(struct node));
    strcpy(var->data, loc);

    if (head==NULL)
    {
        head=var;
        head->previous=NULL;
        head->next=NULL;

    }
    else
    {
        temp=head;
        while ((temp!=NULL) && (strcmp(temp->data, words)))
        {
            temp=temp->next;
        }
        if (temp==NULL)
        {
            printf("\n %s not presented at list\n", words);
        }
        else
        {
            struct node * followingNode = temp->next;
            temp->next=var;
            var->previous=temp;
            var->next= followingNode;
            if (followingNode)
                followingNode->previous = var;
        }
    }
    return 0;
}

char delete_from_end()
{
    struct node *temp;
    temp=last;

    if (temp->previous==NULL)
    {
        free(temp);
        head=NULL;
        last=NULL;
        return 0;
    }

    printf("\n Data deleted from list %s\n", last->data);
    last=temp->previous;
    last->next=NULL;
    free(temp);
    return 0;
}

char delete_from_middle(char words[99])
{
    struct node *h;
    h = head;
    while ( h != last)   {
        if ( strcmp(h->data, words) == 0)
        {
            if ( h->previous == NULL )
            {
                if( head == last )
                {
                    head = NULL;
                    last = NULL;
                }
                else
                {
                    head = head->next;
                    head->previous = NULL;

                }
            }
            else
            {
                if( h->next!=NULL ) h->next->previous = h->previous;
                h->previous->next = h->next;
            }
            printf(" Data deleted from list is %s \n", words);
            free(h);
            return 0;
        }
        h = h->next;
    }
    printf(" Data not found");
    return 0;  }



void display()
{
    struct node *temp;
    temp=head;

    if(temp==NULL)
    {
        printf("List is Empty");
    }

    while(temp!=NULL)
    {
        printf("-> %s ",temp->data);
        temp=temp->next;
    }
}



/* preform merge sort on the linked list */
struct node *merge_sort(struct node *head)
{
    struct node *head_one;
    struct node *head_two;

    if((head == NULL) || (head->next == NULL))
        return head;

    head_one = head;
    head_two = head->next;

    while((head_two != NULL) && (head_two->next != NULL))
    {
        head = head->next;
        head_two = head->next->next;
    }

    head_two = head->next;
    head->next = NULL;

    return merge(merge_sort(head_one), merge_sort(head_two));
}

/* merge the lists.. */
struct node *merge(struct node *head_one, struct node *head_two)
{
    struct node *head_three;

    if(head_one == NULL)
        return head_two;

    if(head_two == NULL)
        return head_one;

    if(strcmp(head_one->data, head_two->data)<0)
    {
        head_three = head_one;
        head_three->next = merge(head_one->next, head_two);
    }
    else
    {
        head_three = head_two;
        head_three->next = merge(head_one, head_two->next);
    }

    return head_three;
}

int main()
{
    char loc[99];
    char words[99];
    int i, dat;

    head=NULL;

    printf("Select the choice of operation on link list");
    printf("\n1.) Insert At Begning\n2.) Insert At End\n3.) Insert At Middle");
    printf("\n4.) Delete From End\n5.) Reverse The Link List\n6.) Display List\n7.)Exit");

    while(1)
    {
        printf("\n\n Enter the choice of operation you want to do ");
        scanf("%d",&i);

        switch(i)
        {
            case 1:
            {
                printf("Enter a word you want to insert in the 1st node ");
                scanf(" %s",words);

                insert_beginning(words);
                display();
                break;
            }
            case 2:
            {
                printf("Enter a word you want to insert in the last node ");
                scanf(" %s",words);
                insert_end(words);
                display();
                break;
            }
            case 3:
            {
                printf("After which data you want to insert your new data ");
                scanf(" %s",words);

                printf("Enter the data you want to insert in list ");
                scanf(" %s",loc);

                insert_after(words, loc);
                display();
                break;
            }
            case 4:
            {
                delete_from_end();
                display();
                break;
            }
            case 5:
            {
                printf("Enter the value you want to delete");
                scanf(" %s",words);
                delete_from_middle(words);
                display();
                break;
            }
            case 6 :
            {
                display();
                break;
            }

            case 7 :
            {
                exit(0);
                break;
            }
            case 8:
            {
                head = merge_sort(head);
                display();
                break;
            }
        }
    }
    printf("\n\n%s",last->data);
    display();
    scanf("%d", &dat);
}

Have a look at my code. Let me try and re-explain what I need.
When I'm entering a word to be stored in my list I want to be able to have a function that scans the definition i'm inputting and gives me back the word it relates to thats all.

Any help?

You'll have to be more patient, please wait more than simply 4 hours before bumping like that. Give it at least 24 hours, as some people aren't in the same timezone as you.

sorry guys i'll also try to work on it.
Thanks for your help

Hey any news about my post?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.