Question

"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;
}

Homework Answers

Answer #2

First question answer

#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

int i;
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 = new_ptr;

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

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

return 0;
}

Sample output

Enter last name: a
Enter first name: a
Enter GPA: 5
Enter age: 5
Enter EXIT to exit: n
Enter last name: b
Enter first name: b
Enter GPA: 3
Enter age: 3
Enter EXIT to exit: n
Enter last name: c
Enter first name: c
Enter GPA: 9
Enter age: 9
Enter EXIT to exit: n
Enter last name: m
Enter first name: m
Enter GPA: 7
Enter age: 7
Enter EXIT to exit: EXIT
Last name: b, GPA: 3.000000
Last name: a, GPA: 5.000000
Last name: m, GPA: 7.000000
Last name: c, GPA: 9.000000
Last name: b, GPA: 3.000000
Last name: a, GPA: 5.000000
Last name: m, GPA: 7.000000
Last name: c, GPA: 9.000000
Last name: b, GPA: 3.000000
Last name: a, GPA: 5.000000
Last name: m, GPA: 7.000000
Last name: c, GPA: 9.000000

Last name: b, GPA: 3.000000


Second question answer


#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;
struct student_node *prev;
  
}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,*last_node_ptr; //a pointer to a student_node type that tracks

int i;
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;
new_ptr->prev = NULL;

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

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

Sample output

Enter last name: aaa
Enter first name: aaa
Enter GPA: 5
Enter age: 5
Enter EXIT to exit: n
Enter last name: ttt
Enter first name: ttt
Enter GPA: 3
Enter age: 3
Enter EXIT to exit: n
Enter last name: m
Enter first name: m
Enter GPA: 7
Enter age: 7
Enter EXIT to exit: EXIT
forward traversing
Last name: ttt, GPA: 3.000000
Last name: aaa, GPA: 5.000000
Last name: m, GPA: 7.000000
backward traversing
Last name: m, GPA: 7.000000
Last name: aaa, GPA: 5.000000
Last name: ttt, GPA: 3.000000


answered by: anonymous
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if you try to add to a position greater than the number of nodes you get an error warning. /INSERT.C /*THIS PROGRAM READS A BINARY FILE (MUST ALREADY EXIST AND BE FILLED) */ /*AND PUTS IT INTO A LINKED LIST AND PRINTS THE LIST TO THE SCREEN) */ #include #include #include #include typedef struct ENTRY { char name[81]; }ENTRY; typedef struct LISTREC /* LISTREC is...
Given an array of Student type and size 10, create a linked list of students by...
Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list. (Use C++ ) #include<iostream> #include<string> #include<fstream> using namespace std; const int NUM = 10; struct Student{ string fName; string lName; Student * next; }; int main() {        Student stuArr[NUM];        ifstream myfile;        myfile.open("Test.txt");        for(int...
Design flow chart of this code and make sure it is not handwritten it must on...
Design flow chart of this code and make sure it is not handwritten it must on some software. struct user {    int id;    int age;    bool annualClaim;    int plan;    char name[30];    char contactNum[15];    char address[50]; }; #define MAX_LENGTH 500 struct user users[100]; int userCount = 0; struct claim {    int id;    int claimedYear;    int amountClaimed;    int remaininigAmount; }; struct claim claims[100]; void loadData() {    char line[MAX_LENGTH];    const...
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when...
C++ questions QUESTION 1 What kind of linked list begins with a pointer to the first...
C++ questions QUESTION 1 What kind of linked list begins with a pointer to the first node, and each node contains a pointer to the next node, and the pointer in the last node points back to the first node? A. Circular, singly-linked list. B. Circular, doubly-linked list. C. Singly-linked list. D. Doubly-linked list. E. None of the above.    QUESTION 2 _________ is not an advantage of linked lists when compared to arrays. A. Dynamic memory allocation. B. Efficient...
1. Create a linked list using the Node class that contains the first million prime numbers...
1. Create a linked list using the Node class that contains the first million prime numbers (primes4.txt). It should have a menu with these options: 1 = Search for a Number (let the user enter a number to search for) 2 = Add a new Number (let the user enter a number and add it to the head of the list) 3 = Delete a Number (let the user enter a number and delete it if found) 4 = Exit...
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT