单链表实现(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);
}

 

以下为部分截图:

源码图

测试图

 

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

posted @ 2011-07-27 22:50  Rockay.lau  阅读(447)  评论(0编辑  收藏  举报