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...
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...
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++ 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...
C LANGUAGE IMPLEMENTATION - Writing source files implement several functions, declared in student.h, transcript.h and internal.h...
C LANGUAGE IMPLEMENTATION - Writing source files implement several functions, declared in student.h, transcript.h and internal.h (with support from common.h). There are 3 rules: 1. There are three types of students: undergraduate, MEng and PhD. 2. Undergraduates must complete 40 courses, MEng students 5 and PhD students 2. 3. Undergraduate students require a mark of 50 to pass; graduate students need a 65. ----------------Common.h-------------------------------------------------------------------------------------------------------------------------------------------------------------- #ifndef COMMON_H #define COMMON_H /*Portable macros for declaring C functions visible to C++.*/ #ifdef __cplusplus #define...
Explain this code function by function in detail int menu() {   int choice;   do {     system("cls");...
Explain this code function by function in detail int menu() {   int choice;   do {     system("cls");     printf("1-Insurence Plan Subscription\n");     printf("2-Claim Processing\n");     printf("3-Accounts Information\n");     printf("4-Searching Functionalities\n");     printf("5-Exit\n");     scanf("%d", &choice);   } while (choice > 5 || choice < 1);   return choice; void subscribe() system("cls");   victims[userCount].id = userCount + 1;   printf("Enter age: ");   scanf("%d", &victims[userCount].age);   printf("\n\n%-25sHealth Insurence Plan\n\n", " ");   printf("-----------------------------------------------------------------------------------------------\n");   printf("|%-30s|%-20s|%-20s|%-20s|\n", " ", "Plan 120(RM)", "Plan 150(RM)", "Plan 200(RM)");   printf("-----------------------------------------------------------------------------------------------\n");   printf("|%-30s|%-20d|%-20d|%-20d|\n", "Monthly Premium", 120, 150, 200);   printf("|%-30s|%-20d|%-20d|%-20d|\n", "Annual Claim Limit", 120000,150000,200000);   printf("|%-30s|%-20d|%-20d|%-20d|\n",...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT