View Single Post
Join Date: Aug 2008
Posts: 13
Reputation: rajenpandit is an unknown quantity at this point 
Solved Threads: 1
rajenpandit rajenpandit is offline Offline
Newbie Poster

Re: linked list deleteing problem

 
0
  #4
Nov 21st, 2008
  1. struct node
  2. {
  3. int data;
  4. struct node *link;
  5. };
  6.  
  7.  
  8. void delet(struct node **currNode,int location)
  9. {
  10. int i;
  11. struct node *temp,*temp1,*temp2;
  12. int cnt;
  13. temp= *currNode;
  14. cnt=count(currNode);
  15. if(location>cnt)
  16. {
  17. printf("\nIndex should be <= %d",cnt);
  18. }
  19. for(i=1;i<=cnt;i++)
  20. {
  21. if(location==1&&i==1)
  22. {
  23. *currNode=temp->link;
  24. free(temp);
  25. }
  26. if(i==location-1)
  27. {
  28. temp1=temp->link;
  29. temp->link=temp->link->link;
  30. free(temp1);
  31. }
  32. temp=temp->link;
  33. }
  34. }
  35. int count(struct node **currNode)
  36. {
  37. int count;
  38. struct node *temp;
  39. temp= *currNode;
  40. if(*currNode==NULL)
  41. {
  42. count=0;
  43. }
  44. else
  45. count=1;
  46. while(temp->link!=NULL)
  47. {
  48. temp=temp->link;
  49. count++;
  50. }
  51. return count;
  52. }
  53.  
  54.  
  55. void main()
  56. {
  57. int value,index;
  58. struct node *currNode;
  59. currNode=NULL;
  60. printf("Enter Index : ");
  61. scanf("%d",&value);
  62. //delete node in the given index of a link list
  63. delet(&currNode,value);
  64. }
Reply With Quote