| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
//写出将带头结点的线性单链表L就地逆置的算法 
#include<stdio.h> 
#include<stdlib.h> 
#include<malloc.h> 
#define LEN sizeof(struct student)// student结构的大小  
  struct student *L();//创建链表  
  void nizhi(struct student *L);//逆置链表  
  void print(struct student *L);//打印链表  
  struct student{ 
        int num; 
        struct student *next; 
}; 
int n;//全局变量  
 
struct student *L() 
{ 
        struct student *head,*p1,*p2; 
p1=p2 = (struct student*)malloc(LEN); 
        if (NULL == p1) 
                return NULL; 
        else { 
                p1->next = NULL; 
                p1->num = 0; 
        } 
        printf("请输入数字:\n"); 
        scanf("%d",&p1->num ); 
        int n=0; 
        head=NULL; 
 
        while(p1->num )//只要输入的数字不是0,链表会一直创建下去  
        { 
                n++; 
                if(n==1) 
                { 
                        head->next =p1; 
                } 
                else 
                { 
                        p2->next =p1; 
                         
                } 
                p2=p1; 
                p1=(struct student *)malloc(LEN); 
        printf("请输入数字:\n"); 
        scanf("%d",&p1->num ); 
        } 
        p2->next =NULL;}  
         
 void nizhi(struct student *L)//构造逆置算法  
 { 
         struct student *p,*p1,*p2; 
         p1=head->next; 
         p2=p1->next ; 
         p1->next=p ; 
 while(p2){ 
         p=p1; 
         p1=p2; 
         p2=p2->next ; 
         p1->next =p; 
          } 
          p2->next =p1; 
          head->next =p2; 
return(L);  
} 
void print(struct student *L) //构造打印链表算法  
{ 
        while(head) 
        { 
                printf("%d\t",head->num ); 
                head=head->next ; 
                 
        } 
        return (L); 
         
} 
void main()//主函数  
{ 
        struct student *L(); 
        void nizhi(struct student *L); 
         void print(struct student *L); 
         return 0;  
} 
 
- #include <stdio.h>
 
 - #include <stdlib.h>
 
  
- struct student{
 
 -         int num                                                                          ;
 
 -         struct student * next                                                            ;
 
 - }                                                                                        ;
 
  
- int n                                                                                    ;
 
  
- void print(struct student * head)
 
 - {
 
 -         printf("\n")                                                                     ;
 
 -         if(head) {
 
 -                 printf("%d" , head -> num )                                              ;
 
 -                 head = head -> next                                                      ;
 
 -                 for(; head ; head = head -> next) printf("\t%d" , head -> num )          ;
 
 -                 printf("\n")                                                             ; 
 
 -         }
 
 - }
 
  
- struct student * create()
 
 - {
 
 -         struct student * head , * p1 , * p2                                               ;
 
 -         int num                                                                           ;
 
 -         printf("\n")                                                                      ;
 
 -         for(n = 0 , p1 = p2 = head = NULL ; ; p2 = p1) {
 
 -                 printf("请输入数字 : ") ;
 
 -                 scanf("%d",& num )      ;
 
 -                 if(num > 0) {
 
 -                         if((p1 = (struct student *) malloc(sizeof(struct student)))) {
 
 -                                 p1 -> num = num                                           ;
 
 -                                 p1 -> next = NULL                                         ;
 
 -                                 if(head) p2 -> next = p1                                  ;
 
 -                                 else head = p1                                            ;
 
 -                                 n ++                                                      ;
 
 -                         } else {
 
 -                                 for(p1 = head ; p1 ; p1 = p2) {
 
 -                                         p2 = p1 -> next                                   ;
 
 -                                         free(p1)                                          ; 
 
 -                                 }
 
 -                                 fprintf(stderr , "\n")                                    ;
 
 -                                 fprintf(stderr , "Error : malloc()\n")                    ;
 
 -                                 fprintf(stderr , "\n")                                    ;
 
 -                                 break                                                     ;
 
 -                         }
 
 -                 } else {
 
 -                         break                                                             ;
 
 -                 }
 
 -         }
 
 -         return head                                                                       ;
 
 - }        
 
  
- struct student * nizhi(struct student * head)
 
 - {
 
 -         struct student * p1 , * p2 , * p3                                                 ;
 
 -         for(p1 = head , p2 = NULL ; p1 ; p1 = p3) {
 
 -                 p3 = p1 -> next                                                           ;
 
 -                 if(p1 == head) p1 -> next = NULL                                          ;
 
 -                 else p1 -> next = p2                                                      ;
 
 -                 if(! p3) head = p1                                                        ;
 
 -                 p2 = p1                                                                   ;
 
 -         }
 
 -         return head                                                                       ;
 
 - }
 
  
- int main(void)
 
 - {
 
 -         struct student * h = NULL , * p1 , * p2                                          ;
 
 -         h = create()                                                                     ;
 
 -         print(h)                                                                         ;
 
 -         h = nizhi(h)                                                                     ;
 
 -         print(h)                                                                         ;
 
 -         for(p1 = h ; p1 ; p1 = p2) {
 
 -                 p2 = p1 -> next                                                          ;
 
 -                 free(p1)                                                                 ;
 
 -         }                 
 
 - }
 
  复制代码
        编译、运行实况
 - D:\00.Excise\C>g++ -o x x.c
 
  
- D:\00.Excise\C>x
 
  
- 请输入数字 : 100
 
 - 请输入数字 : 101
 
 - 请输入数字 : 102
 
 - 请输入数字 : 103
 
 - 请输入数字 : 104
 
 - 请输入数字 : 105
 
 - 请输入数字 : 106
 
 - 请输入数字 : 107
 
 - 请输入数字 : 108
 
 - 请输入数字 : 109
 
 - 请输入数字 : 0
 
  
- 100     101     102     103     104     105     106     107     108     109
 
  
- 109     108     107     106     105     104     103     102     101     100
 
  
- D:\00.Excise\C>
 
  复制代码 
 
 
 |   
 
 
 
 |