zoukankan      html  css  js  c++  java
  • 伤不起的指针

    虽然知道怎么做,但是还是做一遍啦。结果调试了两个多小时,真崩溃。

    /*
    * =====================================================================================
    *
    * Filename: intlist.h
    *
    * Description:
    *
    * Version: 1.0
    * Created: 09/16/2011 02:56:13 AM
    * Revision: none
    * Compiler: gcc
    *
    * Author: YOUR NAME (),
    * Company:
    *
    * =====================================================================================
    */

    #include
    <stdio.h>
    #include
    <stdlib.h>
    struct list_node
    {
    int data;
    struct list_node *next;
    };
    struct list_node * make_node(int n)
    {
    struct list_node *pNode = (struct list_node *)malloc(sizeof(struct list_node ));
    pNode
    ->next = 0;
    pNode
    ->data = n;
    return pNode ;
    }
    void put_node(struct list_node *pNode)
    {
    free(pNode);
    }
    struct list
    {
    struct list_node *head;
    };

    void CreateNoHead(struct list *root,int arrary[],int n)
    {
    struct list_node ** ppHead = &(root->head);
    struct list_node * pNode ;
    for(int i = 0; i< n ;++i)
    {
    pNode
    = make_node(arrary[i]);
    /*if(!*ppHead)
    {
    *ppHead = pNode;
    }else{
    pNode->next = (*ppHead);
    *ppHead = pNode;
    }
    */
    if(*ppHead)
    pNode
    ->next = (*ppHead);
    *ppHead = pNode;
    printf(
    "%d\t",pNode->data);
    }
    printf(
    "\n");
    fflush(stdin);
    }
    void CreateHead(struct list *root,int arrary[],int n)
    {
    root
    ->head = make_node(0);
    struct list_node * pHead = root->head;
    struct list_node * pNode ;
    for(int i = 0; i< n ;++i)
    {
    pNode
    = make_node(arrary[i]);
    /*
    if(*head)
    {
    pNode->next = (*head)->next;
    (*head)->next = pNode;
    }
    else//root->head = null
    pNode->next = *head;
    *head = pNode;
    */
    //先进后出链表
    pNode->next = pHead->next;
    pHead
    ->next = pNode;
    printf(
    "%d\t",pNode->data);
    }
    printf(
    "\n");
    fflush(stdin);
    }
    void Print(const struct list *root)
    {

    struct list_node * pNode = root->head;
    while(pNode)
    {
    printf(
    "%d\t",pNode->data);
    pNode
    = pNode->next;
    }
    printf(
    "\n");
    fflush(stdin);
    }
    void Destroy(struct list *root)
    {

    struct list_node * pNode = root->head;
    struct list_node * pNext;

    while(pNode)
    {
    pNext
    = pNode->next;
    put_node(pNode);
    pNode
    = pNext;
    }
    }
    void ReverseHead(struct list *root)
    {
    struct list_node * pHead = root->head->next;
    struct list_node * pNode ;
    struct list_node * pNext ;
    if(!pHead ||!(pNode = pHead->next))
    return ;
    //伤不起啊,该处要赋值为空
    pHead->next = 0;
    while(pNode)
    {
    pNext
    = pNode->next;
    pNode
    ->next = pHead;
    pHead
    = pNode;
    pNode
    = pNext;
    }
    //重新赋值
    root->head->next = pHead;
    }
    void ReverseNoHead(struct list *root)
    {
    struct list_node * pHead = root->head;
    struct list_node * pNode ;
    struct list_node * pNext ;
    //链表只有一个或零个节点
    if(!pHead ||!(pNode = pHead->next))
    return ;
    pHead
    ->next = 0;
    while(pNode)
    {
    pNext
    = pNode->next;
    //插入
    pNode->next = pHead;
    pHead
    = pNode;

    pNode
    = pNext;
    }
    //head重新赋值,新的表头
    root->head = pHead;
    }
    void TestNoHead(struct list *root)
    {

    }
    void TestHead(struct list *root)
    {

    }
    int main(int argc,char *argv[])
    {
    int arrary [] = {1,2,3,4,5,6,7,8,9,10};
    struct list task;
    task.head
    = 0;
    CreateHead(
    &task,arrary,sizeof(arrary)/sizeof(int));
    //CreateNoHead(&task,arrary,sizeof(arrary)/sizeof(int));
    Print(&task);
    printf(
    "\n");
    //Reverse(&task);
    //ReverseNoHead(&task);
    ReverseHead(&task);
    Print(
    &task);
    Destroy(
    &task);
    return 0;
    }

      

  • 相关阅读:
    IOS 推送消息 php做推送服务端
    判断是否是iPhone5
    BeeFramework
    如何在类中获取request,和网站路径
    maven build 报release 400错误
    mysql启动问题access denied for user 'root'@'localhost'(using password:YES)
    adapter结构异常记录
    eclipse项目报红解决
    Location Type Project 'testma' is missing required source folder: 'src/test/resources' testma Build
    当遇到eclipse调试断点乱走数据不准确的时候,请maven clean,maven install
  • 原文地址:https://www.cnblogs.com/westfly/p/2179826.html
Copyright © 2011-2022 走看看