链表

  

#include  < stdio.h >
#include 
< malloc.h >
#define  NULL 0
#define  LEN sizeof(struct student)

struct  student
{
    
int data;
    
struct student *next;
}
;

struct  student  * creat( )
{
    
struct student *head, *p, *s;
    p 
= (struct student *)malloc( LEN );
    scanf(
"%d"&p->data);
    head 
= NULL;
    
while0 != p->data)
    
{
        
if(head == NULL)
        head 
= p;
        
else
        
{
            s 
= (struct student *)malloc( LEN );
            scanf(
"%d",&s->data);
            p
->next = s;
            p 
= s;
        }

    }

    p
->next = NULL;
    
return(head);
}


void  print( struct  student  * head)
{
    
struct student *p;
    p 
= head;
    
while(p != NULL)
    
{
        printf(
"%d ",p->data);
        p 
= p->next;
    }

}


int  main()
{
    
struct student *head;
    head 
= creat();
    print(head);
    getch();
    
return 0;
}

 插入和删除新的节点

 

#include  < stdio.h >
#include 
< malloc.h >
#define  NULL 0
#define  LEN sizeof(struct student)

struct  student
{
    
int data;
    
struct student *next;
}
;

struct  student  * creat( )         /* 创建无头节点的链表 */
{
    
struct student *head, *p, *s;    /* s为新开辟的接点 */
    p 
= (struct student *)malloc( LEN );
    scanf(
"%d"&p->data);
    head 
= NULL;
    
while0 != p->data)
    
{
        
if(head == NULL)
        head 
= p;
        
else
        
{
            s 
= (struct student *)malloc( LEN );
            scanf(
"%d",&s->data);
            p
->next = s;
            p 
= s;
        }

    }

    p
->next = NULL;
    
return(head);
}


struct  student  * insert( struct  student  * head, struct  student  * stu)
/* 插入新的接点 */
{
    
struct student *p, *q, *s; /* p为游标结点,q为p前结点,s为插入结点 */
    p 
= head;
    s 
= stu;
    
if(head == NULL)
    
{
        head 
= s;
        s 
->next  = NULL;
    }

    
else
    
{
        
while((s->data > p->data)&&(p->next != NULL))
        
{
            q 
= p;
            p 
= p->next;
        }

        
if(s->data <= p->data)
        
{
            
if(head == p)
            head 
= s;
            
else
            q
->next = s;
            s
->next = p;
        }

        
else
        
{
            p
->next = s;
            s
->next =NULL;
        }

    }

    
return(head);
}



struct  student  * del( struct  student  *  head,  int  num)
/* 删除节点函数 */
{
    
struct student *p, *q, *f;    /* 释放删除节点的内存 */
    
if(head == NULL)
    
{
        printf(
"linklist null! ");
        
goto end;
    }

    
else
    
{
         p 
= head;
        
while((num != p->data)&&(p->next != NULL))
        
{
            q 
= p;
            p 
= p->next;
        }

        
if(num == p->data)
        
{
            
if( p == head )
            
{
                f 
= head;
                head 
= p->next;
                free(f);
            }

            
else
            
{
                f 
= p;
                q
->next = p->next;
                free(f);
            }

        }

    }

end:
    
return head;
}


void  print( struct  student  * head)    // 输出链表
{
    
struct student *p;
    p 
= head;
    printf(
"head=%d ",head->data);
    
while(p != NULL)
    
{
        printf(
"%d ",p->data);
        p 
= p->next;
    }

}


int  main()
{
    
struct student *head, *s;
    
int data;
    head 
= creat();
    print(head);

    printf(
"insert new node: ");
    s 
= (struct student *)malloc(LEN);
    scanf(
"%d",&s->data);
    head 
= insert(head,s);

    printf(
"now the linklist is: ");
    print(head);

    printf(
"input the delete num: ");
    scanf(
"%d",&data);
    head 
= del(head, data);
    print(head);

    getch();
    
return 0;
}

链表的创建,插入,删除,打印输出(c)

 

struct  student  * reverse( struct  student  * head)
{
    
struct student *p, *q, *r;
    p 
= head ;
    q 
= p->next;
    
while(q != NULL)
    
{
        r 
= q->next;
        q
->next = p;
        p 
= q;
        q 
= r;
    }

    head
->next = NULL;
    head 
= p;
    
return(head);
}
倒置函数如下,插入即可使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值