数字,字符串的从尾到头的输出

来源:互联网 发布:ubuntu切换中文输入法 编辑:程序博客网 时间:2024/06/05 14:22

一 链表的从尾到头输出

#include "stdio.h"#include "malloc.h"struct ListNode  {        int       m_nKey;        ListNode* m_pNext;  }; ///////////////////////////////////////////////////////////////////////   // Print a list from end to beginning   // Input: pListHead - the head of list   ///////////////////////////////////////////////////////////////////////   void PrintListReversely(ListNode* pListHead)  {        if(pListHead != NULL)        {              // Print the next node first              // if (pListHead->m_pNext != NULL)             // {                    PrintListReversely(pListHead->m_pNext);             // }                 // Print this node               printf("%3d", pListHead->m_nKey);        }  }  int main(){struct ListNode * head,*p1,*p2,*p3;head = NULL;p1 = (struct ListNode *)malloc(sizeof(struct ListNode ));p2 = (struct ListNode *)malloc(sizeof(struct ListNode ));p3 = (struct ListNode *)malloc(sizeof(struct ListNode ));p1->m_nKey = 1;p2->m_nKey = 2;p3->m_nKey = 3;p1->m_pNext = p2;p2->m_pNext = p3;p3->m_pNext = NULL;head = p1;PrintListReversely(head);printf("\n");return 0;}

二 字符串的从尾到头输出

#include "stdio.h"void printListReverse(char *p){if(*p != '\0'){printListReverse(p+1);printf("%2c",*p);}}void main(){char *string = "1234567";printListReverse(string);printf("\n");}

 

原创粉丝点击