fork download
  1. //Linked list basic functions
  2.  
  3. #include <stdio.h>
  4. #define NHASH 100
  5.  
  6. struct node
  7. {
  8. int data;
  9. struct node *next;
  10. };
  11.  
  12. struct treenode
  13. {
  14. int data;
  15. struct treenode* left;
  16. struct treenode* right;
  17. };
  18.  
  19. struct node* bin[NHASH];
  20. struct node* readlistfromarray(int array[], int n);
  21. struct node * readlistfromfile(FILE *fp);
  22. struct node* readlistfromstdin();
  23. struct node* readfrombst(struct treenode*);
  24. struct node** makehashtable(struct node* head);
  25.  
  26. void print(struct node *head);
  27. // then sth to read the list from input or from a file?
  28.  
  29. struct node * insertattail(struct node *head,int data);
  30. struct node * insertathead(struct node *head,int data);
  31. struct node * insertnexttohead(struct node *head,int data);
  32.  
  33. struct node * deltail(struct node *head);
  34. struct node * delhead(struct node *head);
  35.  
  36. struct node* reverse(struct node *head);
  37. struct node* sort(struct node *head);
  38. struct node* ispallindrome(struct node *head);
  39. struct node* mergesort(struct node* first,struct node *second);
  40.  
  41. struct node* insertattail(struct node *head,int data)
  42. {
  43. //First construct the node in newp
  44. struct node *newp;
  45. struct node *p;
  46. newp=malloc(sizeof(struct node));
  47. newp->data=data;
  48. newp->next=NULL;
  49.  
  50. //Standard name for node pointers only used for traversal? p? q? tmp? if more than 1 kind of nodes?
  51. // Then what about temp pointers for swapping,adjusting etc but not exactly for traversal?
  52. // Make your own naming conventions for commonly needed kind of variables.
  53.  
  54. if(head == NULL) // is there more elegant way of dealing with this? any idiom?
  55. {
  56. head=newp;
  57. return head;
  58. }
  59.  
  60. for(p=head;p->next!=NULL;p=p->next)
  61. ;
  62.  
  63. p->next=newp;
  64. return head;
  65. }
  66.  
  67. struct node* insertathead(struct node *head,int data)
  68. {
  69. //First construct a new node;
  70. struct node *new;
  71. new=malloc(sizeof(struct node));
  72. new->data=data;
  73. new->next=NULL;
  74.  
  75. new->next=head;
  76. return new;
  77. }
  78.  
  79. struct node* insertnexttohead(struct node *head,int data)
  80. {
  81. //if head exists insert next to it else put it in head.
  82. //First construct a new node;
  83. struct node *tmp; //should be onetimetmp? singleadjust? adjustonce? adjust? what does Brian name it?
  84. struct node *new;
  85. new->data=data;
  86. new->next=NULL;
  87.  
  88. if(head == NULL)
  89. {
  90. new->next=head;
  91. return head;
  92. }
  93.  
  94. tmp=head->next;
  95. head->next=new;
  96. new->next=tmp;
  97. return head;
  98. }
  99.  
  100. struct node * readlistfromarray(int array[], int n)
  101. {
  102. // Make use of already written insert functions.
  103. int i;
  104. struct node *head=NULL;
  105. for(i=0;i<n;i++)
  106. {
  107. head=insertattail(head,array[i]);
  108. //head=insertathead(head,array[i]);
  109. }
  110. return head;
  111. }
  112.  
  113. void print(struct node *head)
  114. {
  115. struct node *p;
  116. for(p=head;p!=NULL;p=p->next)
  117. printf("%d\n",p->data);
  118. }
  119.  
  120. main()
  121. {
  122. struct node *head;
  123. int array[5]={3,1,2,4,2};
  124. head=readlistfromarray(array,5);
  125. print(head);
  126. }
  127.  
stdin
Standard input is empty
compilation info
prog.c: In function ‘insertattail’:
prog.c:46: warning: implicit declaration of function ‘malloc’
prog.c:46: warning: incompatible implicit declaration of built-in function ‘malloc’
prog.c: In function ‘insertathead’:
prog.c:71: warning: incompatible implicit declaration of built-in function ‘malloc’
prog.c: At top level:
prog.c:121: warning: return type defaults to ‘int’
prog.c: In function ‘main’:
prog.c:126: warning: control reaches end of non-void function
prog.c: In function ‘insertnexttohead’:
prog.c:85: warning: ‘new’ is used uninitialized in this function
stdout
3
1
2
4
2