home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18838 < prev    next >
Encoding:
Text File  |  1992-12-23  |  3.1 KB  |  135 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!spool.mu.edu!uwm.edu!ux1.cso.uiuc.edu!cs.uiuc.edu!sparc0b!ctaylor
  3. From: ctaylor@cs.uiuc.edu (Conrad Taylor)
  4. Subject: What's wrong with this code...???
  5. Message-ID: <BzqCsy.1v4@cs.uiuc.edu>
  6. Sender: news@cs.uiuc.edu
  7. Organization: University of Illinois at Urbana-Champaign
  8. Date: Wed, 23 Dec 1992 20:55:45 GMT
  9. Lines: 124
  10.  
  11.  
  12.     Question: why would the 'free(p)' in 'main' cause this program to go
  13. into an infinite loop?  I have enclosed complete source and not an abstract
  14. that can be viewed.  Also, how would one break out of an infinite loop in
  15. BBorland C/C++ 3.1? Thanks in advance to all that reply to this post.
  16.  
  17. ps:  My e-mail is taylor1@wse.eecs.uic.edu.
  18.  
  19. -------------------------------- Cut Here ------------------------------------
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23.  
  24.  
  25.  
  26. typedef int INT;
  27.  
  28. struct NODE {
  29.   INT info;
  30.   struct NODE* prev;
  31.   struct NODE* next;
  32.   };
  33.  
  34. typedef struct NODE* NODEPTR;
  35.  
  36. /* Function prototypes. */
  37. void DoublyLinkedInsert(NODEPTR p, NODEPTR q);
  38. void DoublyLinkedDelete(NODEPTR p);
  39. void DoublyLinkedPrint(NODEPTR head);
  40. NODEPTR GetNewNode(void);
  41.  
  42.  
  43.  
  44. NODEPTR GetNewNode(void)
  45. {
  46.    NODEPTR newNode;
  47.  
  48.    newNode = (NODEPTR)malloc(sizeof(struct NODE));
  49.    newNode->prev = newNode;
  50.    newNode->next = newNode;
  51.  
  52.    return(newNode);
  53. }
  54.  
  55.  
  56. void DoublyLinkedInsert(NODEPTR p, NODEPTR q)
  57. {
  58.    /* Insert node pointed to by p just after */
  59.    /* node pointed to by q. */
  60.  
  61.    p->prev = q;
  62.    p->next = q->next;
  63.    q->next->prev = p;
  64.    q->next = p;
  65. }
  66.  
  67. void DoublyLinkedDelete(NODEPTR p)
  68. {
  69.    /* Delete cell p from its doubly linked list. */
  70.  
  71.    p->prev->next = p->next;
  72.    p->next->prev = p->prev;
  73.    free(p);
  74. }
  75.  
  76. void DoublyLinkedPrint(NODEPTR head)
  77. {
  78.     NODEPTR temp = head->next;
  79.  
  80.     while(temp != head)
  81.       {
  82.          
  83.      printf("Value: %2d\n", temp->info);
  84.      temp = temp->next;
  85.       }
  86. }
  87.  
  88. INT main(void)
  89. {
  90.    NODEPTR head;
  91.    NODEPTR p;
  92.  
  93.    head = (NODEPTR)malloc(sizeof(struct NODE));
  94.    head->prev = head;
  95.    head->next = head;
  96.    head->info = -2;
  97.  
  98.    p = GetNewNode();
  99.    p->info = 1;
  100.    DoublyLinkedInsert(p, head);
  101. /*
  102.    printf("\nDEBUG 1:\n head->info is %d\n", head->info);
  103.    printf(" head->next->info is %d\n", head->next->info);
  104.    printf(" head->next->next->info is %d\n", head->next->next->info);
  105.    printf(" head->next->next->next->info is %d\n", head->next->next->next->info);
  106. */
  107.    p = GetNewNode();
  108.    p->info = 2;
  109.    DoublyLinkedInsert(p, head);
  110.    free(p); /* This is the line of code that I'm talking about above. */
  111. /*
  112.    printf("\nDEBUG 2:\n head->info is %d\n", head->info);
  113.    printf(" head->next->info is %d\n", head->next->info);
  114.    printf(" head->next->next->info is %d\n", head->next->next->info);
  115.    printf(" head->next->next->next->info is %d\n", head->next->next->next->info);
  116. */
  117.    DoublyLinkedPrint(head);
  118. /*
  119.    printf("\nDEBUG 3:\n head->info is %d\n", head->info);
  120.    printf(" head->next->info is %d\n", head->next->info);
  121.    printf(" head->next->next->info is %d\n", head->next->next->info);
  122.    printf(" head->next->next->next->info is %d\n", head->next->next->next->info);
  123. */
  124.  
  125.    free(head);
  126.    free(p);
  127.  
  128.    return 0;
  129. }
  130.  
  131.    
  132.     
  133.  
  134.   
  135.