468,253 Members | 1,239 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,253 developers. It's quick & easy.

Doubly Linked List

kim6987
12
can you please spend a little time evaluating this code.
I can not run it successfully

thanks :)

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #define SIZE 10
  5.  
  6. typedef struct dlist
  7. {
  8.  int val;
  9.  struct dlist *next,*prev;
  10. }*dll;
  11. //---------------- FUNCTION SEARCH_VAL --------------------------------------
  12. dll search_val(dll head,int data)
  13. {
  14.  dll temp;
  15.  temp=head;
  16.  while(temp)
  17.    {
  18.     if(temp->val==data)
  19.       return(1);
  20.     else
  21.       temp=temp->next;
  22.    }
  23.   return(0);
  24. }
  25. //---------------- FUNCTION ADD_BEG -----------------------------------------
  26. void add_beg(dll head,dll n_new)
  27. {
  28.  dll temp;
  29.  temp=head;
  30.  while(temp->prev!=NULL)
  31.      temp=temp->prev;
  32.  
  33.  n_new->next=temp->next;
  34.  temp->next->prev=n_new;
  35.  temp->next=n_new;
  36.  n_new->prev=temp;
  37. }
  38. //---------------- FUNCTION ADD_END  ----------------------------------------
  39. void add_end(dll head,dll n_new)
  40. {
  41.  dll temp;
  42.  temp=head;
  43.  while(temp->next!=NULL)
  44.    temp=temp->next;
  45.  temp->next=n_new;
  46.  n_new->prev=temp;
  47.  n_new->next=NULL;
  48. }
  49. //---------------- FUNCTION DELETE ------------------------------------------
  50. int delete(dll head,int data)
  51. {
  52.  dll temp;
  53.  temp=head;
  54.  while(temp)
  55.   {
  56.    if(temp->val==data)
  57.     {
  58.      temp->prev->next=temp->next;
  59.      temp->next->prev=temp->prev;
  60.      temp->prev=temp->next->next;
  61.      return(temp->val);
  62.     }
  63.   else
  64.    temp=temp->next;
  65.   }
  66.  
  67.  return(NULL);
  68. }
  69. //---------------- FUNCTION DISPLAY  ----------------------------------------
  70. void display(dll head)
  71. {
  72.  dll temp;
  73.  printf("\n\n\t THE DOUBLY LINKED LIST IS ::");
  74.  printf("HEAD->");
  75.  temp=head->next;
  76.  while(temp)
  77.   {
  78.    printf("<-%d->",temp->val);
  79.    temp=temp->next;
  80.   }
  81.  printf("NULL");
  82. }
  83. //---------------- FUNCTION GET_NODE-----------------------------------------
  84. dll get_node()
  85. {
  86.  dll n_new;
  87.  n_new=malloc(sizeof(struct dlist));
  88.  printf("\n\n\t ENTER THE VALUE ::");
  89.  scanf("%d",&n_new->val);
  90.  n_new->next=NULL;
  91.  n_new->prev=NULL;
  92.  return(n_new);
  93. }
  94. //---------------- FUNCTION CREATE ------------------------------------------
  95. dll create()
  96. {
  97.  dll head,n_new,last;
  98.  char c;
  99.  head=malloc(sizeof(struct dlist));
  100.  head->next=NULL;
  101.  head->prev=NULL;
  102.  last=head;
  103.  do
  104.  {
  105.   n_new=malloc(sizeof(struct dlist));
  106.   printf("\n\n\t ENTER THE VALUE ::");
  107.   scanf("%d",&n_new->val);
  108.   last->next=n_new;
  109.   n_new->prev=last;
  110.   n_new->next=NULL;
  111.   last=n_new;
  112.   printf("\n\n\t CREATE ANY MORE NODE (Y/N) ::");
  113.   c=getche();
  114.  }while(c=='y'||c=='Y');
  115.  
  116.  return(head);
  117. }
  118. //--------------------------------------------------------------------------
  119. void main()
  120. {
  121.  dll head,n_new;
  122.  int choice,data;
  123.  char c;
  124. // textcolor(14);
  125.  while(1)
  126.  {
  127.  //clrscr();
  128.  printf("\n\n\t THE DOUBLY LINKED LIST OPERATION :: ");
  129.  printf("\n\n\t 1> CREATE LIST");
  130.  printf("\n\n\t 2> DISPLAY");
  131.  printf("\n\n\t 3> ADD AT END");
  132.  printf("\n\n\t 4> ADD AT BEGINING");
  133.  printf("\n\n\t 5> SEARCH");
  134.  printf("\n\n\t 6> DELETE");
  135.  printf("\n\n\t 7> EXIT");
  136.  printf("\n\n\t ENTER YOUR CHOICE ::");
  137.  scanf("%d",&choice);
  138.  switch(choice)
  139.   {
  140.    case 1:head=create();
  141.           display(head);
  142.           break;
  143.    case 2:display(head);
  144.           break;
  145.    case 3:new=get_node();
  146.           add_end(head,n_new);
  147.           display(head);
  148.           break;
  149.    case 4:n_new=get_node();
  150.           add_beg(head,n_new);
  151.           display(head);
  152.           break;
  153.    case 5:printf("\n\n\t ENTER THE VALUE TO BE SEARCHED :: ");
  154.           scanf("%d",&data);
  155.           if(search_val(head,data))
  156.             {
  157.              printf("\n\n\t THE VALUE IS PRESENT IN THE LIST ");
  158.              display(head);
  159.             }
  160.           else
  161.             {
  162.              printf("\n\n\t THE VALUE IS NOT PRESENT IN THE LIST ");
  163.              display(head);
  164.             }
  165.           break;
  166.    case 6:printf("\n\n\t ENTER THE VALUE TO BE DELETED ::");
  167.           scanf("%d",&data);
  168.           if(search_val(head,data))
  169.             {
  170.              data=delete(head,data);
  171.              printf("\n\n\t THE DATA DELETED FROM THE LIST IS :: %d",data);
  172.              display(head);
  173.             }
  174.           else
  175.             {
  176.               if(head->next==NULL)
  177.                 {
  178.                  printf("\n\n\t THE LIST IS EMPTY ");
  179.                  display(head);
  180.                 }
  181.               else
  182.                {
  183.                 printf("\n\n\t THE DATA IS NOT PRESENT IN THE LIST ");
  184.                 display(head);
  185.                }
  186.             }
  187.           break;
  188.    case 7:printf("\n\n\t PRESS ESC TO EXIT ");
  189.           if(getch()==27)
  190.             exit(0);
  191.           break;
  192.  
  193.   }
  194.  
  195.   getch();
  196.  }
  197. }
  198. //--------------------------------------------------------------------------*/
  199.  
here are the error info. in Dev C
In function `dlist* search_val(dlist*, int)':
19 .invalid conversion from `int' to `dlist*'
19 At global scope:
50 expected unqualified-id before "delete"
......
Dec 7 '07 #1
4 2598
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. dll search_val(dll head,int data)
  2. {
  3.  dll temp;
  4.  temp=head;
  5.  while(temp)
  6.    {
  7.     if(temp->val==data)
  8.       return(1);
  9.     else
  10.       temp=temp->next;
  11.    }
  12.   return(0);
  13. }
Your function header says this returns a dll, but you only return 0 or 1.
Dec 7 '07 #2
kim6987
12
oh , thanks.
one more please :)
after I fix that one , anothers occur :


In function `int n_delete(dlist*, int)':
67 [Warning] converting to non-pointer type `int' from NULL
In function `dlist* get_node()':
87 invalid conversion from `void*' to `dlist*'

Dec 7 '07 #3
Ganon11
3,652 Expert 2GB
In the first case, you are now doing to opposite thing - returning NULL, which only has meaning when used with pointers, when your function promises an int return value.

In the second case, you are using malloc, but not casting the result to anything. malloc returns a void*, and you need a dll*.

Have you tried reading your errors? They provide the line of code where the error/warning occurs, and usually give an accurate description of what's going on. All I've done is look at the line number, go to that spot, and see another simple mistake. You can do the exact same thing without me, I'm sure.
Dec 7 '07 #4
kim6987
12
hi Ganon11 !

after reading your suggestion then I 've corrected them
about Null : I just ignore it . :)

// return(NULL);
about malloc

it now is

n_new=(struct dlist*) malloc(sizeof(struct dlist));
that's all I could do.
and then I compiled. It worked :but it run incorrectly with the delete function.( because of the return I ignored ?

definitely I need more reading about C

Thank you for your patience !
Dec 8 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by surrealtrauma | last post: by
4 posts views Thread by dssuresh6 | last post: by
5 posts views Thread by free2cric | last post: by
1 post views Thread by drewy2k12 | last post: by
2 posts views Thread by murali | last post: by
3 posts views Thread by maruf.syfullah | last post: by
5 posts views Thread by adam.kleinbaum | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.