码迷,mamicode.com
首页 > 其他好文 > 详细

序列翻转 指针

时间:2014-10-16 23:25:03      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   for   sp   数据   2014   log   amp   

#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#define LIST_INIT_SIZE  100
#define add  10
typedef struct Lnode{
   int date;
   struct Lnode *next;
}Lnode ,*LinkList;
LinkList CreatList_L()
{
       LinkList phead, p,L;
    L=(LinkList)malloc(sizeof(Lnode));
    int val, i, n;
     phead=(LinkList)malloc(sizeof(Lnode));
    phead=NULL;
    printf("请输入您要建立的链表长度:\n");
    scanf("%d", &n);
    printf("请输入您要输入的数据:\n");
    for(i=0; i<n; ++i)
    {
        scanf("%d", &val);
        p = (LinkList)malloc(sizeof(Lnode));
        p->date = val;
        if(phead==NULL)//寻找出头结点
        L=phead=p;
        else
        L->next=p;
        L=p;
    }
    L->next=NULL;
    return phead;
}  //CreatList_L
LinkList  Resver_L(LinkList head){
<span style="white-space:pre">	</span>LinkList  p, q, r;
    p = head;
    q=r=NULL;
    while(p)
    {
        q = p->next;
        p->next = r;
        r = p;
        p = q;
    }
    return r;
}//Resver_L
void output_L(LinkList head){
    LinkList p;
<span style="white-space:pre">	</span>p=(LinkList)malloc(sizeof(Lnode));
<span style="white-space:pre">	</span>p=head;
<span style="white-space:pre">	</span>while(p!=NULL){
<span style="white-space:pre">	</span>printf("%d ",p->date);
<span style="white-space:pre">	</span>p=p->next;
}
   puts("");
}//output_L
int main(){
<span style="white-space:pre">	</span>int n;
<span style="white-space:pre">	</span>LinkList L,head;
<span style="white-space:pre">	</span>head=(LinkList)malloc(sizeof(Lnode));
<span style="white-space:pre">	</span>head=CreatList_L();
<span style="white-space:pre">	</span>printf("翻转之前的序列:\n");
<span style="white-space:pre">	</span>output_L(head);
<span style="white-space:pre">	</span>printf("翻转之后的序列:\n");
<span style="white-space:pre">	</span>head=Resver_L(head);
<span style="white-space:pre">	</span>output_L(head);
}

序列翻转 指针

标签:style   blog   io   for   sp   数据   2014   log   amp   

原文地址:http://blog.csdn.net/u013076044/article/details/40158279

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!