title: Permuting bottom element with top element in a stack
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;conio.h&gt;
  
  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", &amp;ch);
  switch (ch)
          {
          case 1:
              printf("Enter data : ");
              scanf("%d", &amp;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-&gt;data=no;
      temp-&gt;next=top;
      top=temp;
      count++;
  }
   void Print()
  {
      struct node *temp=top;
      printf("List is:");
      while(temp!=NULL)
      {
          printf("%d  ",temp-&gt;data);
          temp=temp-&gt;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&lt;count-1;i++)
      {
          temp1=temp1-&gt;next;
      }
      temp1-&gt;next=temp2;
      temp2-&gt;data=temp1-&gt;next-&gt;data;
      temp1-&gt;next=temp;
      temp-&gt;data=top-&gt;data;
      temp=NULL;
      temp2-&gt;next=top;
      top=temp2;
  }
  </code></pre>
  
  <p>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.</p>