基本算法实现小结(一)—— 单链表

简介: 各种基本算法实现小结(一)—— 单链表(均已测试通过) ============================================================单链表(测试通过)   测试环境: Win-TC#include struct _node { ...

各种基本算法实现小结(一)—— 单链表

(均已测试通过)

 

============================================================

单链表(测试通过)   

测试环境: Win-TC

#include <stdio.h>  
struct _node  
{  
    int data;  
    struct _node *next;  
};  
typedef struct _node list;  
void display(list *l)  
{  
    list *p;  
    p=l;  
    while(p->next)  
    {  
        printf("%5d", p->next->data);  
        p=p->next;  
    }  
}  
void main()  
{  
    int i, n;  
    list *h, *p, *s;  
    printf("Enter num n:");  
    scanf("%d", &n);  
    h=(list*)malloc(sizeof(list));  
    h->data=-1;  
    h->next=NULL;  
    s=p=h;  
    for(i=n;i>0;i--)  
    {  
        p=(list*)malloc(sizeof(list));  
        scanf("%d", &(p->data));  
        p->next=h->next;  
        h->next=p;  
        h=h->next;  
    }  
    display(s);  
    getch();  
}  



运行结果:


 

=================================================

单链表各种操作(测试通过)   

测试环境: Win-TC


#include <stdio.h>  
#include <malloc.h>  
#include <stdlib.h>  
struct _node  
{  
    int data;  
    struct _node *next;  
};  
typedef struct _node node, *plist;  
plist init_list()  
{  
    plist pl;  
    pl=(plist)malloc(sizeof(node));  
    if(NULL==pl)  
    {  
        printf("init list,  malloc is fail.../n");  
        return NULL;  
    }  
    pl->data=-1;  
    pl->next=NULL;  
    return pl;  
}  
int isempty_list(plist pl)  
{  
    if(NULL==pl || NULL!=pl->next)  
        return 1;  
    else  
        return 0;  
}  
plist clear_list(plist pl)  
{  
    pl=NULL;  
    return pl;  
}  
void destroy_list(plist pl)  
{  
    plist p, s;  
      
    p=pl->next;  
    while(p)  
    {  
        s=p;  
        p=p->next;  
        free(s);       
    }  
      
    pl=NULL;  
}  
void insert_item(plist pl, int i, int e)  
{  
    int j=1;  
    plist p, s;  
    p=pl;  
    while(p && j<i)  
    {  
        p=p->next;  
        j++;  
    }  
    if(!p || j>i)  /* >len or <1  */  
        printf("Insert fail.../n");  
    s=(plist)malloc(sizeof(node));  
    s->data=e;  
    s->next=p->next;  
    p->next=s;  
}  
void display(plist pl)  
{  
    plist p;  
    p=pl->next;  
    while(pl && p)  
    {  
        printf("%5d", p->data);  
        p=p->next;  
    }  
    printf("/n/n");  
}  
int getbyid_item(plist pl, int i)  
{  
    plist p=pl->next;  
    int j=1;  
    while(p && j<i)  
    {  
        p=p->next;  
        j++;  
    }  
    if(!p || j>i)  /* >len or <1  */  
    {  
        printf("fail.../n");  
        exit(1);  
    }  
    return p->data;  
}  
int locate_item(plist pl, int e)  
{  
    plist p=pl->next;  
    int j=1;  
    while(p->data != e && p->next)  
    {  
        p=p->next;  
        j++;  
    }  
    if(p->data == e)  
        return j;  
    else  
    {  
        printf("There is n %d in list/n", e);  
        return -1;  
    }  
}  
void delete_item(plist pl, int i, int *e)  
{  
    plist p=pl;  
    plist q;  
    int j=1;  
    while(p->next && j<i)  
    {  
        p=p->next;  
        j++;  
    }  
    if(!p->next || j>i)  /* >len or <1  */  
    {  
        printf("fail..../n");  
        return;  
    }  
    q=p->next;  
    p->next=q->next;  
    *e=q->data;  
    free(q);  
}  
int len_list(plist pl)  
{  
    int j=0;  
    plist p=pl;  
    while(pl && p->next)  
    {  
        j++;  
        p=p->next;  
    }  
    return j;  
}  
plist traverse_list(plist pl)  
{  
    plist h, p, s;  
      
    if(!pl || !pl->next)  
        return pl;  
    h=pl->next;  
    s=h;  
    p=s->next;  
    h->next=NULL;  
    while(p)  
    {  
        s=p;  
        p=p->next;  
        s->next=h;  
        h=s;  
    }  
    pl->next=h;  
      
    return pl;  
}  
void main()  
{  
    int len, pos, *del;  
    plist pl=NULL;  
    del=(int *)malloc(sizeof(int));  
    pl=init_list();  
    isempty_list(pl);  
    insert_item(pl, 1, 1);  
    insert_item(pl, 2, 3);  
    insert_item(pl, 3, 5);  
    insert_item(pl, 4, 7);  
    insert_item(pl, 5, 9);  
    insert_item(pl, 6, 11);  
    display(pl);  
    len=len_list(pl);  
    printf("link list len: %d/n", len);  
    pos=locate_item(pl, 7);  
    printf("num 7 pos: %d/n", pos);  
    delete_item(pl, 3, del);  
    printf("delete pos 3 num: %d/n", *del);  
    display(pl);  
    printf("link list traverse.../n");  
    pl=traverse_list(pl);  
    display(pl);  
    destroy_list(pl);  
    getch();  
}  


运行结果:


 

=================================================

单向循环链表(测试通过)   

测试环境: Win-TC

#include <stdio.h>  
#include <malloc.h>  
struct _node  
{  
    int data;  
    struct _node *next;  
};  
typedef struct _node node, *plist;  
plist init_list()  
{  
    plist pl=(plist)malloc(sizeof(node));  
    if(!pl)  
    {  
        printf("error malloc fail.../n");  
        return NULL;  
    }  
    pl->data=-1;  
    pl->next=pl;    /* pl->next=NULL */  
    return pl;  
}  
void insert_item(plist pl, int pos, int data)  
{  
    int j=0;  
    plist p,s;  
    s=p=pl;  
    while(p && j<pos-1)  
    {  
        p=p->next;  
        j++;  
    }  
    if(!p || j>pos-1)  
    {  
        printf("Error insert fail.../n");  
        return;  
    }  
    s=(plist)malloc(sizeof(node));  
    if(!s)  
    {  
        printf("Error malloc fail.../n");  
        return;  
    }  
    s->data=data;  
    s->next=p->next;  
    p->next=s;  
}  
int find_item(plist pl, int data)  
{  
    plist s,p;  
    s=p=pl;  
    p=p->next;  
    while(s != p)  
    {  
        if(data==p->data)  
            return 1;  
        p=p->next;  
    }  
    return 0;  
}  
void delete_item(plist pl, int data)  
{  
    plist p,s;  
    s=p=pl;  
    if(data == p->data) /* first item is equal with data, then last item = second item */  
    {  
        s=p;  
        while(s != p->next)  
            p=p->next;  
        p->next=s->next;  
        return;  
    }  
    while(s != p->next) /* first item is not equal with data */  
    {  
        if(data == p->next->data)  
        {  
            p->next=p->next->next;  
            return;  
        }  
        p=p->next;  
    }  
}  
void display(plist pl)  
{  
    plist s,p;  
    s=p=pl;  
    printf("%5d", p->data); /* print first item */  
    p=p->next;  
    while(s != p)  
    {  
        printf("%5d", p->data);  
        p=p->next;  
    }  
    printf("/n/n");  
}  
void main()  
{  
    int f;  
    plist pl;  
    pl=init_list();  
    insert_item(pl, 1, 1);  
    insert_item(pl, 2, 3);  
    insert_item(pl, 3, 5);  
    insert_item(pl, 4, 7);  
    insert_item(pl, 5, 9);  
    display(pl);  
    printf("Finding 3.../n");  
    f=find_item(pl, 3);  
    if(f)  
        printf("True find 3/n");  
    else  
        printf("False find 3.../n");  
    printf("/nDeleting 1.../n");  
    delete_item(pl->next, 1);  
    display(pl->next);  
    getch();  
}  





运行结果:

 
=================================================

双向循环链表(测试通过)   

测试环境: Win-TC


