In: Computer Science
"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;
}
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;
}