Thread: Linked Lists please help!!!

  1. #16
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    Hi everyone! I am here again. I need your help. Actually i have a problem in linked lists. The exercise says to make a program with linked lists which will tell the user if an equation is true or false, and by true or false i mean that the program must check if the precedence of operations is true or false. For example when the user puts this : (a+10)*((c+d)/(x-5))) + (7*3) the program will say it's true and if he puts this : (a+10)*((c+d)/(x-5))) + 7*3) the program will say it's wrong. The exercise must be done with linked lists. Does anyone knows how i am supposed to start it??? I know a way but i can't make my program work. I appreciate every small tip.

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ValL View Post
    The exercise says to make a program with linked lists which will tell the user if an equation is true or false, and by true or false i mean that the program must check if the precedence of operations is true or false.
    I don't understand that part. How can the precedence be true or false? The precedence of an operation is defined somewhere, thus it is always true.

    Quote Originally Posted by ValL View Post
    For example when the user puts this : (a+10)*((c+d)/(x-5))) + (7*3) the program will say it's true and if he puts this : (a+10)*((c+d)/(x-5))) + 7*3) the program will say it's wrong.
    IMHO both examples are wrong because both have mismatching parentheses.

    I find your description confusing.

    Bye, Andreas

  3. #18
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    I mean if the user forget to put a parenthesis then the equation will be false... i mean this (a+10)*((c+d)/(x-5))) + (7*3) is different from this (a+10)*((c+d)/(x-5))) + 7*3)

  4. #19
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ValL View Post
    I mean if the user forget to put a parenthesis then the equation will be false... i mean this (a+10)*((c+d)/(x-5))) + (7*3) is different from this (a+10)*((c+d)/(x-5))) + 7*3)
    But both are still wrong.

    You could add a node for each opening parenthesis and remove a node for each closing one, i.e. implement a stack with a linked list.
    If the list is empty after reading the expression, it's ok.

    Bye, Andreas

  5. #20
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    mmm i think you are right but the teachers in the exercise the give it like told you before. Maybe they forgot a parenthesis in the beginning of the first equation. Thanks by the way for your help!!

  6. #21
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    Let me ask you something more. First the user gives the equation, then if the parenthesis is ( it puts it in the list and if the parenthesis is ) it removes it from the list. But the think i do not understand is how the program will choose to take only the parenthesis and check them and not the numbers and the symbols from the equation?

  7. #22
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Quote Originally Posted by ValL View Post
    Let me ask you something more. First the user gives the equation, then if the parenthesis is ( it puts it in the list and if the parenthesis is ) it removes it from the list. But the think i do not understand is how the program will choose to take only the parenthesis and check them and not the numbers and the symbols from the equation?
    why not use a stack?

  8. #23
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    I don't know because the teachers said, make the exercise with linked lists Is it difficult to make it work with lists??

  9. #24
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    Does anyone Knows how and where to download matlab?? I need it for an exercise in school and i can't find it anywhere :/

  10. #25
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by ValL View Post
    Does anyone Knows how and where to download matlab?? I need it for an exercise in school and i can't find it anywhere :/
    The student version of Matlab costs $100.00 in the USA; asking to get a illegal copy might violate this site rules.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #26
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    Ok and sorry i didn't know that. By the way can someone tell me how i can make the above work? I tried it many times but nothing. I mean how the program will know how to choose only the parenthesis and put them in the linked list and not the numbers and the symbols??

  12. #27
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Scilab and Octave are open source alternatives to Matlab.

    Octave is closer to being a Matlab Clone.
    GNU Octave - Wikipedia, the free encyclopedia

    FreeMat is something I just found out about; no idea how it compares to the other two.
    http://en.wikipedia.org/wiki/FreeMat

    Can't really help with the programming question; it is not something I can understand today.

    Tim S.
    Last edited by stahta01; 11-19-2012 at 01:12 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #28
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ValL View Post
    By the way can someone tell me how i can make the above work? I tried it many times but nothing. I mean how the program will know how to choose only the parenthesis and put them in the linked list and not the numbers and the symbols??
    I guess you get the equation from the user as a string, right?

    So just walk through the string character by character and when you come across an opening parenthesis add it to your list and if you come across a closing parenthesis remove the open parenthesis from the list.

    Can you show us the code you have tried?

    Bye, Andreas

  14. #29
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    Hi again!! Here is what i have tried so far... I sent it as you told me and i please want to tell me if you can where are my mistakes and explain them to try and fix them.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Node
    {
        char Data;
        struct Node *Next;
    }*Head;
    
    
    void insert(char equation) //start of insert function
    {
      int length = strlen(equation); //Here i am trying to find the length of the equation, so i don't know if is the wright way to put it
      int i;
      struct Node *new_node, *old_node;
    
      new_node = (struct Node *)malloc(sizeof(struct Node));
    
      for(i=0; i<length; i++)
      {
        if(equation == '(' )
        {
            new_node->Data=equation;
        }
        else if(equation == ')' )
        {
            Delete();
        }
      }
    
      old_node = Head;
    
      if(Head == NULL)
      {
          printf("The equation is correct");
          printf("\n");
          Head=new_node;
          Head->Next=NULL;
      }
      else
      {
         printf("The equation is not correct");
         printf("\n");
         while(old_node->Next != NULL)
         old_node = old_node->Next;
    
         new_node->Next = NULL;
         old_node->Next = new_node;
      }
    I know that if the list is empty i have to tell to user that the equation is correct and if the list is not empty that the equation is not correct but i don't know if i only have to print that messages or continue to the next node too.
    }
    
    void Delete()//start of delete function i don't know here if my function is correct or is false so i need your help suriously... 
    { 
        struct Node *last;
        last = (struct Node *)malloc(sizeof(struct Node));
        last = Head;
    
        struct Node *previous_last;
        previous_last = (struct Node *)malloc(sizeof (struct Node));
    
        while(last->Next != NULL)
        {
            previous_last = last;
            last = previous_last->Next;
        }
        previous_last->Next = NULL;
        free(last);
        printf("The element has been deleted \n");
        printf("\n");
    
    }
    
    int main()
    {
        char eq;
        Head = NULL;
    
        printf("Please give an equation: ");
        scanf("%s", &eq);
        printf("\n");
        insert(eq);
    
    return 0;
    system ("pause");
    }
    Thanx for your understanding
    Last edited by ValL; 11-19-2012 at 05:47 PM.

  15. #30
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    Anyone????

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  4. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM
  5. Linked List of Linked lists Revisited.
    By Qui in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2004, 09:45 PM