Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct Node
- {
- int data;
- Node * Next;
- Node * Prev;
- };
- Node * Start;
- //Declaring Functions
- void AddToFront();
- void AddToBack();
- void AddToMiddle();
- void AddBeforeNode();
- void DeleteFirstNode();
- void DeleteLastNode();
- void DeleteAfterNode();
- void DeleteBeforeNode();
- void PrintListForward();
- void PrintListBack();
- void saveSeqFile();
- void loadSeqFile();
- void saveRdmFile();
- void loadRdmFile();
- void createNewLinkList(int);
- void main() {
- Start = 0;
- int Choice;
- int MaxChoices;
- // set up your Maximum Choice here
- MaxChoices = 15;
- // It creates a menu with 10 items
- Choice = 0;
- while (Choice < MaxChoices)
- {
- printf("\n\n");
- printf("--------------------------------- MAIN MENU ---------------------------------\n\n");
- printf(" Please Enter one of the following Choices\n\n");
- printf("--------------------------- Linked List Processing --------------------------\n\n\n");
- printf("%s\n", "1: Add a Node to the Front of the List");
- printf("%s\n", "2: Add a node to the back of the list ");
- printf("%s\n", "3: Add a node to the middle of the list after a specific node");
- printf("%s\n", "4: Add a node to the middle of the list before a specific node");
- printf("%s\n", "5: Delete a node from the front of the list");
- printf("%s\n", "6: Delete a node from the back of the list");
- printf("%s\n", "7: Delete a node from the middle of the list after a specific node");
- printf("%s\n", "8: Delete a node from the middle of the list before a specific node");
- printf("%s\n", "9: Print a list forward");
- printf("%s\n", "10: Print a list backward");
- printf("%s\n", "11: Save in Sequential file");
- printf("%s\n", "12: Load from Sequential file");
- printf("%s\n", "13: Save in Ramdom file");
- printf("%s\n", "14: Load from Ramdom file");
- printf("%s\n", "15: Exit The Program");
- printf("-----------------------------------------------------------------------------");
- printf("\n\n");
- // Print Your choices here
- scanf("%d", &Choice);
- switch (Choice)
- {
- case 1:
- AddToFront();
- break;
- case 2:
- AddToBack();
- break;
- case 3:
- AddToMiddle();
- break;
- case 4:
- AddBeforeNode();
- break;
- case 5:
- DeleteFirstNode();
- PrintListForward();
- break;
- case 6:
- DeleteLastNode();
- break;
- case 7:
- DeleteAfterNode();
- break;
- case 8:
- DeleteBeforeNode();
- break;
- case 9:
- PrintListForward();
- break;
- case 10:
- PrintListBack();
- break;
- case 11:
- saveSeqFile();
- break;
- case 12:
- loadSeqFile();
- break;
- case 13:
- saveRdmFile();
- break;
- case 14:
- loadRdmFile();
- break;
- case 15:
- break;
- }
- }
- return;
- }
- // ***********************************************************
- // ************************ FUNCTIONS ************************
- // ***********************************************************
- //CASE 1 -------------> Add note to the begining of the list
- void AddToFront() {
- Node * NewNode;
- if (Start == 0)
- {
- //This list is empty. Create the first node
- NewNode = (Node *)malloc(sizeof(Node));
- printf("\n");
- printf("Enter data for the node: ");
- scanf("%d", &NewNode->data);
- NewNode->Next = 0;
- NewNode->Prev = 0;
- Start = NewNode;
- //Print list
- printf("\n");
- printf("Addition of First Node done!\n\n");
- PrintListForward();
- }
- else
- {
- //If the list is not Empty then create new node
- NewNode = (Node *)malloc(sizeof(Node));
- printf("\n");
- printf("Enter data for the node: ");
- scanf("%d", &NewNode->data);
- printf("\n");
- //NewNode 2nd layer (Next) points to the start of a Next Node
- NewNode->Next = Start;
- //NewNode 3rd layer (Prev) point to nothing
- NewNode->Prev = 0;
- //The previous Node 3rd layer (Prev) points to the Node that is been added
- Start->Prev = NewNode;
- //Start variable is now the Current Node, the one that's been added
- Start = NewNode;
- //Print list
- printf("New node added at the begining of the list!\n\n");
- PrintListForward();
- }
- return;
- }
- //CASE 2 -------------> Add Node to Last part of the list
- void AddToBack() {
- Node * NewNode;
- if (Start == 0)
- {
- //This list is empty. Create the first node
- NewNode = (Node *)malloc(sizeof(Node));
- printf("\n");
- printf("Enter data for the node: ");
- scanf("%d", &NewNode->data);
- NewNode->Next = 0;
- NewNode->Prev = 0;
- Start = NewNode;
- //Print list
- printf("\n");
- printf("Addition of First Node done!\n\n");
- PrintListForward();
- }
- else
- {
- //when the list is not empty we have to check every node until we find the last one
- Node * Current;
- Current = Start;
- while (Current->Next != 0)
- {
- Current = Current->Next;
- }
- //After finding the last node of the list we can create a new node
- NewNode = (Node *)malloc(sizeof(Node));
- printf("\n");
- printf("Enter data for the node: ");
- scanf("%d", &NewNode->data);
- printf("\n");
- //NewNode 2nd layer (Next) points to nothing
- NewNode->Next = 0;
- //NewNode 3rd layer (Prev) point to the start of the Current Node
- NewNode->Prev = Current;
- //The actual Node 2nd layer (Next) points to the Node that is been added
- Current->Next = NewNode;
- //Print list
- printf("New node added at the end of the list!\n\n");
- PrintListForward();
- }
- return;
- }
- //CASE 3 -------------> Add to middle after
- void AddToMiddle() {
- if (Start == 0)
- {
- printf("\n");
- printf("List is empty! Please add nodes to list");
- printf("\n");
- return;
- }
- int findData, findIt;
- Node * Current;
- Node * NextOfCurrent;
- findIt = 0;
- Current = Start;
- NextOfCurrent = Current->Next;
- printf("\n");
- printf("Please insert data of node you want to add: ");
- scanf("%d", &findData);
- printf("\n");
- while (Current != 0)
- {
- if (Current->data == findData)
- {
- findIt = 1;
- break;
- }
- Current = Current->Next;
- if (Current == 0) // If Current is 0 (is the end of the list) the finish the while
- {
- break;
- }
- NextOfCurrent = Current->Next;
- }if (findIt == 0)
- {
- printf("We can't find the node!");
- return;
- }
- //If data is found we can add a new node next to it
- Node * NewNode;
- NewNode = (Node*)malloc(sizeof(Node));
- printf("\n");
- printf("Enter data for the new node: ");
- scanf("%d", &NewNode->data);
- printf("\n");
- if (NextOfCurrent == 0)
- {
- Current->Next = NewNode;
- NewNode->Prev = Current;
- NewNode->Next = 0;
- printf("The node is succesfully added after ");
- printf("%d\n", findData);
- PrintListForward();
- return;
- }
- Current->Next = NewNode;
- NewNode->Prev = Current;
- NextOfCurrent->Prev = NewNode;
- NewNode->Next = NextOfCurrent;
- printf("The new node was added after: ");
- printf("%d", findData);
- printf("\n");
- PrintListForward();
- }
- //CASE 4 ------------->
- void AddBeforeNode() {
- if (Start == 0)
- {
- printf("\n");
- printf("List is empty! Please add nodes to list");
- printf("\n");
- return;
- }
- int findData, findIt;
- Node * Current;
- Node * PrevOfCurrent;
- findIt = 0;
- Current = Start;
- PrevOfCurrent = Current->Prev;
- printf("\n");
- printf("Please insert data of node you want to add: ");
- scanf("%d", &findData);
- printf("\n");
- while (Current != 0)
- {
- if (Current->data == findData)
- {
- findIt = 1;
- break;
- }
- Current = Current->Next;
- if (Current == 0) // If Current is 0 (is the end of the list) the finish the while
- {
- break;
- }
- PrevOfCurrent = Current->Prev;
- }if (findIt == 0)
- {
- printf("We can't find the node!");
- return;
- }
- //If data is found we can add a new node next to it
- Node * NewNode;
- NewNode = (Node*)malloc(sizeof(Node));
- printf("\n");
- printf("Enter data for the new node: ");
- scanf("%d", &NewNode->data);
- printf("\n");
- if (PrevOfCurrent != 0)
- {
- Current->Prev = NewNode;
- NewNode->Next = Current;
- PrevOfCurrent->Next = NewNode;
- printf("\n");
- printf("The node is succesfully added after ");
- printf("%d\n", findData);
- printf("The new node was added before: ");
- printf("%d", findData);
- printf("\n");
- PrintListForward();
- return;
- }
- if (PrevOfCurrent == 0)
- {
- printf("The node can't be add before the first node, use option 1 in menu\n\n");
- PrintListForward();
- }
- }
- //CASE 5 -------------> Delete first Node of the list
- void DeleteFirstNode() {
- if (Start == 0)
- {
- printf("\n");
- printf("List is empty! Please add nodes to list");
- printf("\n");
- return;
- }
- Node * Current;
- Node * NextOfCurrent;
- Current = Start;
- NextOfCurrent = Current->Next;
- //When there's only one we empty the list again
- if (NextOfCurrent == 0)
- {
- Start = 0;
- }
- //If there are more nodes in the list
- else
- {
- NextOfCurrent->Prev = 0;
- Start = NextOfCurrent;
- }
- free(Current);
- }
- //CASE 6 -------------> Delete last Node of the list
- void DeleteLastNode() {
- if (Start == 0)
- {
- printf("\n");
- printf("List is empty! Please add nodes to list");
- printf("\n");
- return;
- }
- Node * NextOfCurrent;
- Node * Current;
- Current = Start;
- NextOfCurrent = Current->Next;
- if (NextOfCurrent == 0 && Current->Prev == 0)
- {
- Start = 0;
- free(Current);
- //Print list
- printf("\n");
- printf("The list is now empty, you delete the only node!\n\n");
- PrintListForward();
- return;
- }
- //when the list is not empty we have to check every node until we find the last one
- while (NextOfCurrent->Next != 0)
- {
- Current = Current->Next;
- NextOfCurrent = Current->Next;
- }
- //After finding the last node of the list we can delete node
- //Current is now the last one and we can delete Next of current
- Current->Next = 0;
- free(NextOfCurrent);
- //Print list
- printf("\n");
- printf("Last Node is been delete!\n\n");
- PrintListForward();
- return;
- }
- //CASE 7 -------------> Delete After Node
- void DeleteAfterNode() {
- //Case 1 Empty list, return to menu
- if (Start == 0)
- {
- printf("\n");
- printf("List is empty! Please add nodes to list");
- printf("\n");
- return;
- }
- int findData, findIt;
- Node * Current;
- Node * NextOfCurrent;
- findIt = 0;
- Current = Start;
- NextOfCurrent = Current->Next;
- printf("\n");
- printf("Please insert data of node you want to delete after: ");
- scanf("%d", &findData);
- printf("\n");
- //Find the node enter by user
- while (Current != 0)
- {
- if (Current->data == findData)
- {
- //You found the node
- findIt = 1;
- break;
- }
- Current = Current->Next;
- if (Current == 0) // If Current is 0 (is the end of the list) the finish the while
- {
- break;
- }
- NextOfCurrent = Current->Next;
- }
- if (findIt == 0) //Case 5 Node enter by user can't be found
- {
- printf("We can't find the node!");
- return;
- }
- //when the user wants to delete the node after the last.
- if (Current->Next == 0)
- {
- printf("You can't delete this one, because there is no node next to delete");
- return;
- }
- //when you want to delete the last node
- if (NextOfCurrent->Next == 0)
- {
- printf("You just delete the last node!\n");
- Current->Next = 0;
- free(NextOfCurrent);
- PrintListForward();
- return;
- }
- Current->Next = NextOfCurrent->Next;
- NextOfCurrent->Prev = Current;
- free(NextOfCurrent);
- printf("The node after %d was delete.", findData);
- printf("\n");
- PrintListForward();
- return;
- }
- //CASE 8 -------------> Delete Before Node
- void DeleteBeforeNode() {
- //Case 1 Empty list, return to menu
- if (Start == 0)
- {
- printf("\n");
- printf("List is empty! Please add nodes to list");
- printf("\n");
- return;
- }
- int findData, findIt;
- Node * Current;
- Node * PrevOfCurrent;
- findIt = 0;
- Current = Start;
- PrevOfCurrent = Current->Prev;
- printf("\n");
- printf("Please insert data of node you want to delete before: ");
- scanf("%d", &findData);
- printf("\n");
- //Find the node enter by user
- while (Current != 0)
- {
- if (Current->data == findData)
- {
- //You found the node
- findIt = 1;
- break;
- }
- Current = Current->Next;
- if (Current == 0) // If Current is 0 (is the end of the list) the finish the while
- {
- break;
- }
- PrevOfCurrent = Current->Prev;
- }
- if (findIt == 0) //Case 5 Node enter by user can't be found
- {
- printf("We can't find the node!");
- return;
- }
- //when the user wants to delete the node before the first.
- if (Current->Prev == 0)
- {
- printf("You can't delete this one, because there is no node before to delete");
- PrintListForward();
- return;
- }
- //when you want to delete the first node
- if (PrevOfCurrent->Prev == 0)
- {
- printf("The node can't be delete!, use option 5 in menu to delete first node\n\n");
- PrintListForward();
- return;
- }
- Current->Prev = PrevOfCurrent->Prev;
- PrevOfCurrent->Prev->Next = Current;
- printf("\n");
- printf("The node before %d was delete.", findData);
- printf("\n");
- PrintListForward();
- return;
- }
- //CASE 9 ------------->Print List Forward
- void PrintListForward() {
- if (Start == 0)
- {
- printf("\n");
- printf("List is empty! Please add nodes to list");
- printf("\n");
- return;
- }
- Node * Current;
- // The Current Node (NewNode) created is using the variable Start
- Current = Start;
- printf("\n");
- printf("This is the list:");
- printf("\n\n");
- while (Current != 0)
- {
- printf("%d", Current->data);
- printf("->");
- Current = Current->Next;
- }
- printf("NULL\n");
- return;
- }
- //CASE 10 -------------> Print List Backward
- void PrintListBack() {
- if (Start == 0)
- {
- printf("\n");
- printf("List is empty! Please add nodes to list");
- printf("\n");
- return;
- }
- Node * Current;
- // The Current Node (NewNode) created is using the variable Start
- Current = Start;
- printf("\n");
- printf("This is the list backwards:");
- printf("\n\n");
- while (Current->Next != 0)
- {
- Current = Current->Next;
- }
- while (Current != 0)
- {
- printf("%d", Current->data);
- printf("->");
- Current = Current->Prev;
- }
- printf("NULL\n");
- }
- //CASE 11 -------------> Save Sequential file
- void saveSeqFile()
- {
- Node * Current;
- Current = Start;
- FILE * CfPtr;
- if (Current == NULL)
- {
- printf("it is a empty list");
- return;
- }
- if ((CfPtr = fopen("SequenceFile.dat", "w")) == NULL)
- {
- printf("there is no file\n");
- }
- else
- {
- CfPtr = fopen("SequenceFile.dat", "w");
- while (Current != 0)
- {
- fprintf(CfPtr, "%d", Current->data);
- if (Current->Next != 0) //
- {
- fprintf(CfPtr, "%s", "\n");
- }
- Current = Current->Next;
- }
- }
- printf("saved succesfully!\n");
- fclose(CfPtr);
- }
- //CASE 12 -------------> Load Sequential file
- void loadSeqFile()
- {
- Node * current;
- current = Start;
- FILE * CfPtr;
- int tempInteger;
- while (current != NULL)
- {
- current = current->Next;
- DeleteFirstNode();
- }
- //the list is empty
- if ((CfPtr = fopen("SequenceFile.dat", "r")) == NULL)
- {
- printf("there is no file\n");
- }
- else
- {
- while (!feof(CfPtr)) //Not the end of file
- {
- fscanf(CfPtr, "%d", &tempInteger);
- createNewLinkList(tempInteger);
- }
- }
- fclose(CfPtr);
- PrintListForward();
- }
- //FUNCTION NOT IN MENU -------------> Using a argument to create a linked list
- void createNewLinkList(int aIntegerValue)
- {
- Node * NewNode;
- if (Start == 0)
- {
- //This list is empty. Create the first node
- NewNode = (Node *)malloc(sizeof(Node));
- NewNode->data = aIntegerValue;
- NewNode->Next = 0;
- NewNode->Prev = 0;
- Start = NewNode;
- }
- else
- {
- //when the list is not empty we have to check every node until we find the last one
- Node * Current;
- Current = Start;
- while (Current->Next != 0)
- {
- Current = Current->Next;
- }
- NewNode = (Node *)malloc(sizeof(Node));
- NewNode->data = aIntegerValue;
- NewNode->Next = 0;
- NewNode->Prev = Current;
- Current->Next = NewNode;
- }
- return;
- }
- //CASE 13 -------------> Save Ramdom File
- void saveRdmFile(){
- BlankRdmFile();
- //add list is empty
- Node * Current;
- Current = Start;
- FILE *cfPtr;
- //r+ read and write, read and more.
- if ((cfPtr = fopen("credit.dat", "r+")) == NULL)
- printf("File could not be opened.\n");
- else {
- while (Current->data != 0) {
- printf("Enter number: \n");
- scanf("%d", Current->data);
- //fseek will go to the file and skip some records before getting to the destination
- //SEEK_SET is to start from the begining of the file
- //&client.acctNum - 1 is the number of elements to skip, if the user wants to go to the begining of element 10, the program needs to skip 9 elements.
- fseek(cfPtr, (Current->data - 1) *
- sizeof(int), SEEK_SET);
- //write the info one time in the document.
- fwrite(&Current->data, sizeof(struct Node), 1, cfPtr);
- printf("Enter account number\n? ");
- scanf("%d", &Current->data);
- }
- }
- fclose(cfPtr);
- }
- //Create blank Ramdom file
- void BlankRdmFile(){
- int i;
- //Use interger not struct because in this case it only has one data as int
- struct Node NodeData = { 0 };
- FILE * cfPtr;
- if ((cfPtr = fopen("NodeList.dat", "w")) == NULL)
- printf("File could not be opened.\n");
- else {
- //Write one blank client the same size of the struct, 1 time inside Document.
- //This loop will go for 100 times, it would create a file with 100 blank clients.
- for (i = 1; i <= 100; i++)
- fwrite(&NodeData,
- sizeof(struct Node), 1, cfPtr);
- fclose(cfPtr);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement