/FILE: node1.h (part of the namespace main_savitch 5) // PROVIDES: A class for a
ID: 3859407 • Letter: #
Question
/FILE: node1.h (part of the namespace main_savitch 5) // PROVIDES: A class for a node in a linked list and a collection of functions for // manipulating linked list.s /I TYPEDEF for the node class /I Each node of the list contains a piece of data and a pointer to the next node. The // type of the data is defined as node::value type in a typedef statement. The value type /I may be any of the C++ built-in types (int, char, etc.), or a class with a default constructor, // a copy constructor, an assignment operator, and a test for equality I CONSTRUCTOR for the node class // node(const value_type& init_data, nodeinit_link) // Postcondition: The node contains the specified data and link //NOTE: The init data parameter has a default value that is obtained from the default /I constructor of the value type. In the ANSI/ISO Standard, this notation is also allowed //for the built-in types, providing a default value of zero. The init_link has a default //value of NULL. (continued) // NOTE // Some of the functions have a return value that is a pointer to a node. Each of these // functions comes in two versions: a non-const version (where the return value is node) / and a const version (where the return value is const node) EXAMPLES const node *C; //C->1ink( activates the const version of link / ist search(c, calls the const version of list search // node *p; // p->link activates the non-const version of link // list search(p, calls the non-const version of list search /MEMBER FUNCTIONS for the node class // void set_data(const value_type& new_data) / Postcondition: The node now contains the specified new data / Void set.link(node* new_link) // Postcondition: The node now contains the specified new link // value_type data() const /Postcondition: The return value is the data from this node.Explanation / Answer
/*C program to read and print the N student
details using structure and Dynamic Memory Allocation.*/
#include <stdio.h>
#include <stdlib.h>
/*structure declaration*/
struct student
{
char name[30];
int roll;
float perc;
};
int main()
{
struct student *pstd;
int n,i;
printf("Enter total number of elements: ");
scanf("%d",&n);
/*Allocate memory dynamically for n objetcs*/
pstd=(struct student*)malloc(n*sizeof(struct student));
if(pstd==NULL)
{
printf("Insufficient Memory, Exiting... ");
return 0;
}
/*read and print details*/
for(i=0; i<n; i++)
{
printf(" Enter detail of student [%3d]: ",i+1);
printf("Enter name: ");
scanf(" "); /*clear input buffer*/
gets((pstd+i)->name);
printf("Enter roll number: ");
scanf("%d",&(pstd+i)->roll);
printf("Enter percentage: ");
scanf("%f",&(pstd+i)->perc);
}
printf(" Entered details are: ");
for(i=0; i<n; i++)
{
printf("%30s %5d %.2f ",(pstd+i)->name,(pstd+i)->roll,(pstd+i)->perc);
}
return 0;
}
----------------------------------
/*C program to read and print the student details
using structure and Dynamic Memory Allocation.*/
#include <stdio.h>
#include <stdlib.h>
/*structure declaration*/
struct student
{
char name[30];
int roll;
float perc;
};
int main()
{
struct student *pstd;
/*Allocate memory dynamically*/
pstd=(struct student*)malloc(1*sizeof(struct student));
if(pstd==NULL)
{
printf("Insufficient Memory, Exiting... ");
return 0;
}
/*read and print details*/
printf("Enter name: ");
gets(pstd->name);
printf("Enter roll number: ");
scanf("%d",&pstd->roll);
printf("Enter percentage: ");
scanf("%f",&pstd->perc);
printf(" Entered details are: ");
printf("Name: %s, Roll Number: %d, Percentage: %.2f ",pstd->name,pstd->roll,pstd->perc);
return 0;
}
--------------------------
/*C program to input and print text
using Dynamic Memory Allocation.*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
char *text;
printf("Enter limit of the text: ");
scanf("%d",&n);
/*allocate memory dynamically*/
text=(char*)malloc(n*sizeof(char));
printf("Enter text: ");
scanf(" "); /*clear input buffer*/
gets(text);
printf("Inputted text is: %s ",text);
/*Free Memory*/
free(text);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.