| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 583 人关注过本帖, 1 人收藏
标题:单链表插入修改
取消只看楼主 加入收藏
aKARL
Rank: 1
等 级:新手上路
帖 子:36
专家分:6
注 册:2013-7-1
结帖率:20%
收藏(1)
已结贴  问题点数:10 回复次数:0 
单链表插入修改
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN 10
typedef struct node
{
    int date;
    struct node *next;
}*PNODE;

PNODE create_list(void);
bool charu_list(PNODE,int,int);
void transt_list(PNODE);

int main(void)
{
    PNODE p;
    p=create_list();
    printf("All is:\n");
    transt_list(p);
    printf("\nAll is:\n");
    charu_list(p,7,7);
    transt_list(p);   
    printf("\n");
    return 0;
}
PNODE create_list(void)
{
    int i;
    int a[]={
        1,2,3,4,5,6,8,9,10,11
    };
    PNODE head=(PNODE)malloc(sizeof(node));
    if(NULL==head)
    {
        printf("neicun feipei shibai!");
        exit(-1);
    }
    PNODE tail=head;
    tail->next=NULL;
    for(i=0;i<LEN;i++)
    {
        PNODE pnew=(PNODE)malloc(sizeof(node));
        if(NULL==pnew)
        {
            printf("neicun feipei shibai!");
            exit(1);
        }   
        pnew->date=a[i];   
        tail->next=pnew;   
        pnew->next=NULL;
        tail=pnew;
    }
    return head;
}
bool charu_list(PNODE p,int pos,int value)
{
     int i=0;
     PNODE v=p;
     while(NULL!=v && i<pos-1)
     {
         v=v->next;
         ++i;
     }
     if(i>pos-1 && NULL==v)
        return false;
     PNODE pnod=(PNODE)malloc(sizeof(node));
     if(NULL==pnod)
     {
         printf("feipei neicun shibai!");
         exit(-1);
     }
     pnod->date=value;
     PNODE r=v->next;
     v->next=pnod;
     pnod->next=r;
     return true;
}
void transt_list(PNODE head)
{
      PNODE p=head->next;
      while(NULL!=p)
      {
          printf("%d ",p->date);
          p=p->next;   
      }   
      p=head;
      while(NULL!=p)
      {
          free(p);
          p=p->next;   
      }   
}

}
搜索更多相关主题的帖子: include return 
2013-10-11 08:25
快速回复:单链表插入修改
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014667 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved