Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <iostream>
- #include <iomanip>
- #include <stdlib.h>
- struct Node
- {
- struct data
- {
- int ID;
- float ms;
- }number;
- Node * Next;
- Node * Previous;
- };
- Node * Start;
- void AddToFront();
- void DeleteBeginning();
- void PrintListForward();
- void PrintListBackward();
- void main()
- {
- Start = 0;
- int Choice;
- Choice = 0;
- printf("************** MAIN MENU ****************\n");
- printf("Please Enter one of the following Choices\n");
- printf("************** MAIN MENU ****************\n\n");
- do
- {
- printf("======================================================\n");
- printf("1. Add a node to the front of the list.\n");
- printf("2. Delete a node from the beginning of the list.\n");
- printf("3. Print a list Forward.\n");
- printf("4. Print a list Backward.\n");
- printf("======================================================\n");
- printf("5: Exit the program.\n");
- scanf_s("%d", &Choice);
- switch (Choice)
- {
- case 1:
- AddToFront();
- break;
- case 2:
- DeleteBeginning();
- break;
- case 3:
- PrintListForward();
- break;
- case 4:
- PrintListBackward();
- break;
- default:
- break;
- }
- } while (Choice != 5); //when the choice is == 5, the program returns
- }
- void AddToFront()
- {
- Node * NewNode;
- NewNode = (Node *)malloc(sizeof(Node));
- printf("\n");
- printf("Enter ID: ");
- scanf("%d", &NewNode->number.ID);
- printf("Enter milliseconds: ");
- scanf("%f", &NewNode->number.ms);
- if (Start == 0)
- {
- NewNode->Previous = 0;
- NewNode->Next = 0;
- Start = NewNode;
- }
- else
- {
- Node *temp = Start;
- while (temp->Next != 0)
- {
- temp = temp->Next;
- }
- NewNode->Previous = temp;
- temp->Next = NewNode;
- NewNode->Next = 0;
- }
- }
- void PrintListBackward()
- {
- if (Start == 0)
- {
- printf("Nothing to print, insert some elements first.\n\n");
- return;
- }
- Node *temp = Start;
- //Go to the first element
- while (temp->Next != 0)
- {
- temp = temp->Next;
- }
- //Print from first to last
- while (temp != 0)
- {
- /*printf("%d", temp->data);
- printf("->");
- temp = temp->Next;*/
- printf("%d", temp->number.ID);
- printf(" -> ");
- printf("ms: %0.1f", temp->number.ms);
- printf(" -> ");
- temp = temp->Previous;
- }
- printf("NULL\n");
- }
- void PrintListForward()
- {
- if (Start == 0)
- {
- printf("Nothing to print, insert some elements first.\n\n");
- return;
- }
- Node *temp = Start;
- //Go till the last element
- while (temp->Previous != 0)
- {
- temp = temp->Previous;
- }
- //Print from last to first
- while (temp != 0)
- {
- printf("%d", temp->number.ID);
- printf("->");
- printf("ms: %0.1f", temp->number.ms);
- printf(" -> ");
- temp = temp->Next;
- }
- printf("NULL\n");
- }
- void DeleteBeginning()
- {
- if (Start == 0)
- {
- printf("Nothing to delete, insert some elements first.\n\n");
- }
- else if (Start->Next == 0 && Start->Previous == 0) { //There is only one element and it should be removed.
- Start = 0; //this removes the only element
- }
- else
- {
- Node *temp = Start;
- //Go to the LAST element of the list
- while (temp->Next != 0)
- {
- temp = temp->Next;
- }
- //Loop till first element that has a Previous element
- while (temp->Previous != 0)
- {
- //Verify if the element after the current one is the first
- //We want the one before the first to remove its Previous
- if (temp->Previous->Previous == 0) {
- break;
- }
- temp = temp->Previous;
- }
- Node *PreviousOfTemp = temp->Previous;
- Node * NewStart = temp;
- temp->Previous = 0;
- free(PreviousOfTemp);
- Start = NewStart;
- PrintListForward();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement