C language code for creating, reversing, and printing a linked list

Source: Internet
Author: User

The code runs normally in the turboc ++ 3.0 environment, but can be compiled successfully in the vs environment, but the input data is a bit faulty.

After the red part is modified, the code is OK under. But why did the change run normally in the turboc ++ 3.0 environment?

 

# 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 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)/* before modification, 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;
}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.