Advertisement
Guest User

Single List

a guest
Apr 26th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5.    int value;
  6.   struct node *next;
  7. }*t1,*t2,*t3;
  8. struct node* head = NULL; //(struct node *)malloc(sizeof(struct node));
  9. //End of structure
  10.  
  11. void ins()
  12. {
  13.   int data;
  14.   printf("\nEnter Data:");
  15.   scanf("%d",&data);
  16.   t1 = (struct node*)malloc(sizeof(struct node));
  17.  
  18. if(head == NULL)
  19. {
  20.        t1->value = data;
  21.      t1->next  = NULL;  
  22.  
  23.      head = t1;
  24.    
  25.    
  26. }// end of if
  27.  
  28. else
  29. {
  30. t1->value = data;  
  31. t1->next = head;
  32.    head = t1;
  33. }// end of if
  34. }// Insert
  35.  
  36. void dels(int pos)
  37. {
  38.   t2 = head;
  39.  
  40.   if(head == NULL){
  41.  
  42.    printf("list empty");
  43. }//end of if
  44.  
  45. else{
  46.  
  47.    while(t2->next->value != pos)
  48.  {
  49.    t2 = t2->next;
  50. }
  51.  
  52.    t3 = t2->next;
  53.    t2->next = t3->next;
  54.    free(t3);
  55. }// end of else
  56. }//end of dels
  57.  
  58. void disp()
  59. {
  60.    struct node *t4 = head;
  61.   if(head == NULL){
  62.  
  63.     printf("\n Empty");
  64. }
  65.   else
  66. {
  67.    while(t4->next != NULL)
  68. {
  69.    printf(" %d ",t4->value);
  70.    t4 = t4->next;
  71. }
  72.  
  73. }
  74. }
  75. int main()
  76. {
  77.    int ch;
  78.   int val;
  79.    char x = 'y';
  80.    while(x  != 'n')
  81. {
  82.    printf("ch: ");
  83.    scanf("%d",&ch);
  84.    if(ch == 1)
  85. {
  86.  
  87.    ins();
  88.  
  89. }
  90.   if(ch == 2)
  91. {
  92.    
  93.    printf("Val: ");
  94.    scanf("%d",&val);
  95.   dels(val);
  96. }
  97.  
  98. if(ch == 3){
  99. disp();
  100. }
  101.  
  102. printf(" y/n: ");
  103. scanf("%c",&x);
  104. }//end of while
  105. return 0;
  106. }// eom
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement