Abdulg

Linked List (C)

Aug 28th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.78 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. struct Node
  4. {
  5.     int Data;
  6.     struct Node *Next;
  7. }; 
  8.  
  9. struct Node *Head, *Tail;
  10.  
  11. void InsertAfter(int Value, int Position);
  12. void Delete(int Position);
  13. void PrintList();
  14.  
  15. int main()
  16. {
  17.     Head = malloc(sizeof(struct Node));
  18.     Tail = malloc(sizeof(struct Node));
  19.  
  20.     Head->Next = Tail;
  21.     Head->Data = 0;
  22.     Tail->Next = NULL;
  23.     Tail->Data = 2146;
  24.  
  25.     char Input[5] = "Hello";
  26.     int a, b;
  27.  
  28.     while (fgets(Input, 6, stdin) != NULL && Input[0] != 'q')
  29.     {
  30.         a = Input[2] - '0';
  31.         b = Input[4] - '0'; //problem here
  32.    
  33.         if (Input[0] == 'i')
  34.             InsertAfter(a,b);
  35.  
  36.         else if (Input[0] == 'd')
  37.             Delete(a);
  38.  
  39.         else if (Input[0] == 'p')
  40.             PrintList();
  41.     }
  42.  
  43.     return 0;
  44. }
  45.  
  46. void InsertAfter(int Value, int Position)
  47. {
  48.     struct Node *Iterator = Head;
  49.     struct Node *InsertMe = malloc(sizeof(struct Node));
  50.  
  51.     int i;
  52.     for (i = 0; i < Position && Iterator->Data != 2146; i++)
  53.         Iterator = Iterator->Next;
  54.    
  55.     InsertMe->Data = Value;
  56.     InsertMe->Next = Iterator->Next;
  57.     Iterator->Next = InsertMe;
  58.  
  59.     printf("Inserted %d into position %d\n\n", Value, Position);
  60. }
  61.  
  62. void Delete(int Position)
  63. {  
  64.     struct Node *Iterator = Head->Next;
  65.     struct Node *DeleteMe = malloc(sizeof(struct Node));
  66.  
  67.     int i;
  68.     for (i = 0; i < Position && Iterator->Data != 2146; i++)
  69.         Iterator=Iterator->Next;
  70.        
  71.     DeleteMe = Iterator->Next;
  72.     Iterator->Next = DeleteMe->Next;
  73.    
  74.     printf("Deleted %d from position %d\n\n", DeleteMe->Data, Position);
  75.     free(DeleteMe);
  76. }
  77.  
  78. void PrintList()
  79. {
  80.     struct Node *Iterator = Head->Next;
  81.     printf("Printing list:\n");
  82.     while (Iterator->Data != 2146)
  83.     {
  84.         printf("%d ", Iterator->Data);
  85.         Iterator = Iterator->Next;
  86.     }
  87.     printf("\n\n");
  88. }
  89.  
  90. /*
  91. abdul@debian:~/Programming/C$ make test
  92. gcc LinkedList.c -o LinkedList
  93. LinkedList.c: In function ‘main’:
  94. LinkedList.c:17:9: warning: incompatible implicit declaration of built-in function ‘malloc’
  95.   Head = malloc(sizeof(struct Node));
  96.          ^
  97. LinkedList.c: In function ‘InsertAfter’:
  98. LinkedList.c:49:26: warning: incompatible implicit declaration of built-in function ‘malloc’
  99.   struct Node *InsertMe = malloc(sizeof(struct Node));
  100.                           ^
  101. LinkedList.c: In function ‘Delete’:
  102. LinkedList.c:65:26: warning: incompatible implicit declaration of built-in function ‘malloc’
  103.   struct Node *DeleteMe = malloc(sizeof(struct Node));
  104.                           ^
  105. LinkedList.c:75:2: warning: incompatible implicit declaration of built-in function ‘free’
  106.   free(DeleteMe);
  107.   ^
  108. ./LinkedList
  109. i 1 0
  110. Inserted 1 into position 0
  111.  
  112. i 2 1
  113. Inserted 2 into position 1
  114.  
  115. i 3 2
  116. Inserted 3 into position 2
  117.  
  118. i 6 0
  119. Inserted 6 into position 0
  120.  
  121. i 6 3
  122. Inserted 6 into position 3
  123.  
  124. i 7 10
  125. Inserted 7 into position 1
  126.  
  127. d 3
  128. Deleted 6 from position 3
  129.  
  130. p
  131. Printing list:
  132. 6 7 1 2 3
  133. */
Advertisement
Add Comment
Please, Sign In to add comment