stacks, ques and linked list in C

katahari puluwanda C language eken stacks,ques and linked list define karanne kohamada kiyala mata pennanna.
loku udawwak
Declaration eka tibbama athi.
poddak terena widiyata dapalla
Thanks


P.S
Katahari kiyanna puluwanda C programming hoda mona wage applications walatada kiyala. Examples ekka.
Ewagema java wage OOP language ekak hoda mona wage applications walatada.Examples ekka.
Link ekak thiyenawanm diyanko. Ekath loku udawwak
 
Last edited:

san**

Well-known member
  • Feb 21, 2012
    5,418
    8,368
    113
    kandy
    Defining singly linked list



    #include <iostream.h>
    #include <conio.h>
    struct NodeType
    {
    int number;
    NodeType *next;
    };
    int main()
    {
    NodeType *head, //pointer to point to the first node
    *current, //pointer to keep track of pos in list
    *tmp; //pointer used to display list
    char answer;
    head = new NodeType; //create the first node & make
    current = head; //head and current point to it
    (*current).next = NULL;
    clrscr();
    do //populate the list
    {
    cout << "Enter an integer ";
    cin >> (*current).number;
    cout << "add another node? ";
    cin >> answer;
    if(answer == 'Y' || answer == 'y')
    {
    (*current).next = new NodeType; //create new node
    current = (*current).next;
    (*current).next = NULL; //make last node the tail
    }
    }while(answer == 'Y' || answer == 'y');
    tmp = head;
    while (tmp != NULL) //display the list
    {
    cout << (*tmp).number << " ";
    tmp = (*tmp).next;
    }
    return 0;
    }




    stacks

    # include <iostream.h>

    const int MAX_STACK_SIZE = 5;

    class Stack
    {
    public:
    Stack(); // default constructor
    ~Stack(); // destructor
    bool isEmpty();
    bool isFull();
    Type top();
    void push(Type n);
    void pop();
    int size();
    private:
    struct Node
    {
    Type data;
    Node* next;
    }
    Node* currPtr;
    int sizeOf; // number of items
    }



    Stack:Stack() // constructor
    {
    sizeOf = 0;
    }



    Stack::~Stack() // destructor
    {
    while ( !( isEmpty() ) )
    {
    pop();
    }
    }



    bool Stack::isEmpty()
    {
    return ( sizeOf == 0 );
    }




    bool Stack::isFull()
    {
    return ( sizeOf >= MAX_STACK_SIZE );
    }



    Stack::top()
    {
    assert ( !isEmpty() );
    return (*currPtr).data;
    }



    int Stack::size()
    {
    return sizeOf;
    }



    void Stack::push( Type n )
    {
    assert ( !isFull() );
    Node* newItem = new Node;
    (*newItem).data = n;
    (*newItem).next = currPtr;
    currPtr = newItem;
    sizeOf++;
    }



    void Stack::pop()
    {
    assert ( !isEmpty() );
    Node* temp = currPtr;
    currPtr = (*temp).next;
    delete temp;
    sizeOf--;
    }





    void display ( Stack& s )
    {
    Stack temp;
    while( (!s.isEmpty() ) )
    {
    cout << s.top() <<‘,’ ;
    temp.push( s.pop() );
    s.pop();
    }

    while( (!temp.isEmpty() ) )
    {
    s.push( temp.pop() );
    temp.pop();
    }
    }



    void main()
    {
    Stack myIntStack;
    int num = 1;

    while( !(myStack.isFull() ) )
    {
    cout << “Pushing “ << num << <<myIntStack.push(num);
    num++;
    display(myIntStack);
    cout << endl;
    }

    while( !(myIntStack.isEmpty() ) )
    {
    cout << “Popping “ << myIntStack.top();
    myIntStack.pop();
    cout << “ : ” ;
    display(myIntStack);
    cout << endl;
    }
    }




    Queues

    class Queue
    {
    private:
    struct NameRec
    {
    char name[MAXCHARS];
    NameRec *nextaddr;
    };
    NameRec *queue_in;
    NameRec *queue out;
    public:
    Queue(void);
    void enqueue(char *);
    char *dequeue(void);
    int isEmpty(void);
    };


    Queue::Queue(void)
    {
    queue_in = NULL;
    queue_out = NULL;
    }


    void Queue::enqueue(char *newname)
    {
    NameRec *new_addr;
    new_addr = new(NameRec);
    if (new_addr==NULL) {
    cout << “No more memory” << endl;
    else {
    strcpy(new_addr->name, newname);
    new_addr->nextaddr = NULL;
    if (queue_in!=NULL)
    queue_in->nextaddr = new_addr;
    if (queue_out==NULL)
    queue_in = new_addr;
    }
    return;
    }


    char *Queue::dequeue(void)
    {
    NameRec *temp_addr;
    char name[MAXCHARS];
    strcpy(name, queue_out->name);
    temp_addr = queue_out->nextaddr;
    delete(queue_out);
    queue_out = temp_addr;
    return name;
    }



    int Queue::isEmpty(void)
    {
    if (queue_out ==NULL)
    return true;
    else
    return false;
    }


    int main()
    {
    char Newname[MAXCHARS];
    Queue namequeue;
    cin.getline(newname,MAXCHARS);
    namequeue.enqueue(newname);
    ….
    while (!namequeue.isEmpty())
    {
    strcpy(newname, namequeue.dequeue());
    cout << newname << endl;
    }
    return 0;
    }


    language-C++
     
    Last edited:
    machan mata nam hariyata therenne na oya mokadda kiyala
    lesson eka, watapitawe wisthara kiwwoth nam puluwan widihata help 1k denna puluwan wei

    anyway meka balanna
    http://my.safaribooksonline.com/book/programming/c/1565924533/data-structures/ch06-1-fm2xml
    Thanks machan !!!
    Defining singly linked list



    #include <iostream.h>
    #include <conio.h>
    struct NodeType
    {
    int number;
    NodeType *next;
    };
    int main()
    {
    NodeType *head, //pointer to point to the first node
    *current, //pointer to keep track of pos in list
    *tmp; //pointer used to display list
    char answer;
    head = new NodeType; //create the first node & make
    current = head; //head and current point to it
    (*current).next = NULL;
    clrscr();
    do //populate the list
    {
    cout << "Enter an integer ";
    cin >> (*current).number;
    cout << "add another node? ";
    cin >> answer;
    if(answer == 'Y' || answer == 'y')
    {
    (*current).next = new NodeType; //create new node
    current = (*current).next;
    (*current).next = NULL; //make last node the tail
    }
    }while(answer == 'Y' || answer == 'y');
    tmp = head;
    while (tmp != NULL) //display the list
    {
    cout << (*tmp).number << " ";
    tmp = (*tmp).next;
    }
    return 0;
    }




    stacks

    # include <iostream.h>

    const int MAX_STACK_SIZE = 5;

    class Stack
    {
    public:
    Stack(); // default constructor
    ~Stack(); // destructor
    bool isEmpty();
    bool isFull();
    Type top();
    void push(Type n);
    void pop();
    int size();
    private:
    struct Node
    {
    Type data;
    Node* next;
    }
    Node* currPtr;
    int sizeOf; // number of items
    }



    Stack:Stack() // constructor
    {
    sizeOf = 0;
    }



    Stack::~Stack() // destructor
    {
    while ( !( isEmpty() ) )
    {
    pop();
    }
    }



    bool Stack::isEmpty()
    {
    return ( sizeOf == 0 );
    }




    bool Stack::isFull()
    {
    return ( sizeOf >= MAX_STACK_SIZE );
    }



    Stack::top()
    {
    assert ( !isEmpty() );
    return (*currPtr).data;
    }



    int Stack::size()
    {
    return sizeOf;
    }



    void Stack::push( Type n )
    {
    assert ( !isFull() );
    Node* newItem = new Node;
    (*newItem).data = n;
    (*newItem).next = currPtr;
    currPtr = newItem;
    sizeOf++;
    }



    void Stack::pop()
    {
    assert ( !isEmpty() );
    Node* temp = currPtr;
    currPtr = (*temp).next;
    delete temp;
    sizeOf--;
    }





    void display ( Stack& s )
    {
    Stack temp;
    while( (!s.isEmpty() ) )
    {
    cout << s.top() <<‘,’ ;
    temp.push( s.pop() );
    s.pop();
    }

    while( (!temp.isEmpty() ) )
    {
    s.push( temp.pop() );
    temp.pop();
    }
    }



    void main()
    {
    Stack myIntStack;
    int num = 1;

    while( !(myStack.isFull() ) )
    {
    cout << “Pushing “ << num << <<myIntStack.push(num);
    num++;
    display(myIntStack);
    cout << endl;
    }

    while( !(myIntStack.isEmpty() ) )
    {
    cout << “Popping “ << myIntStack.top();
    myIntStack.pop();
    cout << “ : ” ;
    display(myIntStack);
    cout << endl;
    }
    }




    Queues

    class Queue
    {
    private:
    struct NameRec
    {
    char name[MAXCHARS];
    NameRec *nextaddr;
    };
    NameRec *queue_in;
    NameRec *queue out;
    public:
    Queue(void);
    void enqueue(char *);
    char *dequeue(void);
    int isEmpty(void);
    };


    Queue::Queue(void)
    {
    queue_in = NULL;
    queue_out = NULL;
    }


    void Queue::enqueue(char *newname)
    {
    NameRec *new_addr;
    new_addr = new(NameRec);
    if (new_addr==NULL) {
    cout << “No more memory” << endl;
    else {
    strcpy(new_addr->name, newname);
    new_addr->nextaddr = NULL;
    if (queue_in!=NULL)
    queue_in->nextaddr = new_addr;
    if (queue_out==NULL)
    queue_in = new_addr;
    }
    return;
    }


    char *Queue::dequeue(void)
    {
    NameRec *temp_addr;
    char name[MAXCHARS];
    strcpy(name, queue_out->name);
    temp_addr = queue_out->nextaddr;
    delete(queue_out);
    queue_out = temp_addr;
    return name;
    }



    int Queue::isEmpty(void)
    {
    if (queue_out ==NULL)
    return true;
    else
    return false;
    }


    int main()
    {
    char Newname[MAXCHARS];
    Queue namequeue;
    cin.getline(newname,MAXCHARS);
    namequeue.enqueue(newname);
    ….
    while (!namequeue.isEmpty())
    {
    strcpy(newname, namequeue.dequeue());
    cout << newname << endl;
    }
    return 0;
    }


    language-C++
    Kollo me tika C walin karana widiya kiyanna baida. Anyway thanks help ekata.

    Thanks machan !!!
     

    san**

    Well-known member
  • Feb 21, 2012
    5,418
    8,368
    113
    kandy
    loku wenasak wenne na..me code eke OOP concept use karala na mata mathaka hatiyata.. cout>> wenuwata printf..cin<< wenuwata scanf danna puluwan..c compiler eka c++ code awlak nathuwa run karanwa..

    method calling structured widihata karanna. (C ,OOP nathi nisa).object create karanne nathuwa..
    wade amarui wage hithenawa nam c++ to c coveter ekak use karanna..habai result aka nam kohoma weida danne na
     
    Last edited:
    loku wenasak wenne na..me code eke OOP concept use karala na mata mathaka hatiyata.. cout>> wenuwata printf..cin<< wenuwata scanf danna puluwan..c compiler eka c++ code awlak nathuwa run karanwa..

    method calling structured widihata karanna. (C ,OOP nathi nisa).object create karanne nathuwa..
    wade amarui wage hithenawa nam c++ to c coveter ekak use karanna..habai result aka nam kohoma weida danne na
    ela rep added machan
    thanks