Thread: Error in program

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    4

    Error in program

    Here is my sample code... plz plz somebody help me with it... the error i am getting is for the function swap and i just need 2 call that....
    thanx a lot
    Code:
    #include<stdio.h>
    #include<conio.h>
    struct list{
        int t1;          /* Storing roll number of a node */
        struct list *next, *head;    /* Storing next address */
    };
    /*****  Redefining struct list as node  *****/
    typedef struct list node;
    void main()
    {
         node *head;
     head*= void swap(head);
     getch();
    }
    node* swap(node *current)
    {
    int t;                           /* Total number of nodes */
    int rno;
    node *temp;                      /* Temporary copy of current */
    node *tmp;        
        node *roll_no;               /* Temporary variable */
        t=number(current);
        if(t<=1)
        {
            printf("\nYou cannot swap the only node\n");
            return(current);
        }
        printf("\nEnter number whose node you want to swap with the next\n");
        scanf("%d",&rno);
        temp=current;
        if(t==rno)
        {
            tmp=current->next;
            current->next=current->next->next;
            tmp->next=current;
            current=tmp;
            return(current);
        }
        else
        {
            while(temp->next->next!=NULL)
            {
                if(temp->next->t==rno)
                {
                    tmp=temp->next->next;
                    temp->next->next=temp->next->next->next;
                    tmp->next=temp->next;
                    temp->next=tmp;
                    break;
                }
                temp=temp->next;
            }
            return(current);
        }
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    while(temp->next != NULL)
    {
       if(temp->t == rno)
       {
          tmp = temp->next;
          temp->next = temp->next->next;
          tmp->next = temp;
          break;
       }
       temp = temp->next;
    }
    ssharish

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    thanx a lot ssharish ... but i am getting error in function swap ..... where i am declaring

    node* swap(node *current)
    and calling it from

    node *head;
    head*= void swap(head);
    is this correct ????

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    head*= void swap(head);
    This is not right. since you are returning the head node from the swap function again. You not getting the void you are getting node *.

    So you will have to change that.

    Can you be more specific on what error you are getting. That would help a lot.

    EDIT: And forgot to mention you do not have function prototye for function Swap. Include this line on top your main function

    Code:
    node *swap(node *);
    And your main should return a value.
    Code:
    int main()
    {
       return 0;
    }
    And dont use getch(); which is a non standard function instead use getchar();

    ssharish
    Last edited by ssharish2005; 09-12-2007 at 05:59 PM.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    i am getting error

    Type mismatch in redeclaraiton of 'swap'

    thanx a lot again ssharish

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    int main()
    {
       node *head;
       
       head = swap( head );
       
       getchar();
       return 0;
    }
    Right, Change the main to that. And before you can send the list to the swap function. You need to build the list. So you need to have one more function. Are you aware of that?

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM