一个链表创建、反转、打印的C语言代码

代码在Turboc++3.0环境下运行正常,但是在vs环境下能编译成功,但输入数据有点问题。

红色部分修改后,代码在vs下也ok了。但是为什么修改前在Turboc++3.0环境下运行正常呢?

 

#include <stdio.h>
#include <malloc.h>
#include <ctype.h>

typedef struct node LNKLIST;

struct node
{
 int data;
 LNKLIST *next;
};

int main(void)
{
 LNKLIST *start = NULL,*p,*q,*temp;
 char opt;
 do
 {
  printf("/n/t/t Menu"
   "/n/t 1. Create/Append Linked List"
   "/n/t 2. Reverse Linked List"
   "/n/t 3. Display Linked List"
   "/n/t 4. Exit"
   "/n Enter your choice:"
   );
  opt = getchar();
  flushall();
 
  switch(opt)
  {
  case '1':
   do
   {
    p = start;
   while (p != NULL && p->next != NULL)  /*修改前while(p->next != NULL)*/

    {
     p = p->next;
    }
    q = (LNKLIST*)malloc(sizeof(LNKLIST));
    printf("/nEnter the data: ");
    scanf("%d",&q->data);
    q->next = NULL;
    if(start == NULL)
     start = q;
    else
     p->next = q;
    printf("Wanna continue? ");
   } while (tolower(getchar())== 'y');
   break;
  case '2':
   p = start;
   q = p->next;
   while(q!=NULL)
   {
    temp = q->next;
    q->next = p;
    p = q;
    q = temp;
   }
   start->next = NULL;
   start = p;
   break;
  case '3':
   p = start;
   printf("/nstart = %u ",start);
   while(p!=NULL)
   {
    printf("-> [%d | %u]",p->data,p->next);
    p = p->next;
   }
   getchar();
  }
 } while (opt != '4');

 printf("/nsuccess/n");
 return 0;
}

 

 

你可能感兴趣的:(一个链表创建、反转、打印的C语言代码)