• 伤不起的指针


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

    /*
    * =====================================================================================
    *
    * 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;
    }

      

  • 相关阅读:
    Caliburn micro 学习笔记...
    First steps with Caliburn Micro in Windows Phone 8 系列文章
    WPF and Silverlight.ComboBox 如何通过 Binding IsDropDownOpen 实现下拉菜单展开
    http各个状态码的详解
    点阵字库产生的原理
    Windows 服务调试方法(基于.net framwork4.6)
    关于.net Core 笔记
    JS+ google.maps.api 实现基本的导航功能
    C# 遍历控件检查是否有被选中的项(通用)
    C#编程习惯
  • 原文地址:https://www.cnblogs.com/westfly/p/2179826.html
Copyright © 2020-2023  润新知