链表

一道面试题,建立单向链表,放入a-z字母,单向链表反转,然后输出。

 

 

#include<stdio.h>
#include<stdlib.h>

struct note
{
 char data;
 struct note *next;

};

struct note* list_init(void);
void list_printf(struct note* prt);
void list_switch(struct note* prt);
struct note *heat;

void main(void)
{
 heat=list_init();
 list_switch(heat);
 list_printf(heat); 
}

struct note* list_init(void)
{
 char index;
 struct note *heat,*prt_new,*prt_current;
 heat=NULL;
 for(index=0; index<26; index++)
 {
  prt_new=(struct note*)malloc(sizeof(struct note));
  if(NULL==heat)
  {
   heat=prt_new;
  }
  else
  {
   prt_current->next=prt_new;
  }
  prt_new->data='a'+index;
  prt_current=prt_new;
 }
 prt_current->next=NULL;
 return heat;
}

void list_printf(struct note* prt)
{
 char index;
 for(index=0; index<26; index++)
 {
  printf("%c\n",prt->data);
     prt=prt->next;
 }
}


void list_switch(struct note* prt)
{
 struct note *pheat,*pcurr,*pnext;
 pheat=prt;
 pcurr=pheat->next;
 if((pheat==NULL)||(pcurr==NULL))
  return;
 while(pcurr!=NULL)
 {
  pnext=pcurr->next;
  pcurr->next=pheat;
  pheat=pcurr;
  pcurr=pnext;
 }
 heat->next=NULL;
 heat=pheat;
}

你可能感兴趣的:(链表)