注册 登录
编程论坛 数据结构与算法

单链表插入修改

aKARL 发布于 2013-10-11 08:25, 583 次点击
#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;   
      }   
}

}
3 回复
#2
yuccn2013-10-11 12:08
。。
#3
木头心2013-10-11 13:28
程序代码:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN 10
typedef struct node
{
    int date;
    struct node *next;
}*PNODE;


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

int main(void)
{
    PNODE p;
    create_list(p);//
    printf("All is:\n");
    transt_list(p);
    printf("\nAll is:\n");
    charu_list(p,7,7);
    transt_list(p);   
    printf("\n");
    return 0;
}

 create_list(PNODE &head)//
{
    int i;
    int a[]={
        1,2,3,4,5,6,8,9,10,11
    };
     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;
    }
   
}
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;   
      }  
*/
      //这里你直接释放了链表头 p->next 不存在   p=p->next;这一句有问题
while(NULL!=p)
      {
          free(p);
        }//而这样可行
}

//}//这里多了一个}
//建议你规范一下格式
#4
hunner52013-10-20 19:41
ranh ne  shi shenme cuowu a
1