#include <stdio.h>  
#include <malloc.h>  
struct _node  
{  
    int data;  
    struct _node *prior;  
    struct _node *next;  
};  
typedef struct _node node, *plist;  
plist init_list()  
{  
    plist p;  
    p=(plist)malloc(sizeof(node));  
    if(!p)  
    {  
        printf("Error, malloc fail.../n");  
        return NULL;  
    }  
    p->data=-1;   /* head->data = -1 */  
    p->prior=p;  
    p->next=p;  
    return p;  
}  
void insert_item(plist pl, int pos, int data)  
{  
    int j=0;  
    plist s,p;  
    p=pl;  
    while(p && j<pos-1)  
    {  
        p=p->next;  
        j++;  
    }  
    if(!p || j>pos-1) /* pos is less than 1 or pos larger than len_list+1 */  
    {  
        printf("Error %d is invalide num.../n", pos);  
        return;  
    }  
    s=(plist)malloc(sizeof(node));  
    if(!s)  
    {  
        printf("Error, malloc fail.../n");  
        return NULL;  
    }  
    s->data=data;  
    s->prior=p;  
    s->next=p->next;  
    p->next->prior=s;  
    p->next=s;  
}  
int find_item(plist pl, int data)  
{  
    plist s,p;  
    s=p=pl;  
    if(data == p->data)  
        return 1;  
    p=p->next;  
    while(s != p)  
    {  
        if(data == p->data)  
            return 1;  
        p=p->next;  
    }  
    return 0;  
}  
void delete_item(plist pl, int data)  
{  
    plist s,p;  
    s=p=pl;  
    if(data == p->data) /* first check equal */  
    {  
        p->prior->next=p->next;  
        p->next=p->prior;  
        return;  
    }  
    while(s != p->next)  
    {  
        if(data == p->next->data)  
        {  
            p->next=p->next->next;  
            p->next->next->prior=p;  
        }  
        p=p->next;  
    }  
}  
void display(plist pl)  
{  
    plist s,p;  
    s=p=pl;  
    printf("%5d", p->data);  /* first item, such as head->data is -1 */  
    p=p->next;  
    while(s != p)  
    {  
        printf("%5d", p->data);  
        p=p->next;  
    }  
    printf("/n/n");  
}  
void main()  
{  
    int f;  
    plist pl;  
    pl=init_list();  
    insert_item(pl, 1, 1);  
    insert_item(pl, 2, 3);  
    insert_item(pl, 3, 5);  
    insert_item(pl, 4, 7);  
    insert_item(pl, 5, 9);  
    display(pl);  
    printf("Finding 3.../n");  
    f=find_item(pl->next->next, 3);  
    if(f)  
        printf("True find 3/n");  
    else  
        printf("Fail find 3.../n");  
    printf("Finding 6.../n");  
    f=find_item(pl->prior->prior, 6);  
    if(f)  
        printf("True find 6/n");  
    else  
        printf("Fail find 6.../n");  
    printf("/nDeleting 3.../n");  
    delete_item(pl->next->next, 3);  
    display(pl);  
    getch();  
}  


运行结果:
======================================================

以上代码,均在Win-TC 1.9.1 编译器环境测试通过(测试部分用例)

由于刚刚学习算法,编程水平有限,以上代码多有考虑不周或错误

欢迎大家在下面留言,改正或优化代码,给予指点或建议,谢谢!
相关文章
|
存储 算法 索引
数据结构与算法:单链表
朋友们大家好,本节来到数据结构与算法的新内容:单链表 在上篇文章中,我们知道顺序表通常需要预分配一个固定大小的内存空间, 通常以二倍的大小进行增容,可能会造成空间的浪费,本篇文章我们介绍的链表可以解决这个问题
|
存储 JavaScript 算法
TypeScript算法专题 - blog1.基于TypeScript语言的单链表实现
TypeScript算法专题 - blog1.基于TypeScript语言的单链表实现
297 0
|
存储 JavaScript 算法
TypeScript算法专题 - blog4 - 单链表节点的两-两翻转(两两一组逆序)
TypeScript算法专题 - blog4 - 单链表节点的两-两翻转(两两一组逆序)
226 0
|
存储 算法 Java
算法系列之递归反转单链表
递归反转链表的基本思路是将当前节点的next指针指向前一个节点,然后递归地对下一个节点进行同样的操作。递归的核心思想是将问题分解为更小的子问题,直到达到基本情况(通常是链表末尾)。
539 5
算法系列之递归反转单链表
|
算法 程序员 索引
数据结构与算法学习七:栈、数组模拟栈、单链表模拟栈、栈应用实例 实现 综合计算器
栈的基本概念、应用场景以及如何使用数组和单链表模拟栈,并展示了如何利用栈和中缀表达式实现一个综合计算器。
377 1
数据结构与算法学习七:栈、数组模拟栈、单链表模拟栈、栈应用实例 实现 综合计算器
|
算法 索引
【初阶数据结构篇】单链表算法题进阶
深拷贝应该正好由 n 个全新节点组成,其中每个新节点的值都设为其对应的原节点的值。
178 0
|
存储 算法
数据结构与算法:单链表
数据结构与算法:单链表
219 1
数据结构与算法:单链表
|
算法 C语言
【算法与数据结构】 C语言实现单链表队列详解2
【算法与数据结构】 C语言实现单链表队列详解
143 1
|
算法 程序员 数据处理
【数据结构与算法】使用单链表实现队列:原理、步骤与应用
【数据结构与算法】使用单链表实现队列:原理、步骤与应用
|
算法 C语言
【数据结构与算法 经典例题】返回单链表的倒数第 k 个节点
【数据结构与算法 经典例题】返回单链表的倒数第 k 个节点

热门文章

最新文章