AllExperts > Experts 
Search      

C

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More C Answers
Question Library

Ask a question about C
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Mohamed Ameer Irshad.H
Expertise
can ask me questions regarding general concepts,algorithms in C.STLs(c++)TSR programmin,virus writing,basic level of device driver programming(linux),network programmming(UNIX C) ,UNIX shell scripting,grapihcs using TURBO C and a bit of hardware interaction..

Experience
just a student level experience but hav worked out of my academic contexts..

Organizations
Vellore Institute of Technology(University Of VIT)

Publications
Technical Publication Of NACISS(National Conference On Computational Intelligence and Security Systems)

Paper titled REMOTE DESKTOP ACCESS USING MOBILE PHONES AND AN ENHANCEMENT FOR INTRA-MOBILE SEARCH got selected in an international conference SNPD 2008,Thailand.And the paper will be published IEEE Journal by August 2008.

Paper titled REMOTE DESKTOP ACCESS USING MOBILE PHONES AND AN ENHANCEMENT FOR INTRA-MOBILE SEARCH got selected in International Conference on Information and Communication Technologies(ICT 2008) Paris,France.The same will be published in Proceedings of World Academy of Science, Engineering and Technology, Volume 30, July 2008. Education/Credentials
did a few courses in comp institutions in C and a few projects

Awards and Honors
Presented Papers in National Conferences

 
   

You are here:  Experts > Computing/Technology > C/C++ > C > Non-recursive function

Topic: C



Expert: Mohamed Ameer Irshad.H
Date: 3/19/2008
Subject: Non-recursive function

Question
Write a non-recursive function in 'C' programming language to reverse a doubly linked list.

Answer
you dont need recursion to reverse a singly-linked list.
but recursion is a good technique to reverse a doubly-linked list.
why do you want without recursion?

for your clarification purpose i am sending a source code which contains all linked-list operations.

//All types of Linked List Operations

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
#include<dos.h>
void disp(struct node*);
struct node *addbeg(struct node *,int);
void addend(struct node *,int);
void sortlist(struct node*,int);
struct node *addbef(struct node *,int);
void addaft(struct node *,int);
void addbet(struct node *,int,int);
struct node *del(struct node *,int);
struct node *befdel(struct node *,int);
void aftdel(struct node *,int);
void betdel(struct node *,int,int);
void update(struct node *,int);
void search(struct node *,int);
struct node *reverse(struct node *);
struct node{ int n;
       struct node *next;
     } ;
void main()
{ char ch,boolc1,boolc2,boolc3,boolc4,boolc5,boolc6,boolc7;
 int
i,num,no,addb,adde,befadd,aftadd,fnode,snode,cut,befcut,aftcut,prnode,succ
node,change,find;
 struct node *head,*tail,*ptr;
 clrscr();
 printf("THIS IS A PROGRAM ABOUT LINKED LIST
");
 printf("supply no. of elements in the linked list
");
 scanf("%d",&num);
 head=tail=ptr=NULL;
 for(i=0;i<num;i++)
 { printf("supply new node
");
   scanf("%d",&no);
   ptr=(struct node*)malloc(sizeof(struct node));
   if(tail==NULL)
   { head=tail=ptr;
     ptr->n=no;
     ptr->next=NULL;
   }
   else
   { tail->next=ptr;
     ptr->n=no;
     ptr->next=NULL;
     tail=ptr;
   }
 }
 disp(head);
 printf("node you want to add before
");
 scanf("%d",&addb);
 if(addb>=0)
 { head=addbeg(head,addb);
 printf("Now");
 disp(head);
}
else  printf("ayou don't! OK
");
 printf("node you want to add end
");
 scanf("%d",&adde);
 if(adde>=0)
 { addend(head,adde);
 printf("Now");
 disp(head);
 }
 else  printf("ayou don't! OK
");
 printf("before which node you want to add?
");
 scanf("%d",&befadd);
 head=addbef(head,befadd);
 printf("Now");
 disp(head);
 printf("after which node you want to add?
");
 scanf("%d",&aftadd);
 addaft(head,aftadd);
 printf("Now");
 disp(head);
 printf("between which two nodes you want to add?
");
 fflush(stdin);
 scanf("%d %d",&fnode,&snode);
 addbet(head,fnode,snode);
 printf("Now");
 disp(head);
 printf("want to delete any node? (y/n)
");
 fflush(stdin);
 scanf("%c",&boolc1);
 if(boolc1=='y')   {  printf("supply node to be deleted
");
            scanf("%d",&cut);
            head=del(head,cut);
            printf("Now");
            disp(head);
        }
 else     printf("OK. list remains same
");
  printf("want to delete before any node? (y/n)
");
 fflush(stdin);
 scanf("%c",&boolc2);
 if(boolc2=='y')   { printf("supply that node
");
           scanf("%d",&befcut);
           head=befdel(head,befcut);
            printf("Now");
            disp(head);
        }
 else     printf("OK. list remains same
");
 printf("want to delete after any node? (y/n)
");
 fflush(stdin);
 scanf("%c",&boolc3);
 if(boolc3=='y')   { printf("supply that node
");
           scanf("%d",&aftcut);
           aftdel(head,aftcut);
            printf("Now");
            disp(head);
        }
 else     printf("OK. list remains same
");
 printf("want to delete node between any two node? (y/n)
");
 fflush(stdin);
 scanf("%c",&boolc4);
 if(boolc4=='y')    { printf("supply those nodes
");
            scanf("%d %d",&prnode,&succnode);
            betdel(head,prnode,succnode);
            printf("Now");
            disp(head);
          }
 else    printf("OK. list remains same
");
 printf("want to update any node? (y/n)
");
 fflush(stdin);
 scanf("%c",&boolc5);
 if(boolc5=='y')  {  printf("supply node to be updated
");
          scanf("%d",&change);
          update(head,change);
          printf("Now");
          disp(head);
        }
 else    printf("OK. list remains same
");
 printf("want to search any node? (y/n)
");
 fflush(stdin);
 scanf("%c",&boolc6);
 if(boolc6=='y')  { printf("node to be searched
");
          scanf("%d",&find);
          search(head,find);
        }
 else    printf("OK. list remains same
");
 printf("want to display the list in reverse order? (y/n)
");
 fflush(stdin);
 scanf("%c",&boolc7);
 if(boolc7=='y')  { printf("In reverse order");
          head=reverse(head);
          disp(head);
        }
 else    printf("OK. list remains same
");
 printf("wish to sort the list? (y/n)
");
 fflush(stdin);
 scanf("%c",&ch);
 if(ch=='y')
 { sortlist(head,num);
 printf("after sorting");
 disp(head); }
 else{ printf("Finally");
  disp(head); }
 getch();
}
void disp(struct node *head)
{  struct node *p;
   p=head;
   printf(" entire linked list is
");
   while(p->next!=NULL)
   { printf("%d->",p->n);
     p=p->next;
     if (p->next==NULL)
      printf("%d
",p->n);
   }
  return;
}
void sortlist(struct node *head,int num)
{  struct node *temp,*q;
   int i,j;
   q=head;
   temp=(struct node *)malloc(sizeof(struct node));
   for(i=0;i<num;i++)
    for(j=0;j<num-1;j++)
    { while(q->next!=NULL)
      { if((q->n)>(q->next->n))
   { temp->n=q->n;
     q->n=q->next->n;
     q->next->n=temp->n;
   }
   q=q->next;
      }
  if(q->next==NULL && i<num)
    q=head;
    }
  q=head;
  return;
}
struct node *addbeg(struct node *head,int addn)
{ struct node *p;
  p=(struct node *)malloc(sizeof(struct node));
  p->n=addn;
  p->next=head;
  head=p;
  return head;
}
void addend(struct node *head,int addn)
{ struct node *p,*q;
  p=(struct node *)malloc(sizeof(struct node));
  q=head;
  while(q->next!=NULL)
   q=q->next;
  q->next=p;
  p->n=addn;
  p->next=NULL;
  return;
}
struct node *addbef(struct node *head,int befadd)
{ struct node *p,*q,*r;
  int addp;
  printf("new node
");
  scanf("%d",&addp);
  p=(struct node *)malloc(sizeof(struct node));
  p->n=addp;
  q=r=head;
  while(q->n!=befadd)
  { r=q;
    q=q->next;
    if(q==NULL) break;
  }
  if(q==NULL) {  printf("anode %d not found
",befadd);
       delay(1000);
       return head;
         }
  if(q==head) {  p->next=q;
       head=p;
       return head;
         }
  r->next=p;
  p->next=q;
  return head;
}
void addaft(struct node *head,int aftadd)
{  struct node *p,*q;
   int addp;
   printf("new node
");
   scanf("%d",&addp);
   p=(struct node *)malloc(sizeof(struct node));
   p->n=addp;
   q=head;
   while(q->n!=aftadd)
   { q=q->next;
     if(q==NULL)  break;
   }
  if(q==NULL) {  printf("anode %d not found
",aftadd);
       delay(1000);
       return;
         }
   p->next=q->next;
   q->next=p;
   return;
}
void addbet(struct node *head,int no1,int no2)
{  struct node *p,*q,*r,*s;
   int addp;
  // printf("%d %d
",*no1,*no2);
   printf("new node
");
   scanf("%d",&addp);
   p=(struct node *)malloc(sizeof(struct node));
   p->n=addp;
   r=head;
   q=r;
   if(q->n!=no1)
     { r=q;
  q=q->next;
     }
   else
  { if (q->next->n!=no2)
  {      s=q->next;
         while(s!=NULL) { s=s->next;
           if(s->n==no2)
              { printf("anodes are not successive
");
           delay(1000);
           return;
              }
              }
         printf("aillegal node selection
");
         delay(1000);
         return;
  }
    else { q=q->next;
      r->next=p;
      p->next=q;
      return;
    }
  }
  while(r->n!=no1 || q->n!=no2)
   {  r=q;
      q=q->next;
      if(q==NULL)
      { printf("aillegal node selection
");
   delay(1000);
   return;
      }
   }
    r->next=p;
    p->next=q;
    return;
}
struct node *del(struct node *head,int cut)
{ struct node *p,*q;
  p=head;
  q=p;
  while(p->n!=cut)
  { q=p;
    p=p->next;
    if(p==NULL)  { printf("anode %d not found
",cut);
         delay(1000);
         return head;
       }
  }
  if(p==head) {  head=q->next;
       q=head;
       free(p);
       return head;
         }
  q->next=p->next;
  free(p);
  return head;
}
struct node *befdel(struct node *head,int befcut)
{ struct node *p,*q;
  p=head;
  q=p;
  while(p->next->n!=befcut)
  { q=p;
    p=p->next;
    if(p==NULL) {  printf("anode %d not found
",befcut);
         delay(1000);
         return head;
      }
  }
  if(p==head) { head=q->next;
      q=head;
      free(p);
      return head;
         }
  q->next=p->next;
  free(p);
  return head;
}
void aftdel(struct node *head,int aftcut)
{ struct node *p,*q;
 p=head;
 q=p;
 while(q->n!=aftcut)
 { q=p;
   p=p->next;
   if(p==NULL) { if(q->next==NULL)  printf("ano node after node
%d
",aftcut);
       else      printf("anode %d not found
",aftcut);
       delay(1000);
       return;
     }
 }
 if(q==head) p=q->next;
 q->next=p->next;
 free(p);
 return;
}
void betdel(struct node *head,int prnode,int succnode)
{ struct node *p,*q,*r,*s;
 p=head;
 q=p;
 if(p->n!=prnode)
 { q=p;
   p=p->next;
 }
 else { r=p->next;
   if(r->next->n!=succnode)
   { s=r->next;
     while(s!=NULL) { s=s->next;
            if(s->n==succnode)
            { printf("amore nodes between those nodes
");
              delay(1000);
              return;
            }
          }
     printf("aillegal node selection
");
     delay(1000);
     return;
   }
   else   { q->next=r->next;
       free(r);
       return;
     }
      }
 while(q->n!=prnode || p->next->n!=succnode)
 { q=p;
   p=p->next;
   if(p->next==NULL)  { printf("aillegal node selection
");
         delay(1000);
         return;
            }
  }
   q->next=p->next;
   free(p);
   return;
}
void update(struct node *head,int change)
{ struct node *p;
  int upd;
  p=head;
  printf("updated node
");
  scanf("%d",&upd);
  while(p->n!=change)
  { p=p->next;
    if(p==NULL) { printf("anode %d not found
",change);
      delay(1000);
      return;
         }
  }
  p->n=upd;
  return;
 }
void search(struct node *head,int find)
{ struct node *p;
  int j=1;
  p=head;
  while(p->n!=find)
  { p=p->next;
    j++;
    if(p==NULL) { printf("
SORRY. node %d is not present
",find);
        delay(1000);
        return;
      }
  }
  printf("aYES! the node %d is present in the %dth
position
",find,j);
  delay(1000);
  return;
}
struct node *reverse(struct node *head)
{ struct node *t1,*t2;
 t1=head->next;
 t2=t1->next;
 head->next=NULL;
 while(t1!=NULL)
 { t1->next=head;
   head=t1;
   t1=t2;
   t2=t2->next;
 }
  return head;
}


Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.