zoukankan      html  css  js  c++  java
  • 循环链表的插入和删除

    循环链表可以用来使计算机处理内存工作区或输出至数据缓冲区。

    循环链表的插入和删除

    #include"iostream"
    #include
    "stdlib.h"
    using namespace std;

    struct clist
    {
    int data;
    struct clist *next;
    };
    typedef
    struct clist cnode;
    typedef cnode
    *clink;

    /*-----循环链表的输出------*/

    void printclist( clink head)
    {
    clink ptr;
    head
    =head->next;
    ptr
    =head;
    do
    {
    printf(
    "[%d]",ptr->data);
    ptr
    =ptr->next;
    }
    while(head!=ptr && head !=head->next);
    printf(
    "\n");

    }
    /*-----循环链表的结点插入----*/

    clink insertnode(clink head,clink ptr,
    int value)
    {
    clink new_node;
    new_node
    =(clink) malloc(sizeof(cnode));
    if(!new_node)
    return NULL;
    new_node
    ->data=value;
    new_node
    ->next=NULL;

    if(head==NULL)
    {
    new_node
    ->next=new_node;
    return new_node;
    }
    if(ptr==NULL)
    {
    /*----情况1:插在第一结点之前---*/
    new_node
    ->next=head->next;
    head
    ->next->next=new_node;
    }
    else
    {
    /*-----情况2:插在结点之后-------*/
    new_node
    ->next=ptr->next;
    ptr
    ->next=new_node;
    }
    if(ptr==head)
    head
    =new_node;
    return head;
    }
    /*---循环链表结点删除---*/
    clink deletenode(clink head,clink ptr)
    {
    clink previous;
    if(head==NULL)
    {
    /*----情况1:删除第一个结点----*/
    head
    ->next=ptr->next;
    }
    else
    {
    /*--情况2:删除中间结点---*/
    previous
    =head;
    if(head!=head->next)
    while(previous->next!=ptr)
    previous
    =previous->next;
    previous
    ->next=ptr->next;
    }
    if(ptr==head)
    head
    =previous;
    free(ptr);
    return head;
    }

    /*使用插入结点的方式来创建链表,完成后将链表内容输出,然后删除前后两结点*/

    int main()
    {
    clink head
    =NULL;
    int list[6]={9,7,3,4,5,6};
    int i;

    head
    =insertnode(head,head,list[0]);
    printf(
    "创建第一个结点: ");
    printclist(head);
    /*---情况1:插在第一结点前----*/
    head
    =insertnode(head,NULL,list[1]);
    printf(
    "插入第一结点之前: ");
    printclist(head);
    for(i=2;i<6;i++)
    {
    /*---情况2:插在结点之后-----*/
    head
    =insertnode(head,head->next,list[i]);
    printf(
    "插入结点之后: ");
    printclist(head);
    }
    /*---情况1:删除第一个结点---*/
    head
    =deletenode(head,head->next);
    printf(
    "删除第一个结点: ");
    printclist(head);
    /*--删除最后一个结点--*/
    printf(
    "删除最后一个结点: ");
    head
    =deletenode(head,head);
    printclist(head);
    }
  • 相关阅读:
    音乐播放器
    滚动视图、定时器、分页控件的综合使用
    简易拼图
    IOS开发复习笔记(1)-OC基础知识
    64位matlab mex64位编译器解决方案
    LibSvm添加到Matlab
    code first 数据库无损迁移
    asp.net mvc4连接mysql
    自定义控件引用时候尺寸发生变化
    jquery 操作动态添加的元素
  • 原文地址:https://www.cnblogs.com/FCWORLD/p/1882463.html
Copyright © 2011-2022 走看看