以ä¸ä»£ç æ¯ä¸ä¸ªé¾è¡¨çéç½®æä½ä»£ç ï¼ä¾åè
#include<stdio.h>
#include <stdlib.h>
struct node {
int n; //ç»ç¹ä¸çæ°æ®
struct node *next ; //æåå继ç»ç¹çæé
} *list=NULL; //表头ï¼åå¼NULLï¼è¡¨ç¤ºç©ºè¡¨
void createList() //建ç«é¾è¡¨
{
struct node *p; //临æ¶ç»ç¹æé
int i;
for( i=5;i>0;i-- )
{
p=(struct node *) malloc( sizeof(struct node) ); //ç³è¯·ä¸ä¸ªç»ç¹
p->n=i; //æ°æ®èµå¼
//以ä¸ä¸¤å¥ï¼å®ç°å¤´ææ³å»ºç«é¾è¡¨
p->next=list; //ææ°ç»ç¹pè£
å¨è¡¨å¤´å
list=p ; //pæ为æ°ç表头
}
}
void showList()
{
struct node *p=list;
while( p)
{
printf("%d ", p->n );
p=p->next;
}
printf("\n");
}
void reverseList()
{
struct node *p=NULL,*h=list,*t;
while(h)
{
t=h; //å离åºä¸ä¸ªç»ç¹
h=h->next; //hæååç»çé¨åï¼ä¿æå表ä¸ä¸¢å¤±
t->next=p; //头ææ³
p=t;
}
list=p;//表头æéèµå¼
}
int main()
{
printf("link:\n");
createList(); //建表
showList(); //æ¾ç¤ºè¡¨
reverseList(); //éç½®
showList(); //åæ¾ç¤º
return 0;
}
追é®ç»æä½éçé£ä¸ªæåä¸ä¸ªèç¹çæéæ¯æä¹æåç
追çé¦å
ï¼æéï¼é常æ¯ææéåéï¼è¿æ¯ä¸ç§ç¨æ¥åå¨å
åå°åçåéï¼èå
åå°åå¼æ¯ä¸ªæ´æ°ï¼æ以ï¼æéåéä¸åå¨çå°±æ¯ä¸ªæ´æ°ï¼é常æ们ä¼ç¨èµå¼æä½ç¬¦ï¼çå·ï¼æ¥å¯¹å
¶èµå¼ã
p->next=list; //ææ°ç»ç¹pè£
å¨è¡¨å¤´å pç»ç¹çå继æéåéä¸åå¨listçå¼ï¼èlistä¸åçæ¯å½å表çé¦å°åï¼ è¿æ ·ä¸å¥ï¼å°±æpålist表串å¨äºä¸èµ·ã
å设listæ¯ä¸ä¸²æ°çï¼ pæ¯ä¸ä¸ªåç¬çæ°çï¼ ä¸é¢çæä½ï¼å°±æ¯æpè¿ä¸ªæ°çç»å¨äºlistè¿ä¸ªæ°ç串ç第ä¸ä¸ªççåé¢ï¼å½¢æäºä¸ä¸ªæ°çæ°ç串ã