473,320 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Some error occurred in program of Single Linked List

Ajay Bhalala
119 64KB
Hello all,

I have write the code (program) for the Single Linked List using C Language.

Here is my program...
Expand|Select|Wrap|Line Numbers
  1. //Program of Single Linked List
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<alloc.h>
  5.  
  6. void create();
  7. void insert();
  8. void display();
  9. void insert_first();
  10. void insert_last();
  11. void count();
  12. void search();
  13. void insert_after_position();
  14. void insert_after_search_element();
  15. void insert_before_search_element();
  16. void delete_first();
  17. void delete_last();
  18. void delete_search();
  19. void delete_at_position();
  20. void delete_after_position();
  21. void delete_before_position();
  22. void search_update();
  23. void update_position();
  24. void reverse();
  25. void split_list();
  26. void merge_list();
  27. void sorted_insert();
  28.  
  29. //Structure Difination
  30. struct node
  31. {
  32.     int data;
  33.     struct node *next;
  34. }*head=NULL,*head1=NULL;
  35.  
  36. struct node *temp,*q;
  37.  
  38. //Void main started
  39. void main()
  40. {
  41.     int ch;
  42.     clrscr();
  43.     do
  44.     {
  45.         printf("\n\n\n");
  46.         //Giving the choices for the linked list operation
  47.         printf("--------------------------------------------------------------\n");
  48.         printf("\t[1].Create List\n");
  49.         printf("\t[2].Insert\n");
  50.         printf("\t[3].Display\n");
  51.         printf("\t[4].\n");
  52.         printf("\t[5].\n");
  53.         printf("\t[6].\n");
  54.         printf("\t[7].\n");
  55.         printf("\t[8].\n");
  56.         printf("\t[9].\n");
  57.         printf("\t[10].\n");
  58.         printf("\t[11].\n");
  59.         printf("\t[12].\n");
  60.         printf("\t[13].\n");
  61.         printf("\t[14].\n");
  62.         printf("\t[15].\n");
  63.         printf("\t[16].\n");
  64.         printf("\t[17].\n");
  65.         printf("\t[18].\n");
  66.         printf("\t[19].\n");
  67.         printf("\t[20].\n");
  68.         printf("\t[21].\n");
  69.         printf("\t[22].\n");
  70.         printf("\t[23].\n");
  71.         printf("----------------------------------------------------------------");
  72.  
  73.         printf("\n Enter Your Choice :- ");
  74.         scanf("%d",&ch);
  75.  
  76.         switch(ch)
  77.         {
  78.             case 1:
  79.                 create();
  80.                 break;
  81.  
  82.             case 2:
  83.                 insert();
  84.                 break;
  85.  
  86.             case 3:
  87.                 display();
  88.                 break;
  89.  
  90.             case 4:
  91.                 insert_first();
  92.                 break;
  93.  
  94.             case 5:
  95.                 insert_last();
  96.                 break;
  97.  
  98.             case 6:
  99.                 count();
  100.                 break;
  101.  
  102.             case 7:
  103.                 search();
  104.                 break;
  105.  
  106.             case 8:
  107.                 insert_after_position();
  108.                 break;
  109.  
  110.             case 9:
  111.                 insert_after_search_element();
  112.                 break;
  113.  
  114.             case 10:
  115.                 insert_before_search_element();
  116.                 break;
  117.  
  118.             case 11:
  119.                 delete_first();
  120.                 break;
  121.  
  122.             case 12:
  123.                 delete_last();
  124.                 break;
  125.  
  126.             case 13:
  127.                 delete_search();
  128.                 break;
  129.  
  130.             case 14:
  131.                 delete_at_position();
  132.                 break;
  133.  
  134.             case 15:
  135.                 delete_before_position();
  136.                 break;
  137.  
  138.             case 16:
  139.                 delete_after_position();
  140.                 break;
  141.  
  142.             case 17:
  143.                 search_update();
  144.                 break;
  145.  
  146.             case 18:
  147.                 update_position();
  148.                 break;
  149.  
  150.             case 19:
  151.                 reverse();
  152.                 break;
  153.  
  154.             case 20:
  155.                 split_list();
  156.                 break;
  157.  
  158.             case 21:
  159.                 merge_list();
  160.                 break;
  161.  
  162.             case 22:
  163.                 sorted_insert();
  164.                 break;
  165.  
  166.             case 23:
  167.                 exit();
  168.  
  169.             default:
  170.                 printf("\n Enter Proper Choice!");
  171.         }
  172.     }while(ch<=23);
  173.     getch();
  174. }
  175.  
  176. //Difining the user defined functions.
  177. void create()
  178. {
  179.     int d;
  180.     if(head==NULL)
  181.     {
  182.         printf("\n Enter any number :- ");
  183.         scanf("%d",&d);
  184.  
  185.         temp=(struct node *)malloc(sizeof(struct node));
  186.  
  187.         temp->data=d;
  188.         temp->next=NULL;
  189.         head=temp;
  190.         main();
  191.     }
  192.     else
  193.     {
  194.         printf("\n List is already created!");
  195.         return;
  196.     }
  197.     display();
  198. }
  199.  
  200. void insert()
  201. {
  202.     if(head==NULL)
  203.     {
  204.         printf("\n Linked List is not Created!");
  205.         create();
  206.     }
  207.     else
  208.     {
  209.         temp=(struct node*)malloc(sizeof(struct node));
  210.         printf("\n Enter any number :- ");
  211.         scanf("%d",&(temp->data));
  212.  
  213.         temp->next=NULL;
  214.  
  215.         q=head;
  216.  
  217.         while(q->next!=NULL)
  218.         {
  219.             q=q->next;
  220.         }
  221.         q->next=temp;
  222.     }
  223.     display();
  224. }
  225.  
  226. void display()
  227. {
  228.     if(head==NULL)
  229.     {
  230.         printf("\n Linked List is not Created!");
  231.         create();
  232.     }
  233.     else
  234.     {
  235.         q=head;
  236.         while(q!=NULL)
  237.         {
  238.             printf("\n%d",q->data);
  239.             q=q->next;
  240.         }
  241.     }
  242. }
  243.  
  244. void insert_first()
  245. {
  246.     temp=(struct node*)malloc(sizeof(struct node));
  247.     printf("\n Enter the data you want to enter at the first position :- ");
  248.     scanf("%d",&temp->data);
  249.     temp->next=head;
  250.     head=temp;
  251.     display();
  252. }
  253.  
  254. void insert_last()
  255. {
  256.     temp=(struct node*)malloc(sizeof(struct node));
  257.     printf("\n Enter the data :- ");
  258.     scanf("%d",&temp->data);
  259.     temp->next=NULL;
  260.     q=head;
  261.     while(q->next!=NULL)
  262.     {
  263.         q=q->next;
  264.     }
  265.     q->next=temp;
  266.     display();
  267. }
  268.  
  269. void count()
  270. {
  271.     int c=0;
  272.     q=head;
  273.     while(q!=NULL)
  274.     {
  275.         c++;
  276.         q=q->next;
  277.     }
  278.     printf("\n The total no of nodes in the list is :- %d",c);
  279. }
  280.  
  281. void search()
  282. {
  283.     int s,f=0,c=0;
  284.     if(head==NULL)
  285.     {
  286.         printf("\n Linked List is not created!");
  287.         return;
  288.     }
  289.     printf("\n Enter any data to be searched :- ");
  290.     scanf("%d",&s);
  291.     q=head;
  292.     while(q!=NULL)
  293.     {
  294.         c++;
  295.         if(q->data==s)
  296.         {
  297.             f++;
  298.             printf("\n Entered value is found at position %d.",c);
  299.         }
  300.         q=q->next;
  301.     }
  302.     if(f==0)
  303.     {
  304.         printf("\n The no is not Found!");
  305. }
  306.  
  307. void insert_after_position()
  308. {
  309.     int p,l;
  310.     printf("\n After which position, you want to add the data? :- ");
  311.     scanf("%d",&p);
  312.     q=head;
  313.     for(i=0;i<p-1;i++)
  314.     {
  315.         q=q->next;
  316.         if(q==NULL)
  317.         {
  318.             printf("\n Invalid Position");
  319.             return;
  320.         }
  321.     }
  322.     temp=(struct node*)malloc(sizeof(struct node));
  323.     printf("\n Enter the data :- ");
  324.     scanf("%d",&tmp->data);
  325.     temp->next=q->next;
  326.     q->next=temp;
  327.     display();
  328. }
  329.  
  330. void insert_after_search_element()
  331. {
  332.     int s;
  333.     if(head==NULL)
  334.     {
  335.         printf("\n Linked List is not Created");
  336.         return;
  337.     }
  338.     printf("\n After which element, you want to add the data? :- ");
  339.     scanf("%d",&s);
  340.     q=head;
  341.     while(q!=NULL)
  342.     {
  343.         if(s==q->data)
  344.         {
  345.             temp=(struct node*)malloc(sizeof(struct node));
  346.             printf("\n Enter the data :- ");
  347.             scanf("%d",&temp->data);
  348.             temp->next=q->next;
  349.             q->next=temp;
  350.         }
  351.         q=q->next;
  352.     }
  353.     display();
  354. }
  355.  
  356. void insert_before_search_element()
  357. {
  358.     int s;
  359.     struct node *p;
  360.     temp=(struct node*)malloc(sizeof(struct node));
  361.     if(head==NULL)
  362.     {
  363.         printf("\n Linked List is not created!");
  364.         return;
  365.     }
  366.     printf("\n Before which element, you want to add the data? :- ");
  367.     scanf("%d",&s);
  368.     q=head;
  369.     while(q!=NULL)
  370.     {
  371.         if(s==q->data)
  372.         {
  373.             temp=(struct node*)malloc(sizeof(struct node));
  374.             printf("\n Enter the data :- ");
  375.             scanf("%d",&temp->data);
  376.             p->next=temp;
  377.             temp->next=q;
  378.         }
  379.         p=q;
  380.         q=q->next;
  381.     }
  382.     display();
  383. }
  384.  
  385. void delete_first()
  386. {
  387.     if(head==NULL)
  388.     {
  389.         printf("\n Linked List is not created!");
  390.         return;
  391.     }
  392.     temp=head;
  393.     if(head->next==NULL)
  394.     {
  395.         free(temp);
  396.         head=NULL;
  397.         printf("\n Linked List is empty!");
  398.         return;
  399.     }
  400.     head=head->next;
  401.     printf("\n Deleted value is %d.",q->data);
  402.     free(temp);
  403.     display();
  404. }
  405.  
  406. void delete_last()
  407. {
  408.     struct node *temp,*q;
  409.     q=head;
  410.     while(q->next!=NULL)
  411.     {
  412.         temp=q;
  413.         q=q->next;
  414.     }
  415.     temp->next=NULL;
  416.     printf("\n Deleted value is %d.",q->data);
  417.     free(q);
  418.     display();
  419. }
  420.  
  421. void delete_search()
  422. {
  423.     int a,f=0;
  424.     printf("\n Which element you want to delete? :- ");
  425.     scanf("%d",&s);
  426.     if(head==NULL)
  427.     {
  428.         printf("\n Linked List is not created!");
  429.         return;
  430.     }
  431.     q=head;
  432.     if(head->data==s)
  433.     {
  434.         head=head->next;
  435.         printf("\n Deleted value is %d.",q->data);
  436.         free(q);
  437.         return;
  438.     }
  439.     while(q!=NULL)
  440.     {
  441.         temp=q;
  442.         q=q->next;
  443.         if(s==q->data)
  444.         {
  445.             f=1;
  446.             temp->next=q->next;
  447.             printf("\n Deleted Value is %d.",q->data);
  448.             free(q);
  449.             return;
  450.         }
  451.     }
  452.     if(f==0)
  453.     {
  454.         printf("\n Value not found.");
  455.     }
  456.     display();
  457. }
  458.  
  459. void delete_at_position()
  460. {
  461.     int pos,i;
  462.     if(head==NULL)
  463.     {
  464.         printf("\n Linked List is not created!");
  465.         return;
  466.     }
  467.     printf("\n Enter the position :- ");
  468.     scanf("%d",&pos);
  469.     q=head;
  470.     for(i=0;i<pos-1;i++)
  471.     {
  472.         temp=q;
  473.         q=q->next;
  474.         if(q==NULL)
  475.         {
  476.             printf("\n Invalid Position!");
  477.             return;
  478.         }
  479.     }
  480.     if(q==head)
  481.     {
  482.         head=head->next;
  483.         printf("\n Deleted Value is %d.",q->data);
  484.         free(q);
  485.         return;
  486.     }
  487.     temp->next=q->next;
  488.     printf("\n Deleted Value is %d.",q->data);
  489.     free(q);
  490.     display();
  491. }
  492.  
  493. void delete_after_position()
  494. {
  495.     int i,pos;
  496.     printf("\n Enter the Position :- ");
  497.     scanf("%d",&pos);
  498.     q=head;
  499.     for(i=0;i<=pos-1;i++)
  500.     {
  501.         temp=q;
  502.         q=q->next;
  503.         if(q==NULL)
  504.         {
  505.             printf("\n Invalid Position!");
  506.             return;
  507.         }
  508.     }
  509.     temp->next=q->next;
  510.     printf("\n Deleted Value is %d.",q->data);
  511.     free(q);
  512.     display();
  513. }
  514.  
  515. void delete_before_position()
  516. {
  517.     int i,pos;
  518.     printf("\n Enter the Position :- ");
  519.     scanf("%d",&pos);
  520.     if(pos==2)
  521.     {
  522.         temp=head;
  523.         head=head->next;
  524.         free(temp);
  525.         return;
  526.     }
  527.     q=head;
  528.     for(i=1;i<=pos-2;i++)
  529.     {
  530.         temp=q;
  531.         q=q->next;
  532.         if(q==NULL)
  533.         {
  534.             printf("\n Invalid Position!");
  535.             return;
  536.         }
  537.     }
  538.     temp->next=q->next;
  539.     printf("\n Deleted Value is %d.",q->data);
  540.     free(q);
  541.     display();
  542. }
  543.  
  544. void search_update()
  545. {
  546.     int s,f=0;
  547.     if(head==NULL)
  548.     {
  549.         printf("\n Linked List is not created!");
  550.         return;
  551.     }
  552.     printf("\n Enter the element to be search :- ");
  553.     scanf("%d",&s);
  554.     q=head;
  555.     while(q!=NULL)
  556.     {
  557.         if(q->data==s)
  558.         {
  559.             printf("\n Enter the new data :- ");
  560.             scanf("%d",&q->data);
  561.             f=1;
  562.             display();
  563.             return;
  564.         }
  565.         q=q->next;
  566.     }
  567.     if(f==0)
  568.     {
  569.         printf("\n Element not found!");
  570.     }
  571.     display();
  572. }
  573.  
  574. void update_position()
  575. {
  576.     int pos,i,s;
  577.     if(head==NULL)
  578.     {
  579.         printf("\n Linked List is not created!");
  580.         return;
  581.     }
  582.     printf("\n Enter the Position :- ");
  583.     scanf("%d",&pos);
  584.     q=head;
  585.     for(i=0;i<pos-1;i++)
  586.     {
  587.         q=q->next;
  588.         if(q==NULL)
  589.         {
  590.             printf("\n Invalid Position!");
  591.             return;
  592.         }
  593.     }
  594.     printf("\n Enter the new value :- ");
  595.     scanf("%d",&(q->data));
  596.     display();
  597. }
  598.  
  599. void reverse()
  600. {
  601.     struct node *p1,*p2,*p3;
  602.     if(head==NULL)
  603.     {
  604.         printf("\n Linked List is not created!");
  605.     }
  606.     else if(head->next==NULL)
  607.     {
  608.         {
  609.             printf("\n Only one element is created!");
  610.         }
  611.     }
  612.     else
  613.     {
  614.         p1=head;
  615.         p2=p1->next;
  616.         p3=p2->next;
  617.         p1->next=NULL;
  618.         p2->next=NULL;
  619.         while(p3!=NULL)
  620.         {
  621.             p1=p2;
  622.             p2=p3;
  623.             p3=p2->next;
  624.             p2->next=p1;
  625.         }
  626.         head=p2;
  627.     }
  628.     printf("\n The Reversed Linked List is Successful!");
  629.     printf("\n Press any key ...");
  630.     getch();
  631.     main();
  632. }
  633.  
  634. void split_list()
  635. {
  636.     int pos,i;
  637.     if(head==NULL)
  638.     {
  639.         printf("\n Linked List is not Created!");
  640.     }
  641.     else
  642.     {
  643.         printf("\n Enter the Position you want to split :- ");
  644.         scanf("%d",&pos);
  645.         q=head;
  646.         for(i=1;i<pos-1;i++)
  647.         {
  648.             q=q->next;
  649.         }
  650.         head1=q->next;
  651.         q->next=NULL;
  652.         printf("\n The spliting list are this");
  653.         q=head1;
  654.         while(q!=NULL)
  655.         {
  656.             printf("\n %d",q->data);
  657.             q=q->next;
  658.         }
  659.         printf("\n The First Linked List is this");
  660.         q=head;
  661.         while(q!=NULL)
  662.         {
  663.             printf("\n %d",q->data);
  664.             q=q->next;
  665.         }
  666.         printf("\n\t\t\t The Spliting is Successful");
  667.         printf("\n Press any key ...");
  668.         getch();
  669.         main();
  670. }
  671.  
  672. void merge_list()
  673. {
  674.     int pos,i;
  675.     struct node *d;
  676.     if(head==NULL)
  677.     {
  678.         printf("\n Linked List is not creadted!");
  679.     }
  680.     else
  681.     {
  682.         printf("\n Enter the position you want to split :- ");
  683.         scanf("%d",&pos);
  684.         for(i=0;i<pos-1;i++)
  685.         {
  686.             q=q->next;
  687.         }
  688.         head1=q->next;
  689.         q->next=NULL;
  690.         printf("\n The Spliting List are this");
  691.         q=head1;
  692.         while(q!=NULL)
  693.         {
  694.             printf("\n %d",q->data);
  695.             q=q->next;
  696.         }
  697.         printf("\n The First Linked List is this");
  698.         q=head;
  699.         while(q!=NULL)
  700.         {
  701.             printf("\n %d",q->data);
  702.             q=q->next;
  703.         }
  704.         printf("\n The Merged Linked List is this");
  705.         q=head;
  706.         while(q!=NULL)
  707.         {
  708.             printf("\n %d",a->data);
  709.             q=q->next;
  710.         }
  711.         q->next=head1;
  712.         d=head1;
  713.         while(d!=NULL)
  714.         {
  715.             printf("\n %d",d->data);
  716.             d=d->next;
  717.         }
  718.         printf("\t\t\n Merging is Successful!");
  719.         printf("\t\n Press any key ...");
  720.         getch();
  721.         main();
  722. }
  723.  
  724. void sorted_insert()
  725. {
  726.     temp=(struct node *)malloc(sizeof(struct node));
  727.     printf("\n Enter your next data :- ");
  728.     scanf("%d",&(temp->data));
  729.     temp->next=NULL;
  730.     q=head;
  731.     if(head==NULL||temp->data<q->data)
  732.     {
  733.         temp->next=head;
  734.         head=temp;
  735.     }
  736.     else
  737.     {
  738.         while(q->next!=NULL && q->next->data<temp->data)
  739.         {
  740.             q=q->next;
  741.         }
  742.         temp->next=q->next;
  743.         q->next=temp;
  744.     }
  745.     display();
  746. }
  747.  
When I run my this program, there are 4 errors occurred.
Here are the errors:

Error 1:
Error DS\SLL.C 307: Declaration is not allowed here
Expand|Select|Wrap|Line Numbers
  1. ....
  2. ...
  3. void insert_after_position()
  4. {
  5. ...
  6. ...
  7.  
Error 2:
Error DS\SLL.C 308: Declaration syntax error
Expand|Select|Wrap|Line Numbers
  1. ...
  2. ...
  3. {
  4.     int p,l;
  5. ...
  6. ...
  7.  
Error 3:
Error DS\SLL.C 746: Declaration missing ;
Expand|Select|Wrap|Line Numbers
  1. ...
  2. ...
  3.     display();
  4. }
  5.  
Error 4:
Error DS\SLL.C 746: Compound statement missing }
Expand|Select|Wrap|Line Numbers
  1. ...
  2. ...
  3.     display();
  4. }
  5.  
What changes I have to do in my above program?

Please help me. Thank You in advance.
Jan 25 '15 #1

✓ answered by weaknessforcats

Just compile your code.

Fix only the first error.

Often the first error causes other errors and by fixing it, the others disappear.

Repeat compiling until your errors are gone.

Then you can start debugging the program.

5 1288
weaknessforcats
9,208 Expert Mod 8TB
You have a lot of errors in this code.

The biggest one is the missing } at the end of search().

Also, you call main(). While you can do this, your main is not re-entrant and so you can't call it.

There are also various other errors that are stopping you from compiling.
Jan 25 '15 #2
donbock
2,426 Expert 2GB
You declare main() to return void. That's wrong - it must return int.
Jan 26 '15 #3
Ajay Bhalala
119 64KB
Thank you so much for your replies weeknessforcat and donbock.

But can you say which changes I have to do?
Jan 26 '15 #4
weaknessforcats
9,208 Expert Mod 8TB
Just compile your code.

Fix only the first error.

Often the first error causes other errors and by fixing it, the others disappear.

Repeat compiling until your errors are gone.

Then you can start debugging the program.
Jan 26 '15 #5
Ajay Bhalala
119 64KB
Thank you for your reply.

I have make changes as you say, my program is now error free. And its work properly. Thank you for your help
Jan 26 '15 #6

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

Similar topics

4
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
11
by: sam_cit | last post by:
Hi Everyone, I want to actually reverse a single linked list without using many variables, i have a recurssive solution, but i wanted an iterative one. can anyone help me on this?
4
by: gzeng | last post by:
Is there any simple way to move the next pointer back to the beginning of a single linked list after it travers thru the list?
1
by: prasadi01 | last post by:
Hi, How can I find the 3rd last item in Single Linked List efficiently. thanks
20
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
2
by: Bhagyagali | last post by:
Is it possible to delete a middle node in the single linked list when the only information available we have is the pointer to the node to be deleted and not the pointer to the previous node?After...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.