Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct node node;
  4. struct node{
  5. int num;
  6. node *next;
  7. };
  8.  
  9. int main()
  10. {
  11. int type;
  12. node *ptr,*head=NULL,*newnode;
  13. while(EOF != scanf("%d",&type)){
  14. if(type > 0){
  15. newnode = malloc(sizeof(node));//add new node
  16. newnode -> num = type;//put type in
  17. if(head == NULL){//if is empty
  18. head = newnode;//then newnode is head
  19. newnode -> next = NULL;//next point to NULL
  20. }else{
  21. if(head -> num > newnode -> num){//if head > newnode
  22. newnode -> next = head;//then swich
  23. head = newnode;//swich
  24. }else{
  25. ptr = head;//checkpointer back to head
  26. while(ptr -> next != NULL){
  27. if(newnode -> num < ptr -> next -> num){
  28. break;//found the node >newnode
  29. }
  30. ptr = ptr -> next;//next one
  31. }
  32. if(ptr == NULL){//last one
  33. newnode -> next = NULL;//newnode's next point to NULL
  34. ptr -> next = newnode;//ptr's next point to newnode
  35. }else{// not last one
  36. newnode -> next = ptr -> next;//newnode's next point to ptr's next
  37. ptr -> next = newnode;//ptr's next point to newnode
  38. }
  39. }
  40. }
  41. }else if(type == 0){
  42. node *out = head;
  43. if(head == NULL){// no list
  44. printf("list is NULL");
  45. }
  46. while(out != NULL){
  47. printf("%d,",out->num);//print list
  48. out = out -> next;
  49. }
  50. }else{
  51. node *del,*search=head;
  52. while(search -> next != NULL){
  53. if(search -> num == abs(type)){//first del
  54. head = search -> next;//head move to sec.
  55. free(search);//freeeeeeeeeeeeeee
  56. break;
  57. }
  58. if(search -> next -> num == abs(type)){//normal case
  59. del = search -> next;//del is search next
  60. search -> next = del -> next;//search's next point to del's next
  61. free(del);//freeeeeeeeeeeeeee
  62. } else{
  63. search = search -> next;//move on
  64. }
  65.  
  66. }
  67. }
  68. }
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement