SENG 265 A01/A02 Page 11 Section C: C programming (50 marks) Question 24: Weight
ID: 3705074 • Letter: S
Question
SENG 265 A01/A02 Page 11 Section C: C programming (50 marks) Question 24: Weight 20 Consider the following data structure typedef struct Node.t Node t: struct Node t f Float value: Node t *next; Also consider the following diagram showing one posible configuration of nodes using the data structure defined above: Assuming the data-structure declaration in the box above is already given in of some C file, write the remaining C code necessary in order to create dynamically the structure shown in the diagram. Some marks will be given for the quality of your answrer. (The next page is available, ifneeded, for your ansuer to Question 24)Explanation / Answer
#include <stdio.h>
#includde <stdlib.h>
// Assuming that data structure declaration is already given in other file
struct Node_t *head = NULL, *x, *y;
void create()
{
int choice;
x = (struct Node_t*)malloc(sizeof(struct Node_t));
printf (" Enter the value : ");
scanf("%f", &x->value);
x->next = x;
head = x;
printf(" Press 1 to continue else 0 :");
scanf("%d", &choice);
while(choice != 0)
{
y = (struct Node_t*)malloc(sizeof(struct Node_t));
printf(" Enter the value :");
scanf("%f", &y->value);
x->next = y; // insert y after x
y->next = head; // make y point to head to make it circular
x = y;
printf(" Press 1 to continue else 0 :");
scanf("%d", &choice);
}
}
void traverse()
{
if(head == NULL)
printf(" No element to display");
else
{
x = head;
while (x-> next != head)
{
printf("%f->", x->value);
x = x->next;
}
printf("%f", x->value);
}
}
void delete()
{
if(head == NULL)
printf(" List is empty");
else
{
int c=1, pos;
printf(" Enter the position to be deleted:");
scanf("%d", &pos);
x = head;
while (c < pos) // get to the position which is to be deleted
{
y = x;
x = x-> next;
c++;
}
y->next = x->next;
free(x); // free the node from memory
}
}
void main()
{
struct Node_t *head = NULL;
// create this circular list
create();
// traverse and display the list
traverse();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.