单链表实现(c版)

----写前:这是一个做为练习记录,别无它用。不足之处还请指正!

这次练习线性链表环境为ubuntu10.10+vim+gcc,分三个文件,一个是nodetype.h,node.h,listnode.c。

源码如下:

nodetype.h源码:

#ifndef _NODETYPE_H_
#define  _NODETYPE_H_

#define  OK 1;
#define  ERROR 0;

typedef 
int  ElemType;
typedef 
int  Status;
#endif

 

node.h源码:

#ifndef _NODE_H_
#define  _NODE_H_

#include 
" nodetype.h "

typedef 
struct  LNode
{
        ElemType data;
        
struct  LNode  * next;
}LNode;

Status DisplayList(LNode 
* head);
LNode 
*  CreateList(LNode  * head, int  n);
LNode
*  InsertElement(LNode  * head, int  position,ElemType data);
LNode
*  GetElement(LNode  * head, int  position);
#endif

 

listnode.h源码:

#include  < stdio.h >
#include 
< stdlib.h >
#include 
< string .h >
#include 
" node.h "

LNode 
*  CreateList(LNode  * head, int  n)
{
        LNode 
* p, * p1;
        p1 
=  head  =  ( LNode  *  )malloc(  sizeof (LNode) );
        
if (NULL  ==  p1) 
        {   
                printf(
" Apply memory failed \n " );
                
return  head;
        }   
        p1
-> next  =  NULL;
        printf(
" Please input the list root data: " );
        scanf(
" %d " , & p1 -> data);
        
return  head;
}

Status DisplayList(LNode 
* head)
{
        printf(
" -----------------------------\n " );
        LNode 
* temp;
        temp 
=  head;
        
while (temp  != NULL)
        {   
                printf(
" Address:0x%x\tData:%d\tNext:0x%d \n " ,(unsigned  int )temp,temp -> data,(unsigned  int )temp -> next);
                temp 
=  temp -> next;
        }   
        printf(
" -----------------------------\n " );
}

LNode
*  InsertElement(LNode  * head, int  position,ElemType data)
{
        
if (NULL  !=  head)
        {   
LNode 
* node;
                node
= (LNode  * )malloc( sizeof (LNode));
                LNode 
* p1;
                p1
= head;
                
if (position  <=   1 )
                {
                        node
-> data = data;
                        node
-> next = p1;
                        head
= node;
                }
                
else
                {
                        
int  i = 0 ;
                        
for (i = 1 ;i < position - 1 ;i ++ )
                        {
                                p1
= p1 -> next;
                        }
                        
if (NULL  !=  node)
                        {
                                node
-> data = data;
                                node
-> next = p1 -> next;
                                p1
-> next = node;
                        }
                }
        }
        
return  head;
}
LNode 
* GetElement(LNode  * head, int  position)
{
        LNode 
* node;
        
if (NULL  !=  head)
        {
LNode 
* p1;
                p1
= head;
                
while ((position -- ) > 1   &&  NULL  != p1 -> next)
                {
                        p1
= p1 -> next;
                }
                
if (NULL  !=  p1)
                        node
= p1;
        }
        
return  node;
}
void  main()
{
        LNode 
* node;
        node
= NULL;
        node
= CreateList(node, 3 );
        
if  (node  ==  NULL)
        {
                printf(
" Create List Failure!\n " );
        }
        DisplayList(node);
        
int  pos,val = 1 ;
        
while (val > 0 )
        {
                printf(
" Please input the data position and value(as 1,2 end by value<1) which you want to insert: " );
                scanf(
" %d,%d " , & pos, & val);
                node
= InsertElement(node,pos,val);
                DisplayList(node);
        }
        LNode 
* elem;
        pos
= 1 ;
        
while (pos)
        {
                printf(
" Please input the position no. which element you want to find(end by position<1): " );
                scanf(
" %d " , & pos);
                elem
= GetElement(node,pos);
printf(
" Element at %d  data: %d\n " ,pos,elem -> data);
        }
        free(node);
}

 

以下为部分截图:

源码图

测试图

 

 注:未完待续(只写了创建链表、插入、获取、打印四个函数)

转载于:https://www.cnblogs.com/Rockay/archive/2011/07/27/listnode.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值