链表的实现(二)


头文件:

list.h

typedef int ElementType;

#ifndef _LIST_H
#define _LIST_H

struct node;//节点

typedef struct node *ptr_node;//指向节点的指针

typedef ptr_node List;

typedef ptr_node position;

List init_list(void);

void delet_list(List);

int is_empty(List);

void delet_node(List,position);

void insert_node(position,int);

position find_node(List,int);

position find_previous(List,position);

position find_last(List );

void print_list(List);

#endif


fatal.h:

#include <stdio.h>
#include <stdlib.h>

#define Error(Str) FatalError(Str)
#define FatalError(Str) fprintf(stderr,"%s\n",Str),exit(1)

原文件 main.c:

#include "list.h"
#include <stdlib.h>
#include "fatal.h"

struct node{
    ElementType element;
    position next;
};

int main()
{
    List L;
    position np;
    
    int i;
    /* elements to be put into the list */
    int a[] = {1, 3, 5, 7, 9};
    
    L = init_list();
    print_list(L);
    
    for (i=4; i>=0; i--) {
        insert_node(L, a[i]);
    }
    print_list(L);
    
    /* delete first node with value 5 */
    np = find_node(L, 5);
    //delete_node(L, np);
    print_list(L);
    
    //delete_list(L);
    L = init_list();
    print_list(L);
    
    for (i=4; i>=0; i--) {
        insert_node(L, a[i]);
    }
    print_list(L);
    
    //delete_list(L);
}


//判断表是否为空
int is_empty(List L){
    return L->next==NULL;
}

//创建表
List init_list(void){
    List L;
    L=(position)malloc(sizeof(struct node));
    L->next=NULL;
    return L;
}

//删除表
void delet_list(List L){
    position np,next;
    np=L;
    do{
        np=np->next;
        free(np);
        np=next;
    }while(next!=NULL);
}

//判断表是否为空
//int is_empty(List L){
    //return ((L->next)==NULL);
//}

//打印表
void print_list(List L){
    position np;
    
    if(is_empty(L)){
        printf("tne list is empty");
    }
    
    np=L;
    while(np->next!=NULL){
        np=np->next;
        printf("%p:%d",np,np->element);
    }
}

//插入元素
void insert_node(position p,int x){
    position node_add;
    node_add=(position)malloc(sizeof(struct node));
    node_add->next=p->next;
    p->next=node_add;
    node_add->element=x;
}

//删除元素
void delet_node(List L,position p){
    position previous,next;
    next=p->next;
    previous=find_previous(L, p);
    if(previous!=NULL){
        previous->next=next;
    }
    else{
        printf("erro");
    }
}

//寻找最后一个节点
position find_last(List L){
    position np;
    np=L;
    while(np->next!=NULL){
        np=np->next;
    }
    return np;
}

//找到前一个节点
position find_previous(List L,position npTarget){
    position np;
    np=L;
    while(np->next!=NULL){
        if(np->next==npTarget) return np;
        np=np->next;
    }
    return NULL;
}

//查询
position find_node(List L,int x){
    position np;
    np=L;
    while(np->next!=NULL){
        if(np->element==x) return np;
        np=np->next;
    }
    return NULL;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值