Guest User

Untitled

a guest
Jan 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node {
  5.     int data;
  6.     struct node* next;
  7. };
  8.  
  9. struct node* BuildOneTwoThree() {
  10.     struct node* head = NULL;
  11.     struct node* second = NULL;
  12.     struct node* third = NULL;
  13.     head = (struct node*) malloc(sizeof(struct node)); // allocate 3 nodes in the heap
  14.     second = (struct node*) malloc(sizeof(struct node));
  15.     third = (struct node*) malloc(sizeof(struct node));
  16.     head->data = 1; // setup first node
  17.     head->next = second; // note: pointer assignment rule
  18.     second->data = 2; // setup second node
  19.     second->next = third;
  20.     third->data = 3; // setup third link
  21.     third->next = NULL;
  22.     // At this point, the linked list referenced by "head"
  23.     // matches the list in the drawing.
  24.     return head;
  25. }
  26.  
  27. /*
  28.  Takes a list and a data value.
  29.  Creates a new link with the given data and pushes
  30.  it onto the front of the list.
  31.  The list is not passed in by its head pointer.
  32.  Instead the list is passed in as a "reference" pointer
  33.  to the head pointer -- this allows us
  34.  to modify the caller's memory.
  35. */
  36. void Push(struct node** headRef, int data) {
  37.     struct node* newNode = (struct node*) malloc(sizeof(struct node));
  38.     newNode->data = data;
  39.     newNode->next = *headRef; // The '*' to dereferences back to the real head
  40.     *headRef = newNode; // ditto
  41. }
  42. void PushTest() {
  43.     struct node* head = BuildOneTwoThree();// suppose this returns the list {2, 3}
  44.     Push(&head, 1); // note the &
  45.     Push(&head, 13);
  46.     // head is now the list {13, 1, 2, 3}
  47. }
  48.  
  49. // Return the number of nodes in a list (while-loop version)
  50. int Length(struct node* head) {
  51.     int count = 0;
  52.     struct node* current = head;
  53.     while (current != NULL) {
  54.     count++;
  55.     current = current->next;
  56.     }
  57.     return(count);
  58. }
  59.  
  60. void displayList(struct node* head){
  61.     for(struct node* current = head;current!=NULL;current = current->next)
  62.         printf("%d ", current -> data);
  63.     printf("\n");
  64. }
  65.  
  66. //#1
  67. /*
  68.  Given a list and an int, return the number of times that int occurs
  69.  in the list.
  70. */
  71. int Count(struct node* head, int searchFor) {
  72.     int count = 0;
  73.     struct node *current;
  74.     for(current = head;current !=NULL;current = current->next){
  75.         if (current->data == searchFor)
  76.             count++;
  77.     }
  78.     return count;
  79. }
  80.  
  81. int CountTest() {
  82.     struct node* myList = BuildOneTwoThree(); // build {1, 2, 3}
  83.     return Count(myList, 2); // returns 1 since there's 1 '2' in the list
  84. }
  85.  
  86. //#2
  87. // Given a list and an index, return the data
  88. // in the nth node of the list. The nodes are numbered from 0.
  89. // Assert fails if the index is invalid (outside 0..lengh-1).
  90. int GetNth(struct node* head, int index) {
  91.     int count = 0;
  92.     struct node* current;
  93.     for(current =head;current!=NULL;current=current->next){
  94.         if (count == index)
  95.             return current->data;
  96.         count++;
  97.     }
  98.     return -1;
  99. }
  100.  
  101. //#3
  102. void DeleteList(struct node** headRef) {
  103.     struct node* current = *headRef;
  104.     struct node* temp;
  105.     while(current!=NULL){
  106.         temp= current;
  107.         current = current->next;
  108.         free(temp);
  109.     }
  110.     *headRef = NULL;
  111. }
  112.  
  113. void DeleteListTest() {
  114.     struct node* myList = BuildOneTwoThree(); // build {1, 2, 3}
  115.     DeleteList(&myList); // deletes the three nodes and sets myList to NULL
  116. }
  117.  
  118. //#4
  119. /*
  120.  The opposite of Push(). Takes a non-empty list
  121.  and removes the front node, and returns the data
  122.  which was in that node.
  123. */
  124. int Pop(struct node** headRef) {
  125.     if (*headRef==NULL)
  126.         return -1;
  127.     struct node* current = *headRef;
  128.     int result = current->data;
  129.     *headRef = (*headRef)->next;
  130.     free(current);
  131.     return result;
  132. }
  133.  
  134. void PopTest() {
  135.     struct node* head = BuildOneTwoThree(); // build {1, 2, 3}
  136.     int a = Pop(&head); // deletes "1" node and returns 1
  137.     int b = Pop(&head); // deletes "2" node and returns 2
  138.     int c = Pop(&head); // deletes "3" node and returns 3
  139.     int len = Length(head); // the list is now empty, so len == 0
  140. }
  141.  
  142.  
  143.  
  144. //#5
  145. /*
  146.  A more general version of Push().
  147.  Given a list, an index 'n' in the range 0..length,
  148.  and a data element, add a new node to the list so
  149.  that it has the given index.
  150. */
  151. void InsertNth(struct node** headRef, int index, int data) {
  152.     if(index==0){
  153.         Push(headRef, data);
  154.         return;
  155.     }
  156.     struct node* current = *headRef;
  157.  
  158.     struct node* newNode;
  159.     newNode = (struct node*) malloc(sizeof(struct node));
  160.     newNode->data = data;
  161.     int count =0;
  162.  
  163.     while(current!=NULL){
  164.         if (count==index-1){
  165.             newNode->next = current->next;
  166.             current->next = newNode;
  167.             return;
  168.         }
  169.         count++;
  170.         current = current->next;
  171.     }
  172. }
  173.  
  174.  
  175. void InsertNthTest() {
  176.     struct node* head = NULL; // start with the empty list
  177.     InsertNth(&head, 0, 13); // build {13)
  178.     InsertNth(&head, 1, 42); // build {13, 42}
  179.     InsertNth(&head, 1, 5); // build {13, 5, 42}
  180.     displayList(head);
  181.     DeleteList(&head); // clean up after ourselves
  182. }
  183.  
  184.  
  185.  
  186. //#6
  187. void SortedInsert(struct node** headRef, struct node* newNode) {
  188.     if (*headRef == NULL || (*headRef)->data >= newNode -> data){
  189.         newNode->next = *headRef;
  190.         *headRef = newNode;
  191.         return;
  192.     }
  193.     struct node* current = *headRef;
  194.     //struct node* temp;
  195.     while(current->next!=NULL){
  196.         if (current->data <= newNode->data && current->next->data >= newNode->data){
  197.             newNode->next = current->next;
  198.             current->next = newNode;
  199.             return;
  200.         }
  201.         current = current->next;
  202.     }
  203.     current->next = newNode;
  204.     newNode->next=NULL;
  205. }
  206.  
  207. //#7
  208.  
  209. // Given a list, change it to be in sorted order (using SortedInsert()).
  210. void Sort(struct node** headRef) {
  211.     struct node* result = NULL;
  212.     struct node* current = *headRef;
  213.     struct node* temp;
  214.  
  215.     while(current!=NULL){
  216.         temp = current->next;
  217.         SortedInsert(&result, current);
  218.         current = temp;
  219.     }
  220.  
  221.     *headRef = result;
  222. }
  223.  
  224. //#8
  225. // Append 'b' onto the end of 'a', and then set 'b' to NULL.
  226. void Append(struct node** aRef, struct node** bRef) {
  227.     if (*aRef==NULL){
  228.         *aRef= *bRef;
  229.         *bRef = NULL;
  230.         return;
  231.     }
  232.  
  233.     struct node* current = *aRef;
  234.  
  235.     while(current->next!= NULL){
  236.         current = current->next;
  237.     }
  238.  
  239.     current->next = *bRef;
  240.     *bRef=NULL;
  241. }
  242.  
  243.  
  244. //#9
  245. /*
  246.  Split the nodes of the given list into front and back halves,
  247.  and return the two lists using the reference parameters.
  248.  If the length is odd, the extra node should go in the front list.
  249. */
  250. void FrontBackSplit(struct node* source, struct node** frontRef, struct node** backRef) {
  251.     struct node* current;
  252.     int length = 0;
  253.    
  254.     for(current = source;current!=NULL;current=current->next)
  255.         length++;
  256.     if (length<2){
  257.         *frontRef = source;
  258.         *backRef = NULL;
  259.     }
  260.     else {
  261.         length = (length-1)/2;
  262.         current = source;
  263.        
  264.         for(int i =0;i<length;i++){
  265.             current = current->next;
  266.         }
  267.         *frontRef = source;
  268.         *backRef= current->next;
  269.         current->next = NULL;
  270.     }
  271. }
  272.  
  273.  
  274. //#10
  275.  
  276. /*
  277.  Remove duplicates from a sorted list.
  278. */
  279. void RemoveDuplicates(struct node* head) {
  280.     if (head == NULL)
  281.         return;
  282.     struct node* current = head;
  283.     struct node* temp;
  284.     while(current->next!=NULL){
  285.         if(current->data == current->next->data){
  286.             temp = current->next;
  287.             current->next = current->next->next;
  288.             free(temp);
  289.         }
  290.         else{
  291.             current = current->next;
  292.         }
  293.     }
  294. }
  295.  
  296. //#11
  297. /*
  298.  Take the node from the front of the source, and move it to
  299.  the front of the dest.
  300.  It is an error to call this with the source list empty.
  301. */
  302. void MoveNode(struct node** destRef, struct node** sourceRef) {
  303.     if(sourceRef == NULL)
  304.         return;
  305.     struct node* current = *sourceRef;
  306.     *sourceRef = (*sourceRef)->next;
  307.     current->next = *destRef;
  308.     *destRef = current;
  309. }
  310.  
  311. void MoveNodeTest() {
  312.     struct node* a = BuildOneTwoThree(); // the list {1, 2, 3}
  313.     struct node* b = BuildOneTwoThree();
  314.     displayList(a);
  315.     displayList(b);
  316.     printf("\nnow\n");
  317.     MoveNode(&a, &b);
  318.     displayList(a);
  319.     displayList(b);
  320.     // a == {1, 1, 2, 3}
  321.     // b == {2, 3}
  322. }
  323.  
  324. //#12
  325. /*
  326.  Given the source list, split its nodes into two shorter lists.
  327.  If we number the elements 0, 1, 2, ... then all the even elements
  328.  should go in the first list, and all the odd elements in the second.
  329.  The elements in the new lists may be in any order.
  330. */
  331. void AlternatingSplit(struct node* source, struct node** aRef, struct node** bRef) {
  332.     if(source==NULL)
  333.         return;
  334.  
  335.     struct node* temp1;
  336.     temp1 = (struct node*) malloc(sizeof(struct node));
  337.  
  338.     struct node* temp2;
  339.     temp2 = (struct node*) malloc(sizeof(struct node));
  340.  
  341.     struct node* current = source;
  342.  
  343.     int count1 = 0;
  344.     int count2 = 0;
  345.    
  346.     for(int i = 0; i<Length(source);i++){
  347.         if (i%2==0&&count1==0){
  348.             *aRef = current;
  349.             count1++;
  350.             temp1 = *aRef;         
  351.         }
  352.         else if (i%2==0){
  353.             temp1->next = current;
  354.             temp1 = temp1->next;
  355.         }
  356.         else if (i%2!=0&&count2==0){
  357.             *bRef = current;
  358.             count2++;          
  359.             temp2 = *bRef;
  360.         }
  361.         else if (i%2!=0){
  362.             temp2->next = current;
  363.             temp2=temp2->next;
  364.         }
  365.         current = current->next;
  366.     }
  367. }
  368.  
  369. /*void AlternatingSplit(struct node* source, struct node** aRef, struct node** bRef){
  370.     struct node* a = NULL; // Split the nodes to these 'a' and 'b' lists
  371.     struct node* b = NULL;
  372.  
  373.     struct node* current = source;
  374.     while(current!=NULL){
  375.         MoveNode(&a, &current);
  376.         if (current!=NULL)
  377.             MoveNode(&b, &current);
  378.     }
  379.     *aRef = a;
  380.     *bRef = b;
  381. }*/
  382.  
  383.  
  384. int main (){
  385.     //#2
  386.     struct node* myList = BuildOneTwoThree(); // build {1, 2, 3}
  387.     DeleteList(&myList);
  388.     InsertNth(&myList, 0, 1);
  389.     InsertNth(&myList, 1, 2);
  390.     InsertNth(&myList, 2, 1);  
  391.     InsertNth(&myList, 3, 2);
  392.     InsertNth(&myList, 4, 1);
  393.     InsertNth(&myList, 5, 2);
  394.    
  395.     struct node* evenList;
  396.     struct node* oddList;
  397.  
  398.     evenList = (struct node*) malloc (sizeof(struct node));
  399.     oddList = (struct node*) malloc (sizeof(struct node));
  400.  
  401.     evenList = NULL;
  402.     oddList = NULL;
  403.  
  404.     displayList(evenList);
  405.     displayList(oddList);
  406.     displayList(myList);
  407.  
  408.     AlternatingSplit(myList, &evenList, &oddList);
  409.        
  410.     displayList(evenList);
  411.     displayList(oddList);
  412.     displayList(myList);
  413.  
  414.     return 0;
  415. }
Add Comment
Please, Sign In to add comment