Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. struct Node {
  2. int data;
  3. struct Node* next;
  4. };
  5. struct Node* head;
  6. void Reverse(struct Node* head) {
  7. struct Node *current, *prev, *next;
  8. current = head;
  9. prev = NULL;
  10. while(current != NULL) {
  11. next = current->next;
  12. current->next = prev;
  13. current = next;
  14. prev = current;
  15. }
  16. head = prev;
  17. }
  18. void Insert(int data, int n) {
  19. struct Node* temp1 = (struct Node*)malloc(sizeof(struct Node));
  20. temp1->data = data;
  21. temp1->next = NULL;
  22. if(n==1) {
  23. temp1->next = head;
  24. head = temp1;
  25. }
  26. int i;
  27. struct Node* temp2 = (struct Node*)malloc(sizeof(struct Node));
  28. for(i=0;i<n-2;i++) {
  29. temp2=temp2->next;
  30. }
  31. temp1->next = temp2->next;
  32. temp2->next = temp1;
  33. }
  34. void Delete(int n) {
  35. struct Node* temp1 = head;
  36. int i;
  37. for(i=0;i<n-2;i++) {
  38. temp1=temp1->next;
  39. }
  40. struct Node* temp2 = temp1->next;
  41. temp1->next = temp2->next;
  42. free(temp2);
  43. }
  44. void Print() {
  45. struct Node* temp = head;
  46. int i;
  47. printf("List is: ");
  48. while(temp->next != NULL) {
  49. printf(" %d", temp->data);
  50. temp=temp->next;
  51. }
  52. }
  53. int main() {
  54. head = NULL;
  55. Insert(4,1);
  56. Insert(8,2);
  57. Insert(6,3);
  58. Insert(3,4);
  59. Insert(9,5);
  60. Insert(23,6);
  61. Insert(12,2);
  62. Insert(45,5);
  63. Print();
  64. Delete(2);
  65. Print();
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement