Thread: linked list stack question

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    linked list stack question

    A more specific question: Why isn't my program pushing and poping correctly?
    Stack program: Develop a stack class using a linked list representation and string data and use it in a program that check C++ programs for the balancing symbols (/* */, ( ), [ ], and { }). The program should read in a C++ program and output one of three messages.

    Case 1: Symbol1 does not match symbol2.
    Example: “(” does not match “*/”.
    Case 2: End of program reached with unmatched symbol.
    Example: End of program reached with unmatched “{”.
    Case 3: All symbols correctly balanced.

    Read the name of the program file to be check from the command line.


    Input file:[] / * */ ( ) [] / * */ ( ) ( ]
    [CODE]
    #include<iostream>
    #include<fstream>
    #include<iomanip>
    #include<cstdlib>
    #include<string>
    #include"StackLLImple.cpp"

    using namespace std;

    int main()
    {
    char answer;
    //char InputChar1, InputChar2;
    int num;
    int success=0;
    string InputChar1;
    string InputChar2;

    character link;

    ifstream InFile;
    ofstream OutFile;

    InFile.open("input.txt");
    OutFile.open("output2.txt");

    OutFile << "This is the the linked list function" << endl;
    OutFile<< "\n \n";

    InFile>>InputChar1;
    InFile>>InputChar2;
    while(InFile != NULL)
    {


    if((InputChar1 == "[") || (InputChar2 == "]"))
    {
    success = link.push(num, success);
    success = link.push(num, success);
    }
    else if((InputChar1 == "(") || (InputChar2 == ")"))
    {
    success = link.push(num, success);
    }
    else if((InputChar1 == "/*") || (InputChar2 == "*/"))
    {
    success = link.push(num, success);
    }
    else //if(InputChar1 != InputChar2)

    success=link.pop(num, success);

    OutFile<<InputChar1;
    OutFile<<InputChar2;

    InFile>>InputChar1;
    InFile>>InputChar2;

    }
    return 0;
    }

    #include<iostream.h>
    #include"stackLL.h"

    //constructor
    character::character()
    {
    head=NULL;
    cur=NULL;
    pre=NULL;
    }

    //destructor
    character::~character()
    {
    node *ptr;

    while(head!=NULL) //delete all the numbers
    {
    ptr=head->next;
    delete head;
    head=ptr;
    }
    }

    int character:ush(int num, int success)
    {

    if(head==NULL)
    {
    head=new node;
    head->num=num;
    head->next=NULL;
    success=1;
    }
    else
    {
    cur=head;
    head=NULL;
    head=new node;
    head->num=num;
    head->next=cur;
    success=1;
    }

    return success;

    }


    //****remove at the head, this is stack, push at the head remove at the head.
    int characterp(int num, int success)
    {
    node *temp; //make a temp pointer

    if(head!=NULL) //if the list is not empty
    {
    temp=head->next; //temp equal to head ->next
    delete head; //delete head, remove the first data of the list
    head=temp; //now the second data of the list become the first data of the list
    return success=1; //return success
    }

    else
    return success=0; //if the list empty return fail

    return success;
    }


    //this function will print from the beginning of the list to the end of the list
    void character:rint()
    {
    //if the list was empty print out an error message
    if(head==NULL)
    {
    cout << "There isn't any number" << endl;
    return;
    }

    //else while not the end of the list, print out the number and go to the next one
    else
    {
    cur=head;
    while(cur!=NULL)
    {
    cout << "number: " << cur->num << endl;
    cur=cur->next;
    }
    }
    }


    //the search function was search from the beginning of the head to the end of the
    //head.
    int character::search(int num, int success)
    {

    if(head==NULL) //if there isn't any data return 0
    {
    success=0;
    return 0;
    }

    if(head->num == num) //if delete the first node
    {
    cout << num << " was found" << endl;
    success=1;
    return 1;
    }
    else //else delete any node other than the first node
    {
    cur=head;
    cur=cur->next;


    while(cur!=NULL) //search all the to the end of the list
    {

    if(cur->num ==num)
    {
    cout << num << " was found" << endl;
    success=1;

    return 1;
    }
    pre=cur;
    cur=cur->next;
    success=0;
    }// while
    } //else
    return success;
    }
    struct node
    {
    int num;
    node *next;
    };

    class character
    {
    public:
    character();
    ~character();

    int push(int num, int success);
    int pop(int num, int success);
    void print();
    int search(int num, int success);

    private:
    node *head;
    node *cur;
    node *pre;
    };
    [/NODE]

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why isn't my program pushing and poping correctly?
    Does the stack library push and pop correctly when you test it independently? If so, does your input match what you expect it to? 1) Your stack may be broken, make sure it works before using it. 2) You may not be reading the file like you think you are. Pushing one character at a time yet having a potential two character token is asking for trouble.

    Your code has a lot of problems, not the least of which is a lack of correct code tags to make it easy to follow. I suggest going over it with a fine tooth comb and clear out the bugs, or rewrite from scratch and be more careful. Here's a sample (ugly, granted) stack that you can use to see if your list logic is correct:
    Code:
    /*
     * Disclaimer:
     *   All unique code supplied in this post is given as-is.
     *   The author of this program is not responsible for any
     *   errors, warnings, inconsistencies, or general lack of
     *   usefulness concerning the actual problem in question.
     *
     *   The following code is given as an example only. Any
     *   resemblance to the complete solution of the given
     *   problem is purely coincidence.
     */
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct node {
      string token;
      node *next;
      node(string item): token(item), next(0) {}
    };
    
    class myStack {
      node *top;
    public:
      myStack(): top(0) {}
      void push(node *link)
      {
        if (link != 0) {
          link->next = top;
          top = link;
        }
      }
      void pop()
      {
        if (top != 0)
          top = top->next;
      }
      node *gettop()
      {
        return top;
      }
      bool empty()
      {
        return top == 0;
      }
    };
    
    int main()
    {
      myStack stack;
      node *n;
    
      stack.push(new node("a"));
      stack.push(new node("b"));
      stack.push(new node("c"));
      while (!stack.empty()) {
        n = stack.gettop();
        stack.pop();
        cout<< n->token <<endl;
        delete n;
      }
    }
    Last edited by Prelude; 09-11-2003 at 05:09 AM.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    As usual Prelude's advise is excellent.

    To make things easier for the reviewers who might be willing to tackle your code as where it stands, go into your first post and edit the last line by removing the word NODE and using the word code, instead.

    Then, when you have your stack working correctly put the declaration for node and character in an file with .h extension to create a header file and the method definitions in a .cpp file. Then using the exact name of the header file you create list it in the include section for the program where you want to declare a stack object.


    Code:
    ///////////////
    //stackList.h
    ///////////////
    #include <iostream>
    struct node
    {
      //...
    };
    
    class character
    {
      //...
    };
    
    ///////////////
    //stackList.cpp
    ///////////////
    #include "stackList.h"
    stackList::stackList()
    {
      //....
    }
    
    void stackList::push(char * input)
    {
      //....
    }
    
    char * stackList::pop()
    {
      //...
    }
    
    //etc.
    
    
    ///////////////
    //myProgram
    ///////////////
    #include <iostream>
    #include "stackList.h"
    //etc.
    Alternatively, if you don't need the modularization afforded by this technique you can just keep building your program from the stack program once you have it working.

    Code:
    ////////////
    //myProgram
    ////////////
    struct node
    {
       //...
    };
    
    class structList
    { 
      //....
    };
    
    ///method definitions go here or after main--your choice
    
    int main()
    //etc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  5. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM