Guest User

Untitled

a guest
Jan 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.88 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.     }
  192.     struct node* current = *headRef;
  193.     //struct node* temp;
  194.     while(current->next!=NULL){
  195.         if (current->data <= newNode->data && current->next->data >= newNode->data){
  196.             newNode->next = current->next;
  197.             current->next = newNode;
  198.             return;
  199.         }
  200.         current = current->next;
  201.     }
  202.     current->next = newNode;
  203.     newNode->next=NULL;
  204. }
  205.  
  206. //#7
  207.  
  208. // Given a list, change it to be in sorted order (using SortedInsert()).
  209. void Sort(struct node** headRef) {
  210.     struct node* result = NULL;
  211.     struct node* current = *headRef;
  212.     struct node* temp;
  213.  
  214.     while(current!=NULL){
  215.         temp = current->next;
  216.         SortedInsert(&result, current);
  217.         current = temp;
  218.     }
  219.  
  220.     *headRef = result;
  221. }
  222.  
  223. //#8
  224. // Append 'b' onto the end of 'a', and then set 'b' to NULL.
  225. void Append(struct node** aRef, struct node** bRef) {
  226.     if (*aRef==NULL){
  227.         *aRef= *bRef;
  228.         free(bRef);
  229.         return;
  230.     }
  231.  
  232.     struct node* current = *aRef;
  233.  
  234.     while(current!= NULL){
  235.         current = current->next;
  236.     }
  237.  
  238.     current = *bRef;
  239.     free(bRef);//? *bRef=NULL;
  240. }
  241.  
  242.  
  243. //#9
  244. /*
  245.  Split the nodes of the given list into front and back halves,
  246.  and return the two lists using the reference parameters.
  247.  If the length is odd, the extra node should go in the front list.
  248. */
  249. void FrontBackSplit(struct node* source, struct node** frontRef, struct node** backRef) {
  250.     struct node* current;
  251.     int length = 0;
  252.    
  253.     for(current = source;current!=NULL;current=current->next)
  254.         length++;
  255.     if (length%2==0){
  256.         length/=2;
  257.         *frontRef = source;
  258.         current = *frontRef;
  259.         for(int i =0;i<length;i++){
  260.             current = current->next;
  261.         }
  262.         *backRef= current->next;
  263.         current->next = NULL;
  264.     }
  265.  
  266. }
  267.  
  268.  
  269. //#10
  270.  
  271. /*
  272.  Remove duplicates from a sorted list.
  273. */
  274. void RemoveDuplicates(struct node* head) {
  275. // Your code...
  276. }
  277.  
  278. int main (){
  279.     //#1
  280.     printf("Q1 : %d\n", CountTest());
  281.     //#2
  282.     struct node* myList = BuildOneTwoThree(); // build {1, 2, 3}
  283.     displayList(myList);
  284.    
  285.     InsertNth(&myList, 0, 20);
  286.    
  287.     displayList(myList);
  288.     InsertNth(&myList, 4, 1);
  289.    
  290.     displayList(myList);
  291.  
  292.     Sort(&myList);
  293.  
  294.     displayList(myList);
  295.     return 0;
  296. }
Add Comment
Please, Sign In to add comment