LINKED LIST PROGRAMS...

4 views
Skip to first unread message

swadhin jaiswal

unread,
Nov 7, 2011, 10:57:34 AM11/7/11
to bbswork...@googlegroups.com
REVERSAL OF LINKED LIST..
1:RECURSION USING SINGLE POINTER..

struct node * reverse( struct node * p)
{
struct node * temp = NULL;

if ( p == NULL)
return NULL;
if ( p->next == NULL )
return p;

temp = reverse ( p->next );
p->next -> next = p;
p->next = NULL;
return temp;
}



2: middle of a linked list.

Node *FindMiddle( Node *head )
{
Node *p1, *p2;
p1 = p2 = head;
while( p2 && p2->next )
{
p2 = p2->next->next;
p1 = p1->next;
}
return( p1 );
}

3: Nth element from end of linked list..

node * nthNodeFromLast(node *p,int n)
{
int count=0;
node *p1,*p2;
p1=p2=p;
if(head==NULL) return NULL;
while(++count<=n)
p2=p2->next;
while(p2)
{
p2=p2->next;
p1=p1->next;
}
return p1;
}




THERE ARE MANY MORE ON LINKED LIST..BUT LOGIC IS IMP..
FOR THOSE OF U ARE PREAPARING FOR GATE..LINKED LIST IS REALY SCORING PORTION..
JUST WORK HARD FROM NOW...
ALL D BEST..

Piyush Sahu

unread,
Nov 7, 2011, 11:34:01 AM11/7/11
to bbswork...@googlegroups.com
sir 3rd one (nth element from last) iska code clear hi nahi ho raha hai plz ise kal discuss it tommorow.......n also discuss its implementation in real world problem
Reply all
Reply to author
Forward
0 new messages