"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;
}
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
Get Answers For Free
Most questions answered within 1 hours.