Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication3.cpp : Defines the entry point for the console application.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <conio.h>
- typedef struct student {
- struct student* next;
- struct student* prev;
- int fnum;
- char firstname[20];
- char lastname[20];
- double grade;
- }student;
- student input(void);
- struct student* Add(student *root);
- void output(student *root);
- int main()
- {
- student* head=NULL;
- int n;
- printf("How many elements do you want to create?");
- scanf_s("%d", &n);
- for (int i = 0; i < n; i++) {
- head=Add(head);
- }
- output(head);
- return 0;
- }
- student input(void) {
- student a = { 0 };
- printf("Firstname:");
- fgets(a.firstname, sizeof a.firstname, stdin);
- printf("Lastname:");
- fgets(a.lastname, sizeof a.lastname, stdin);
- printf("Firstname:");
- fgets(a.firstname, sizeof a.firstname, stdin);
- printf("fnum");
- scanf_s("%d", &(a.fnum));
- printf("grade");
- scanf_s("%lf", &(a.grade));
- a.next = NULL;
- a.prev = NULL;
- return a;
- }
- student* Add(student* root) {
- student* p = malloc(sizeof(student));
- student b = input();
- p = &b;
- student* l;
- if (root == NULL) {
- root = p;
- return root;
- }
- for (;;) {
- if (root->next == NULL) {
- if (root->fnum < p->fnum) {
- root->next = p;
- p->prev = root;
- return root;
- }
- else {
- l = root;
- root = p;
- root->next = l;
- l->prev = p;
- return root;
- }
- }
- if (root->fnum < p->fnum) {
- root = root->next;
- continue;
- }
- else {
- l = root;
- root = p;
- root->next = l;
- l->prev = p;
- return root;
- }
- }
- return root;
- }
- void output(student* root) {
- if (root != NULL) {
- printf("fname %s", root->firstname);
- printf("lname %s",root->lastname);
- printf("fnum %d", root->fnum);
- printf("grade %13.6lf", root->grade);
- output(root->next);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement