Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5.  
  6. struct Node
  7. {
  8. int data;
  9. Node * Next;
  10. Node * Prev;
  11. };
  12.  
  13. Node * Start;
  14. //Declaring Functions
  15.  
  16. void AddToFront();
  17. void AddToBack();
  18. void AddToMiddle();
  19. void AddBeforeNode();
  20.  
  21. void DeleteFirstNode();
  22. void DeleteLastNode();
  23. void DeleteAfterNode();
  24. void DeleteBeforeNode();
  25.  
  26. void PrintListForward();
  27. void PrintListBack();
  28.  
  29. void saveSeqFile();
  30. void loadSeqFile();
  31. void saveRdmFile();
  32. void loadRdmFile();
  33.  
  34. void createNewLinkList(int);
  35. void main() {
  36.  
  37. Start = 0;
  38. int Choice;
  39. int MaxChoices;
  40.  
  41.  
  42. // set up your Maximum Choice here
  43. MaxChoices = 15;
  44. // It creates a menu with 10 items
  45. Choice = 0;
  46.  
  47. while (Choice < MaxChoices)
  48. {
  49. printf("\n\n");
  50. printf("--------------------------------- MAIN MENU ---------------------------------\n\n");
  51. printf(" Please Enter one of the following Choices\n\n");
  52. printf("--------------------------- Linked List Processing --------------------------\n\n\n");
  53. printf("%s\n", "1: Add a Node to the Front of the List");
  54. printf("%s\n", "2: Add a node to the back of the list ");
  55. printf("%s\n", "3: Add a node to the middle of the list after a specific node");
  56. printf("%s\n", "4: Add a node to the middle of the list before a specific node");
  57. printf("%s\n", "5: Delete a node from the front of the list");
  58. printf("%s\n", "6: Delete a node from the back of the list");
  59. printf("%s\n", "7: Delete a node from the middle of the list after a specific node");
  60. printf("%s\n", "8: Delete a node from the middle of the list before a specific node");
  61. printf("%s\n", "9: Print a list forward");
  62. printf("%s\n", "10: Print a list backward");
  63. printf("%s\n", "11: Save in Sequential file");
  64. printf("%s\n", "12: Load from Sequential file");
  65. printf("%s\n", "13: Save in Ramdom file");
  66. printf("%s\n", "14: Load from Ramdom file");
  67. printf("%s\n", "15: Exit The Program");
  68. printf("-----------------------------------------------------------------------------");
  69. printf("\n\n");
  70.  
  71. // Print Your choices here
  72. scanf("%d", &Choice);
  73.  
  74. switch (Choice)
  75. {
  76. case 1:
  77. AddToFront();
  78. break;
  79.  
  80. case 2:
  81. AddToBack();
  82. break;
  83.  
  84. case 3:
  85. AddToMiddle();
  86. break;
  87.  
  88. case 4:
  89. AddBeforeNode();
  90. break;
  91.  
  92. case 5:
  93. DeleteFirstNode();
  94. PrintListForward();
  95. break;
  96.  
  97. case 6:
  98. DeleteLastNode();
  99. break;
  100.  
  101. case 7:
  102. DeleteAfterNode();
  103. break;
  104.  
  105. case 8:
  106. DeleteBeforeNode();
  107. break;
  108.  
  109. case 9:
  110. PrintListForward();
  111. break;
  112.  
  113. case 10:
  114. PrintListBack();
  115. break;
  116.  
  117. case 11:
  118. saveSeqFile();
  119. break;
  120.  
  121. case 12:
  122. loadSeqFile();
  123. break;
  124.  
  125. case 13:
  126. saveRdmFile();
  127. break;
  128.  
  129. case 14:
  130. loadRdmFile();
  131. break;
  132.  
  133. case 15:
  134. break;
  135. }
  136. }
  137. return;
  138. }
  139. // ***********************************************************
  140. // ************************ FUNCTIONS ************************
  141. // ***********************************************************
  142.  
  143.  
  144. //CASE 1 -------------> Add note to the begining of the list
  145. void AddToFront() {
  146.  
  147. Node * NewNode;
  148.  
  149. if (Start == 0)
  150. {
  151. //This list is empty. Create the first node
  152.  
  153. NewNode = (Node *)malloc(sizeof(Node));
  154. printf("\n");
  155. printf("Enter data for the node: ");
  156. scanf("%d", &NewNode->data);
  157. NewNode->Next = 0;
  158. NewNode->Prev = 0;
  159. Start = NewNode;
  160.  
  161. //Print list
  162. printf("\n");
  163. printf("Addition of First Node done!\n\n");
  164. PrintListForward();
  165. }
  166. else
  167. {
  168. //If the list is not Empty then create new node
  169. NewNode = (Node *)malloc(sizeof(Node));
  170. printf("\n");
  171. printf("Enter data for the node: ");
  172. scanf("%d", &NewNode->data);
  173. printf("\n");
  174.  
  175. //NewNode 2nd layer (Next) points to the start of a Next Node
  176. NewNode->Next = Start;
  177. //NewNode 3rd layer (Prev) point to nothing
  178. NewNode->Prev = 0;
  179. //The previous Node 3rd layer (Prev) points to the Node that is been added
  180. Start->Prev = NewNode;
  181. //Start variable is now the Current Node, the one that's been added
  182. Start = NewNode;
  183.  
  184. //Print list
  185. printf("New node added at the begining of the list!\n\n");
  186. PrintListForward();
  187. }
  188. return;
  189. }
  190. //CASE 2 -------------> Add Node to Last part of the list
  191. void AddToBack() {
  192.  
  193. Node * NewNode;
  194.  
  195. if (Start == 0)
  196. {
  197. //This list is empty. Create the first node
  198.  
  199. NewNode = (Node *)malloc(sizeof(Node));
  200. printf("\n");
  201. printf("Enter data for the node: ");
  202. scanf("%d", &NewNode->data);
  203. NewNode->Next = 0;
  204. NewNode->Prev = 0;
  205. Start = NewNode;
  206.  
  207. //Print list
  208. printf("\n");
  209. printf("Addition of First Node done!\n\n");
  210. PrintListForward();
  211. }
  212. else
  213. {
  214. //when the list is not empty we have to check every node until we find the last one
  215.  
  216. Node * Current;
  217. Current = Start;
  218.  
  219. while (Current->Next != 0)
  220. {
  221. Current = Current->Next;
  222. }
  223. //After finding the last node of the list we can create a new node
  224. NewNode = (Node *)malloc(sizeof(Node));
  225. printf("\n");
  226. printf("Enter data for the node: ");
  227. scanf("%d", &NewNode->data);
  228. printf("\n");
  229. //NewNode 2nd layer (Next) points to nothing
  230. NewNode->Next = 0;
  231. //NewNode 3rd layer (Prev) point to the start of the Current Node
  232. NewNode->Prev = Current;
  233. //The actual Node 2nd layer (Next) points to the Node that is been added
  234. Current->Next = NewNode;
  235. //Print list
  236. printf("New node added at the end of the list!\n\n");
  237. PrintListForward();
  238. }
  239. return;
  240. }
  241. //CASE 3 -------------> Add to middle after
  242. void AddToMiddle() {
  243.  
  244. if (Start == 0)
  245. {
  246. printf("\n");
  247. printf("List is empty! Please add nodes to list");
  248. printf("\n");
  249.  
  250. return;
  251. }
  252.  
  253. int findData, findIt;
  254. Node * Current;
  255. Node * NextOfCurrent;
  256.  
  257. findIt = 0;
  258. Current = Start;
  259. NextOfCurrent = Current->Next;
  260.  
  261.  
  262. printf("\n");
  263. printf("Please insert data of node you want to add: ");
  264. scanf("%d", &findData);
  265. printf("\n");
  266.  
  267. while (Current != 0)
  268. {
  269. if (Current->data == findData)
  270. {
  271. findIt = 1;
  272. break;
  273. }
  274.  
  275. Current = Current->Next;
  276. if (Current == 0) // If Current is 0 (is the end of the list) the finish the while
  277. {
  278. break;
  279. }
  280. NextOfCurrent = Current->Next;
  281.  
  282. }if (findIt == 0)
  283. {
  284. printf("We can't find the node!");
  285. return;
  286. }
  287. //If data is found we can add a new node next to it
  288. Node * NewNode;
  289. NewNode = (Node*)malloc(sizeof(Node));
  290. printf("\n");
  291. printf("Enter data for the new node: ");
  292. scanf("%d", &NewNode->data);
  293. printf("\n");
  294.  
  295. if (NextOfCurrent == 0)
  296. {
  297. Current->Next = NewNode;
  298. NewNode->Prev = Current;
  299. NewNode->Next = 0;
  300. printf("The node is succesfully added after ");
  301. printf("%d\n", findData);
  302. PrintListForward();
  303. return;
  304. }
  305.  
  306. Current->Next = NewNode;
  307. NewNode->Prev = Current;
  308. NextOfCurrent->Prev = NewNode;
  309. NewNode->Next = NextOfCurrent;
  310. printf("The new node was added after: ");
  311. printf("%d", findData);
  312. printf("\n");
  313.  
  314. PrintListForward();
  315. }
  316. //CASE 4 ------------->
  317. void AddBeforeNode() {
  318.  
  319. if (Start == 0)
  320. {
  321. printf("\n");
  322. printf("List is empty! Please add nodes to list");
  323. printf("\n");
  324.  
  325. return;
  326. }
  327.  
  328. int findData, findIt;
  329. Node * Current;
  330. Node * PrevOfCurrent;
  331.  
  332. findIt = 0;
  333. Current = Start;
  334. PrevOfCurrent = Current->Prev;
  335.  
  336. printf("\n");
  337. printf("Please insert data of node you want to add: ");
  338. scanf("%d", &findData);
  339. printf("\n");
  340.  
  341. while (Current != 0)
  342. {
  343. if (Current->data == findData)
  344. {
  345. findIt = 1;
  346. break;
  347. }
  348.  
  349. Current = Current->Next;
  350. if (Current == 0) // If Current is 0 (is the end of the list) the finish the while
  351. {
  352. break;
  353. }
  354. PrevOfCurrent = Current->Prev;
  355.  
  356. }if (findIt == 0)
  357. {
  358. printf("We can't find the node!");
  359. return;
  360. }
  361. //If data is found we can add a new node next to it
  362. Node * NewNode;
  363. NewNode = (Node*)malloc(sizeof(Node));
  364. printf("\n");
  365. printf("Enter data for the new node: ");
  366. scanf("%d", &NewNode->data);
  367. printf("\n");
  368.  
  369.  
  370. if (PrevOfCurrent != 0)
  371. {
  372. Current->Prev = NewNode;
  373. NewNode->Next = Current;
  374. PrevOfCurrent->Next = NewNode;
  375. printf("\n");
  376. printf("The node is succesfully added after ");
  377. printf("%d\n", findData);
  378. printf("The new node was added before: ");
  379. printf("%d", findData);
  380. printf("\n");
  381. PrintListForward();
  382. return;
  383. }
  384. if (PrevOfCurrent == 0)
  385. {
  386. printf("The node can't be add before the first node, use option 1 in menu\n\n");
  387. PrintListForward();
  388. }
  389. }
  390. //CASE 5 -------------> Delete first Node of the list
  391. void DeleteFirstNode() {
  392.  
  393. if (Start == 0)
  394. {
  395. printf("\n");
  396. printf("List is empty! Please add nodes to list");
  397. printf("\n");
  398.  
  399. return;
  400. }
  401.  
  402. Node * Current;
  403. Node * NextOfCurrent;
  404.  
  405. Current = Start;
  406. NextOfCurrent = Current->Next;
  407.  
  408. //When there's only one we empty the list again
  409. if (NextOfCurrent == 0)
  410. {
  411. Start = 0;
  412. }
  413. //If there are more nodes in the list
  414. else
  415. {
  416. NextOfCurrent->Prev = 0;
  417. Start = NextOfCurrent;
  418. }
  419. free(Current);
  420. }
  421. //CASE 6 -------------> Delete last Node of the list
  422. void DeleteLastNode() {
  423.  
  424. if (Start == 0)
  425. {
  426. printf("\n");
  427. printf("List is empty! Please add nodes to list");
  428. printf("\n");
  429.  
  430. return;
  431. }
  432.  
  433. Node * NextOfCurrent;
  434. Node * Current;
  435.  
  436. Current = Start;
  437. NextOfCurrent = Current->Next;
  438.  
  439. if (NextOfCurrent == 0 && Current->Prev == 0)
  440. {
  441. Start = 0;
  442. free(Current);
  443. //Print list
  444. printf("\n");
  445. printf("The list is now empty, you delete the only node!\n\n");
  446. PrintListForward();
  447.  
  448. return;
  449. }
  450. //when the list is not empty we have to check every node until we find the last one
  451.  
  452. while (NextOfCurrent->Next != 0)
  453. {
  454. Current = Current->Next;
  455. NextOfCurrent = Current->Next;
  456. }
  457.  
  458. //After finding the last node of the list we can delete node
  459.  
  460. //Current is now the last one and we can delete Next of current
  461. Current->Next = 0;
  462. free(NextOfCurrent);
  463.  
  464. //Print list
  465. printf("\n");
  466. printf("Last Node is been delete!\n\n");
  467. PrintListForward();
  468.  
  469. return;
  470. }
  471. //CASE 7 -------------> Delete After Node
  472. void DeleteAfterNode() {
  473. //Case 1 Empty list, return to menu
  474. if (Start == 0)
  475. {
  476. printf("\n");
  477. printf("List is empty! Please add nodes to list");
  478. printf("\n");
  479.  
  480. return;
  481. }
  482.  
  483. int findData, findIt;
  484. Node * Current;
  485. Node * NextOfCurrent;
  486.  
  487. findIt = 0;
  488. Current = Start;
  489.  
  490. NextOfCurrent = Current->Next;
  491.  
  492. printf("\n");
  493. printf("Please insert data of node you want to delete after: ");
  494. scanf("%d", &findData);
  495. printf("\n");
  496.  
  497. //Find the node enter by user
  498. while (Current != 0)
  499. {
  500. if (Current->data == findData)
  501. {
  502. //You found the node
  503. findIt = 1;
  504. break;
  505. }
  506. Current = Current->Next;
  507. if (Current == 0) // If Current is 0 (is the end of the list) the finish the while
  508. {
  509. break;
  510. }
  511. NextOfCurrent = Current->Next;
  512. }
  513. if (findIt == 0) //Case 5 Node enter by user can't be found
  514. {
  515. printf("We can't find the node!");
  516. return;
  517. }
  518.  
  519. //when the user wants to delete the node after the last.
  520. if (Current->Next == 0)
  521. {
  522. printf("You can't delete this one, because there is no node next to delete");
  523. return;
  524. }
  525. //when you want to delete the last node
  526. if (NextOfCurrent->Next == 0)
  527. {
  528. printf("You just delete the last node!\n");
  529. Current->Next = 0;
  530. free(NextOfCurrent);
  531. PrintListForward();
  532. return;
  533. }
  534.  
  535.  
  536. Current->Next = NextOfCurrent->Next;
  537. NextOfCurrent->Prev = Current;
  538. free(NextOfCurrent);
  539. printf("The node after %d was delete.", findData);
  540. printf("\n");
  541.  
  542. PrintListForward();
  543.  
  544. return;
  545. }
  546. //CASE 8 -------------> Delete Before Node
  547. void DeleteBeforeNode() {
  548.  
  549. //Case 1 Empty list, return to menu
  550. if (Start == 0)
  551. {
  552. printf("\n");
  553. printf("List is empty! Please add nodes to list");
  554. printf("\n");
  555.  
  556. return;
  557. }
  558.  
  559. int findData, findIt;
  560. Node * Current;
  561. Node * PrevOfCurrent;
  562.  
  563. findIt = 0;
  564. Current = Start;
  565.  
  566. PrevOfCurrent = Current->Prev;
  567.  
  568. printf("\n");
  569. printf("Please insert data of node you want to delete before: ");
  570. scanf("%d", &findData);
  571. printf("\n");
  572.  
  573. //Find the node enter by user
  574. while (Current != 0)
  575. {
  576. if (Current->data == findData)
  577. {
  578. //You found the node
  579. findIt = 1;
  580. break;
  581. }
  582. Current = Current->Next;
  583. if (Current == 0) // If Current is 0 (is the end of the list) the finish the while
  584. {
  585. break;
  586. }
  587. PrevOfCurrent = Current->Prev;
  588. }
  589. if (findIt == 0) //Case 5 Node enter by user can't be found
  590. {
  591. printf("We can't find the node!");
  592. return;
  593. }
  594. //when the user wants to delete the node before the first.
  595. if (Current->Prev == 0)
  596. {
  597. printf("You can't delete this one, because there is no node before to delete");
  598. PrintListForward();
  599. return;
  600. }
  601. //when you want to delete the first node
  602. if (PrevOfCurrent->Prev == 0)
  603. {
  604. printf("The node can't be delete!, use option 5 in menu to delete first node\n\n");
  605. PrintListForward();
  606. return;
  607. }
  608.  
  609. Current->Prev = PrevOfCurrent->Prev;
  610. PrevOfCurrent->Prev->Next = Current;
  611.  
  612. printf("\n");
  613. printf("The node before %d was delete.", findData);
  614. printf("\n");
  615. PrintListForward();
  616.  
  617. return;
  618. }
  619. //CASE 9 ------------->Print List Forward
  620. void PrintListForward() {
  621.  
  622. if (Start == 0)
  623. {
  624. printf("\n");
  625. printf("List is empty! Please add nodes to list");
  626. printf("\n");
  627.  
  628. return;
  629. }
  630.  
  631. Node * Current;
  632.  
  633. // The Current Node (NewNode) created is using the variable Start
  634. Current = Start;
  635.  
  636. printf("\n");
  637. printf("This is the list:");
  638. printf("\n\n");
  639.  
  640. while (Current != 0)
  641. {
  642. printf("%d", Current->data);
  643. printf("->");
  644. Current = Current->Next;
  645. }
  646. printf("NULL\n");
  647.  
  648. return;
  649. }
  650. //CASE 10 -------------> Print List Backward
  651. void PrintListBack() {
  652. if (Start == 0)
  653. {
  654. printf("\n");
  655. printf("List is empty! Please add nodes to list");
  656. printf("\n");
  657.  
  658. return;
  659. }
  660.  
  661. Node * Current;
  662.  
  663. // The Current Node (NewNode) created is using the variable Start
  664. Current = Start;
  665.  
  666. printf("\n");
  667. printf("This is the list backwards:");
  668. printf("\n\n");
  669.  
  670. while (Current->Next != 0)
  671. {
  672. Current = Current->Next;
  673. }
  674. while (Current != 0)
  675. {
  676. printf("%d", Current->data);
  677. printf("->");
  678. Current = Current->Prev;
  679. }
  680. printf("NULL\n");
  681. }
  682. //CASE 11 -------------> Save Sequential file
  683. void saveSeqFile()
  684. {
  685. Node * Current;
  686. Current = Start;
  687.  
  688. FILE * CfPtr;
  689.  
  690. if (Current == NULL)
  691. {
  692. printf("it is a empty list");
  693. return;
  694. }
  695. if ((CfPtr = fopen("SequenceFile.dat", "w")) == NULL)
  696. {
  697. printf("there is no file\n");
  698. }
  699. else
  700. {
  701. CfPtr = fopen("SequenceFile.dat", "w");
  702.  
  703. while (Current != 0)
  704. {
  705. fprintf(CfPtr, "%d", Current->data);
  706. if (Current->Next != 0) //
  707. {
  708. fprintf(CfPtr, "%s", "\n");
  709. }
  710. Current = Current->Next;
  711. }
  712. }
  713.  
  714. printf("saved succesfully!\n");
  715. fclose(CfPtr);
  716.  
  717. }
  718. //CASE 12 -------------> Load Sequential file
  719. void loadSeqFile()
  720. {
  721. Node * current;
  722. current = Start;
  723. FILE * CfPtr;
  724. int tempInteger;
  725.  
  726. while (current != NULL)
  727. {
  728. current = current->Next;
  729. DeleteFirstNode();
  730. }
  731. //the list is empty
  732.  
  733. if ((CfPtr = fopen("SequenceFile.dat", "r")) == NULL)
  734. {
  735. printf("there is no file\n");
  736. }
  737. else
  738. {
  739. while (!feof(CfPtr)) //Not the end of file
  740. {
  741. fscanf(CfPtr, "%d", &tempInteger);
  742. createNewLinkList(tempInteger);
  743. }
  744. }
  745. fclose(CfPtr);
  746. PrintListForward();
  747. }
  748. //FUNCTION NOT IN MENU -------------> Using a argument to create a linked list
  749. void createNewLinkList(int aIntegerValue)
  750. {
  751. Node * NewNode;
  752. if (Start == 0)
  753. {
  754. //This list is empty. Create the first node
  755. NewNode = (Node *)malloc(sizeof(Node));
  756. NewNode->data = aIntegerValue;
  757. NewNode->Next = 0;
  758. NewNode->Prev = 0;
  759. Start = NewNode;
  760. }
  761. else
  762. {
  763. //when the list is not empty we have to check every node until we find the last one
  764. Node * Current;
  765. Current = Start;
  766. while (Current->Next != 0)
  767. {
  768. Current = Current->Next;
  769. }
  770.  
  771. NewNode = (Node *)malloc(sizeof(Node));
  772. NewNode->data = aIntegerValue;
  773. NewNode->Next = 0;
  774. NewNode->Prev = Current;
  775. Current->Next = NewNode;
  776. }
  777. return;
  778. }
  779. //CASE 13 -------------> Save Ramdom File
  780. void saveRdmFile(){
  781.  
  782. BlankRdmFile();
  783.  
  784. //add list is empty
  785.  
  786.  
  787.  
  788. Node * Current;
  789. Current = Start;
  790.  
  791. FILE *cfPtr;
  792.  
  793. //r+ read and write, read and more.
  794. if ((cfPtr = fopen("credit.dat", "r+")) == NULL)
  795. printf("File could not be opened.\n");
  796. else {
  797.  
  798. while (Current->data != 0) {
  799. printf("Enter number: \n");
  800. scanf("%d", Current->data);
  801.  
  802. //fseek will go to the file and skip some records before getting to the destination
  803. //SEEK_SET is to start from the begining of the file
  804. //&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.
  805. fseek(cfPtr, (Current->data - 1) *
  806. sizeof(int), SEEK_SET);
  807.  
  808. //write the info one time in the document.
  809. fwrite(&Current->data, sizeof(struct Node), 1, cfPtr);
  810. printf("Enter account number\n? ");
  811. scanf("%d", &Current->data);
  812. }
  813. }
  814.  
  815. fclose(cfPtr);
  816. }
  817.  
  818.  
  819.  
  820. //Create blank Ramdom file
  821. void BlankRdmFile(){
  822. int i;
  823.  
  824. //Use interger not struct because in this case it only has one data as int
  825. struct Node NodeData = { 0 };
  826.  
  827. FILE * cfPtr;
  828.  
  829. if ((cfPtr = fopen("NodeList.dat", "w")) == NULL)
  830. printf("File could not be opened.\n");
  831. else {
  832. //Write one blank client the same size of the struct, 1 time inside Document.
  833. //This loop will go for 100 times, it would create a file with 100 blank clients.
  834. for (i = 1; i <= 100; i++)
  835. fwrite(&NodeData,
  836. sizeof(struct Node), 1, cfPtr);
  837.  
  838. fclose(cfPtr);
  839. }
  840.  
  841. return;
  842. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement