SachinGuptaMNNIT
BAN USER#include<stdio.h>
#define max 20
int n;
void perm(int *p,int index,int i)
{int j;
if(i==n)
{ p[index]=i;
for(j=1;j<=index;j++)
printf("%d ",p[j]);
printf("\n");
}
else
{ p[index]=i;
if(i<n-1)
perm(p,index+1,i+2);
if(i<n)
perm(p,index+1,i+1);
}
}
main()
{
int p[max];
printf("\n Enter no of Rocks :");
scanf("%d",&n);
printf("\n For %d to %d rocks possible paths:\n",1,n);
perm(p,1,1);
}
int height(struct node* node)
{
if (node==NULL)
return 0;
else
{
/* compute the height of each subtree */
int lheight = height(node->left);
int rheight = height(node->right);
if (lheight > rheight)
return(lheight+1);
else return(rheight+1);
}
}
struct node *reverse (struct node *head, int k)
{
struct node* current = head;
struct node* next;
struct node* prev = NULL;
int count = 0;
/*reverse first k nodes of the linked list */
while (current != NULL && count < k)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
count++;
}
/* next is now a pointer to (k+1)th node
Recursively call for the list starting from current.
And make rest of the list as next of first node */
if(next != NULL)
{ head->next = reverse(next, k); }
/* prev is new head of the input list */
return prev;
}
//in gcc it is working
# include <stdio.h>
# include <malloc.h>
struct node
{
int info;
struct node *link;
}*start;
struct node * rev( struct node * head)
{
struct node * temp = NULL;
if ( head == NULL)
return NULL;
if ( head->link == NULL )
return head;
temp = rev ( head->link );
head->link -> link = head;
head->link = NULL;
return temp;
}
create_list(int num)
{
struct node *q,*tmp;
tmp= malloc(sizeof(struct node));
tmp->info=num;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}/*End of create_list() */
display()
{
struct node *q;
if(start == NULL)
{
printf("List is empty\n");
return;
}
q=start;
while(q!=NULL)
{
printf("%d ", q->info);
q=q->link;
}
printf("\n");
}/*End of display()*/
struct node *swaptwo(struct node *head)
{
if((head!=NULL)&&(head->link !=NULL))
{
struct node *tmp1=head->link->link;
struct node *tmp2=head->link;
head->link->link=head;
head->link=swaptwo(tmp1);
return tmp2;
}
else return head;
}
main()
{
int i,n,item;
start=NULL;
printf("How many nodes you want : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the item %d : ",i+1);
scanf("%d",&item);
create_list(item);
}
printf("Initially the linked list is :\n");
display();
start=swaptwo(start);
printf("Linked list after reversing is :\n");
display();
}/*End of main()*/
/*Reverse a single linked list using single pointer iteratively and recursively
(Recursive)for iterative code visit sachingupta.tk */
struct list * reverse_ll( struct list * head)
{
struct list * temp = NULL;
if ( head == NULL)
return NULL;
if ( head->next == NULL )
return head;
temp = reverse_ll ( head->next );
head->next -> next = head;
head->next = NULL;
return temp;
}
/*Swap every consecutive two elements without using value stored in the node of linkedlist
use*/
struct node *swaptwo(struct node *head)
{
if((head!=NULL)&&(head->link !=NULL))
{
struct node *tmp1=head->link->link;
struct node *tmp2=head->link;
head->link->link=head;
head->link=swaptwo(tmp1);
return tmp2;
}
else return head;
}
/*Sachin Gupta MNNIT_ALD
above code is for
1>reverse a linklist
2.revese a linklist using one pointer
3.Swap every consecutive two elements without using value stored in the node of linkedlist
/* Program of reverse linked list*/
# include <stdio.h>
# include <malloc.h>
struct node
{
int info;
struct node *link;
}*start;
struct node * rev( struct node * head)
{
struct node * temp = NULL;
if ( head == NULL)
return NULL;
if ( head->link == NULL )
return head;
temp = rev ( head->link );
head->link -> link = head;
head->link = NULL;
return temp;
}
create_list(int num)
{
struct node *q,*tmp;
tmp= malloc(sizeof(struct node));
tmp->info=num;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}/*End of create_list() */
display()
{
struct node *q;
if(start == NULL)
{
printf("List is empty\n");
return;
}
q=start;
while(q!=NULL)
{
printf("%d ", q->info);
q=q->link;
}
printf("\n");
}/*End of display()*/
reverse()
{
struct node *p1,*p2,*p3;
if(start->link==NULL) /*only one element*/
return;
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
}/*End of reverse() */
struct node *swaptwo(struct node *head)
{
if((head!=NULL)&&(head->link !=NULL))
{
struct node *tmp1=head->link->link;
struct node *tmp2=head->link;
head->link->link=head;
head->link=swaptwo(tmp1);
return tmp2;
}
else return head;
}
main()
{
int i,n,item;
start=NULL;
printf("How many nodes you want : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the item %d : ",i+1);
scanf("%d",&item);
create_list(item);
}
printf("Initially the linked list is :\n");
display();
start=swaptwo(start);
printf("Linked list after reversing is :\n");
display();
}/*End of main()*/
yes in the original question g was declared inside the loop .
- SachinGuptaMNNIT March 15, 2012