Ask Experts Questions for FREE Help !
Ask
    andson88's Avatar
    andson88 Posts: 4, Reputation: 1
    New Member
     
    #1

    May 1, 2009, 07:00 PM
    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()
    Perito's Avatar
    Perito Posts: 3,139, Reputation: 150
    Ultra Member
     
    #2

    May 1, 2009, 07:27 PM

    So, what's your question?

    Writing this is going to take some time, for sure.
    andson88's Avatar
    andson88 Posts: 4, Reputation: 1
    New Member
     
    #3

    May 1, 2009, 07:46 PM
    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.
    Perito's Avatar
    Perito Posts: 3,139, Reputation: 150
    Ultra Member
     
    #4

    May 1, 2009, 08:27 PM

    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.
    andson88's Avatar
    andson88 Posts: 4, Reputation: 1
    New Member
     
    #5

    May 1, 2009, 08:56 PM
    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;
    }

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

C programming [ 2 Answers ]

I'll be extremely thankful if u could provide me with a c program that convert a given nfa to dfa

Something about programming! [ 4 Answers ]

Can anyone help me about this question>>>?? (Microsoft Visual C++) A company wants to transmit data over the telephone, but they are concerned that their phones may be tapped. All of their data is transmitted as four-digit integers. They have asked you to write a program that will encrypt their...

I'm trying to get into programming. [ 3 Answers ]

I have no clue were to start and I don't have the money for a collige but maybe someone can help a site or anything... well thank you for you're time Scythe.

PLC programming [ 2 Answers ]

I have to modify an existing PLC Ladder Logic program but I have no experience in programming the PLC and at this point it is little hard for me to comment on the code as I don't know what it exactly does. Can someone please suggest any reference material from which I can educate myself on...


View more questions Search