Advertisement
dsdeep

HW 3

Sep 29th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct linklist
  4. {
  5.     int data;
  6.     struct linklist *next;
  7. } node;
  8. void newjoin (node *t, int data)
  9. {
  10.     while (t->next != NULL)
  11.     {
  12.         t = t->next;
  13.     }
  14.     t->next = (node*)malloc(sizeof(node));
  15.     t->next->data = data;
  16.     t->next->next = NULL;
  17. }
  18. void display(node *t)
  19. {
  20.     while (t!= NULL)
  21.     {
  22.         printf("%d ", t->data);
  23.         t = t->next;
  24.     }
  25. }
  26. void insert_after(node *t, int x,int data)
  27. {
  28.     int count=0;
  29.     while(t!= NULL)
  30.     {
  31.         count++;
  32.         if(count==x)
  33.         break;
  34.        t = t->next;
  35.     }
  36.     node *tmp=t->next;
  37.     t->next = (node*)malloc(sizeof(node));
  38.     t->next->data = t->data;
  39.     t->next->next = tmp;
  40.     t->data=data;
  41. }
  42. int main()
  43. {
  44.     int data, i;
  45.     node *tab = (node*)malloc(sizeof(node));
  46.     tab->next = NULL;
  47.     printf("                             Input five elements here\n");
  48.     for(i=0; i<5; i++)
  49.     {
  50.         printf("Input data for your node %d: ",i+1);
  51.         scanf("%d", &data);
  52.         newjoin(tab,data);
  53.     }
  54.     printf("\nThe elements are - ");
  55.     display(tab);
  56.     int nth;
  57.     printf("\nInsert 95 at which position: ");
  58.     scanf("%d", &nth);
  59.     insert_after(tab,nth,95);
  60.     // for(i=1; i<6; i++)
  61.     // {
  62.     //     printf("DEEP\n");
  63.  
  64.     // }
  65.     display(tab);
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement