#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node
{
int data;
struct node *next;
};
struct node *top;
int count=0;
void push(int n);
void Print();
void permute();
int main()
{
int no, ch, e;
printf("\n1 - Push");
printf("\n4 - Print");
printf("\n7 - Permute first and last element");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 4:
Print();
break;
case 7:
permute();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
void push(int no)
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=no;
temp->next=top;
top=temp;
count++;
}
void Print()
{
struct node *temp=top;
printf("List is:");
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
void permute()
{
int i;
struct node *temp=(struct node*)malloc(sizeof(struct node));
struct node *temp1=(struct node*)malloc(sizeof(struct node));
struct node *temp2=(struct node*)malloc(sizeof(struct node));
temp=top;
temp1=NULL;
for(i=0;i<count-1;i++)
{
temp1=temp1->next;
}
temp1->next=temp2;
temp2->data=temp1->next->data;
temp1->next=temp;
temp->data=top->data;
temp=NULL;
temp2->next=top;
top=temp2;
}
So my implementation of a stack works fine as for pushing and printing the elements in the stack, but when I want to permute the bottom element with the top element the program crashes. I think I am messing something up in my permute function. Thank you for any help before hand.