Guest User

Untitled

a guest
Jan 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 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. void displayList(struct node* head){
  27.     struct node* current = head;
  28.         while(current!=NULL){
  29.                 printf("%d ", current -> data);
  30.                 current = current->next;
  31.     }
  32.     printf("\n");
  33. }
  34. /*
  35.  Takes a list and a data value.
  36.  Creates a new link with the given data and pushes
  37.  it onto the front of the list.
  38.  The list is not passed in by its head pointer.
  39.  Instead the list is passed in as a "reference" pointer
  40.  to the head pointer -- this allows us
  41.  to modify the caller's memory.
  42. */
  43. void Push(struct node** headRef, int data) {
  44.     struct node* newNode = (struct node*) malloc(sizeof(struct node));
  45.     newNode->data = data;
  46.     newNode->next = *headRef; // The '*' to dereferences back to the real head
  47.     *headRef = newNode; // ditto
  48. }
  49.  
  50. /*
  51.  A more general version of Push().
  52.  Given a list, an index 'n' in the range 0..length,
  53.  and a data element, add a new node to the list so
  54.  that it has the given index.
  55. */
  56.  
  57. void InsertNth(struct node** headRef, int index, int data) {
  58.     if(index==0){
  59.         Push(headRef, data);
  60.         return;
  61.     }
  62.     struct node* current = *headRef;
  63.  
  64.     struct node* newNode;
  65.     newNode = (struct node*) malloc(sizeof(struct node));
  66.     newNode->data = data;
  67.     int count =0;
  68.  
  69.     while(current!=NULL){
  70.         if (count==index-1){
  71.             newNode->next = current->next->next;
  72.             current->next = newNode;
  73.             return;
  74.         }
  75.         count++;
  76.         current = current->next;
  77.     }
  78. }
  79.  
  80. int main (){
  81.      struct node* myList = BuildOneTwoThree(); // build {1, 2, 3}
  82.      displayList(myList);
  83.      //{1, 2, 3}
  84.      InsertNth(&myList, 0, 13);
  85.      displayList(myList);
  86.      //{13, 1, 2, 3}
  87.      InsertNth(&myList, 1, 5);
  88.      displayList(myList);
  89.      //{13, 5, 1, 2, 3}
  90.      InsertNth(&myList, 6, 20);
  91.      displayList(myList);
  92.      //{13, 5, 1, 2, 3, 20}
  93.      displayList(myList);
  94.      return 0;
  95. }
Add Comment
Please, Sign In to add comment