Question

In: Computer Science

"C language" Take this code and make the minor modification necessary to create a circular linked...

"C language"

Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times.

Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not circular because the ends are still NULL. Demonstrate this by traversing the list forward and then traverse the link backward. (Hint: create two pointers in the head one to the front of the linked list and one to the rear of the linked list)

take screen shots of each step in the process and the outcome of each step if you can.

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

typedef struct student_node{
char lname[25];
char fname[25];
float gpa;
int age;
struct student_node *next;
}student_node;

student_node* header = NULL;

//main function
int main(int argc, char *argv[])
{
student_node* new_ptr; //holds the pointer returned in malloc to new node
student_node* cur_ptr; //a pointer to a student_node type that tracks

char choice[10];
while(1)
{
new_ptr = (student_node*) malloc (sizeof (student_node)); // creates a new node
printf("Enter last name: ");
scanf("%s", new_ptr->lname);
printf("Enter first name: ");
scanf("%s", new_ptr->fname);
printf("Enter GPA: ");
scanf("%f", &new_ptr->gpa);
printf("Enter age: ");
scanf("%d", &new_ptr->age);
new_ptr->next = NULL;

if(header==NULL)
{
header = new_ptr;
}
else if(header->gpa < new_ptr->gpa)
{
new_ptr->next = header;
header = new_ptr;
}
else{
cur_ptr = header;
student_node *p=NULL;

while(cur_ptr!=NULL && new_ptr->gpa<cur_ptr->gpa)
{
p = cur_ptr;
cur_ptr = cur_ptr->next;
}
p->next = new_ptr;
new_ptr->next = cur_ptr;
}
printf("Enter EXIT to exit: ");
scanf("%s", choice);
if(!strcmp(choice, "EXIT"))
break;
}

//traverse the list
cur_ptr = header;
while(cur_ptr!=NULL)
{
printf("Last name: %s, GPA: %f\n", cur_ptr->lname, cur_ptr->gpa);
cur_ptr = cur_ptr->next;
}

return 0;
}

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct student_node {

char lname[25];

char fname[25];

float gpa;

int age;

struct student_node *next;

} student_node;

student_node *header = NULL;

// main function

int main(int argc, char *argv[]) {

student_node *new_ptr; // holds the pointer returned in malloc to new node

student_node *cur_ptr; // a pointer to a student_node type that tracks

char choice[10];

while (1) {

new_ptr =

(student_node *)malloc(sizeof(student_node)); // creates a new node

printf("Enter last name: ");

scanf("%s", new_ptr->lname);

printf("Enter first name: ");

scanf("%s", new_ptr->fname);

printf("Enter GPA: ");

scanf("%f", &new_ptr->gpa);

printf("Enter age: ");

scanf("%d", &new_ptr->age);

cur_ptr = header;

if (cur_ptr == NULL) {

new_ptr->next = new_ptr;

header = new_ptr;

} else if (header->gpa < new_ptr->gpa) {

while (cur_ptr->next != header && cur_ptr->next->gpa < new_ptr->gpa)

cur_ptr = cur_ptr->next;

new_ptr->next = cur_ptr->next;

cur_ptr->next = new_ptr;

} else {

while (cur_ptr->next != header)

cur_ptr = cur_ptr->next;

cur_ptr->next = new_ptr;

new_ptr->next = header;

header = new_ptr;

}

printf("Enter EXIT to exit: ");

scanf("%s", choice);

if (!strcmp(choice, "EXIT"))

break;

}

if (header != NULL) {

// traverse the list

cur_ptr = header;

do {

printf("Last name: %s, GPA: %f\n", cur_ptr->lname, cur_ptr->gpa);

cur_ptr = cur_ptr->next;

} while (cur_ptr != header);

}

return 0;

}


Related Solutions

Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
make a calculator in C++ or C language that can take up to 5 values at...
make a calculator in C++ or C language that can take up to 5 values at same time
Create all necessary code to make this main function work. It is not allowed to change...
Create all necessary code to make this main function work. It is not allowed to change the main function. int main() {        int ListDataSample1[] = { 1, 1, 1 };        int ListDataSample2[] = { 2, 2, 2 };        List<int> List1 = List<int>(ListDataSample2, 3);        List<int> List2 = List<int>(ListDataSample2, 3);               cout << "List1 :" << List1 << endl;        cout << "List2 :" << List2 << endl << endl;        List1 += List2;               cout...
This is my C language code. I have some problems with the linked list. I can't...
This is my C language code. I have some problems with the linked list. I can't store the current. After current = temp, I don't know how to move to the next node. current = current-> next keeps making current into NULL. #include #include #include #include struct node{int data; struct node *next;}; int main() {     struct node *head, *current, *temp, *trash;     srand(time(0));     int randNumber = rand()%51;     if(randNumber != 49)     {         temp = (struct node*)malloc(sizeof(struct node));         current = (struct node*)malloc(sizeof(struct node));...
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
1) What is a necessary that might make it impossible for a minor to void a...
1) What is a necessary that might make it impossible for a minor to void a contract?  Give three examples. 2) What is the difference between unilateral mistake of fact and mutual mistake of fact?  How does each affect a contract?
Please Use C language to Make a calculator. Make sure calculator is able to take up...
Please Use C language to Make a calculator. Make sure calculator is able to take up to 5 values. Try to make calculator using simple coding As you can. Please create a simple calculator with only +, -,* and divide. It has to be able to enter any numbers (including decimals) and be able to do the following functions: +, -, divide and multiply. Please have the answers, always rounded to two decimal figures. The calculator will also have to...
Your are to take the following c code and do the following on Eve: create a...
Your are to take the following c code and do the following on Eve: create a file with the code called quiz32.c create a second file with the code called quiz64.c Using gcc and stopping it after the Assembly phase, generate an optimization level 1, 32 bit ISA relocatable object file for quiz32.c Using gcc and stopping it after the Assembly phase, generate an optimization level 1, 64 bit ISA relocatable object file for quiz64.c obtain a list of the...
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
First, create a project that creates an ordered linked list class (use the code provided, make...
First, create a project that creates an ordered linked list class (use the code provided, make sure to update the insert function so that it maintains an ordered list). To this class, add a function that prints the list in REVERSE order. Making use of recursion is the easiest and best way to do this, but certainly not the only way. Submit a .zip of your entire project.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT