472,783 Members | 1,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,783 software developers and data experts.

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 3015
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

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

Similar topics

3
by: surrealtrauma | last post by:
I want to ask what's the differences between doubly liked list and linear liked list, and also the circular doubly liked list in terms of implementation. THX
4
by: dssuresh6 | last post by:
Whether browsing forward or backward can be done using a singly linked list. Is there any specific case where a doubly linked list is needed? For people who say that singly linked list allows...
5
by: free2cric | last post by:
Hi, how to detect head and tail in cyclic doubly link list ? Thanks, Cric
1
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and...
2
by: murali | last post by:
Hi, I want to insert a node in an sorted doubly linked list of integers in ascending order. The list should not have duplicate nodes. I need an algorithm (assuming an object oriented language) ...
8
by: tonywinslow1986 | last post by:
I'm reading MIT's book "Introduction to Algorithms". The following is one of the excercises from it: < 10.2-8 Explain how to implement doubly linked lists using only one pointer value np per...
3
by: maruf.syfullah | last post by:
Consider the following Class definitions: class AClass { int ai1; int ai2; public: CClass* c; AClass(){}
5
by: adam.kleinbaum | last post by:
Hi there, I'm a novice C programmer working with a series of large (30,000 x 30,000) sparse matrices on a Linux system using the GCC compiler. To represent and store these matrices, I'd like to...
10
by: kalar | last post by:
Hello. we have this struct and we must to make a linked list struct node { char name; char phone; struct node *prevName; // previous node alphabetically struct node *prevNumber; //...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.