Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   C (https://www.askmehelpdesk.com/forumdisplay.php?f=438)
-   -   Programming Questions (https://www.askmehelpdesk.com/showthread.php?t=348578)

  • May 1, 2009, 07:00 PM
    andson88
    Programming Questions
    Write a C/C++ program to perform the task of maintaining student information for 123 College. You are to use a linked-list for storing the student records. Each student record contains the student’s name and age, and it should be a node in the linked-list. Your program should have an insert(), delete(), and print() functions. The prototypes are given below:

    void insert( struct student **headOfList, char *name, int age );
    int delete( struct student **headOfList, char *name );
    void print( struct student *headOfList );



    The insert() function inserts the node in increasing alphabetical ordering.

    Perform the following tasks in the main() function of your program:

    1) Add the following 3 students to the linked-list (which is initially empty), one at a time:
    a) John, 18
    b) Sally, 19
    c) Matt, 18
    2) After adding the 3 students, print out the whole list using the print function.
    3) Add the following student to the list, and print out the resulting list.
    d) Jason, 18
    4) Compute and print out the average age of the 4 students by traversing the linked-list.
    5) Delete the student Matt from the list and print out the resulting list.

    The print() function should output each of the students’ name and age in a readable fashion. Make sure the output is properly formatted and each printing can be distinguished from the other.



    MARKING CRITERIA

    Program 1
    - Insert() function
    - Delete() function
    - Print() function
    - Main() function
    - Output Format
    - Formatting, variable names, comments, alignment
    - Use of malloc()






    TASK 2

    Write a second C/C++ program to implement the stack functionality using a linked-list. The stack functions you need to implement are:

    void push( struct student **stack, char *name, int age );
    struct student * pop( struct student **stack );
    int isEmpty( struct student *stack );

    These three functions have the normal stack functionalities. Perform the following tasks in the main() function of your program:

    1) Create an empty stack.
    2) Push 3 students onto the stack ( “Ahmad”, 20; “Beng”, 21; “Cathy, 18 )
    3) Pop a student from the stack and display that student’s information
    4) Push another student onto the stack ( “David”, 25 );
    5) Pop the stack one student at a time until the stack is empty. Each time you popped a student from
    the stack, print out that student’s information.





    MARKING CRITERIA


    Program 2
    - Push() function
    - Pop() function
    - IsEmpty() function
    - Main() function
    - Output Format
    - Formatting, variable names, comments, alignment
    - Use of malloc()
  • May 1, 2009, 07:27 PM
    Perito

    So, what's your question?

    Writing this is going to take some time, for sure.
  • May 1, 2009, 07:46 PM
    andson88
    Whether you see my answer 1st part correct or not?


    (Task 2)



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


    Struct student {
    char *name;
    int age;
    struct student *nextName;
    struct node *nextAge;
    };
    Typedef struct student student;
    Typedef student *studentptr;

    Void push( struct student **stack, char *name, int age );
    Struct student * pop( struct student **stack );
    Int isEmpty( struct student *stack );

    Int main (void)
    {



    Return 0;

    }




    I don't understand the questions talking about what it wants??
    Can you help me to correct it?
    I have already to try my best but still don't understand some part don't not know how to start.
  • May 1, 2009, 08:27 PM
    Perito

    Do you understand "linked lists"?

    C++ Tutorial - Linked Lists

    Creating Linked Lists in C++

    Let me google that for you

    They want you to write a program using a linked list. You create the list, then call the "Insert" function (that you have to write) to add records to the list. Call the "Delete" function (that you have to write) to remove records from the list. Call the "Print" function (that you have to write) to print records from the list.

    The linked list is basically a group of pointers. Each record contains a pointer to the previous and to the next record in the list. When you add a record, you simply allocate memory; fill the record with data; change the pointers. Malloc is usually used in C++ to allocate the memory.

    To start, create a "head" and a "tail" node (beginning and end). The head and tail nodes will be records just like the other records except that in the "head" node, the "Previous" pointer will be Null. The "Next" pointer will point to the tail. In the "tail", the Previous pointer will point to the head and the "Next" pointer will be Null. When you add a record, simply figure out where you wish to insert the node (after you add nodes, there will be many nodes). For the first record, it will be between the head node and the tail node. Set the "next" pointer of the new record to the value of the "Next" pointer in the "head". Then set the Head's "next" pointer to point to the new record. Set the "Previous" pointer of the record to the value of the "Previous" pointer in the tail and set the tail's "Previous" pointer to point to the new record.

    If you can follow this, you can easily build a linked list. Read the references I've attached.
  • May 1, 2009, 08:56 PM
    andson88
    I still don't undertsand how to do these two questions.
    confusing about that.


    but I show u some example.
    Is the way of doing??



    Example:


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


    struct node{
    char data;
    struct node *nextnode;
    };

    void insert( struct node ** headOfList, char dataToInsert ) {
    struct node *newNode = (struct node *)malloc( sizeof( struct node ) );
    if (newNode == NULL) {
    printf( "Not enough memory." );
    return;
    }
    struct node * prevNode = NULL;
    struct node * currentNode = *headOfList;
    while (currentNode != NULL) {
    if (dataToInsert < currentNode->data) {

    break;
    } else {

    prevNode = currentNode;
    currentNode = currentNode->nextNode;
    }
    }

    if (prevNode == NULL) {

    newNode->data = dataToInsert;
    newNode->nextNode = *headOfList;
    *headOfList = newNode;
    } else {
    newNode->data = dataToInsert;
    newNode->nextNode = currentNode;
    prevNode->nextNode = newNode;
    }
    }

    void print( struct node * headOfList ) {
    struct node *currentNode = headOfList;
    while (currentNode != NULL) {
    printf( "Data in node is %c\n", currentNode.data );
    currentNode = currentNode->nextNode;
    }
    printf("--------------------------------");
    }


    int main() {
    struct node *Head = NULL;

    insert( &Head, 'D' );

    print( Head );

    insert( &Head, 'B' );

    print( Head );

    insert( &Head, 'C' );

    print( Head );

    return 0;
    }

  • All times are GMT -7. The time now is 11:43 AM.