zoukankan      html  css  js  c++  java
  • 链表

    单向链表

    数据元素的逻辑顺序是通过链表中的指针链接次序来实现的。

    1.链表头节点

    为了把插入和删除都统一为后删和后插,增加一个不含数据的链表头节点

    2.头指针

    是指向链表中第一个结点(或为头结点或为数据首结点)的指针
    单链表可由一个头指针唯一确定

    3.头节点

    是在链表的数据首结点之前附设的一个结点;不存数据

    4.数据首结点

    是链表中存储第一个数据元素的结点

    单向链表的定义


    结点

    struct Node
    {
        Type data;
        struct Node* next;
    }
    

    (1)指针后移操作
    p=p->next;
    (2)指向指针的指针
    Node **h;

    基本运算操作的实现


    准构造函数

    void IniNode ( Node *head)        //初始化
    {
        *head=(Node*) malloc(sizeof(Node));
        (*head) -> next = NULL;
    }
    

    创建动态结点

    Node* GetNode(Type item)
    {
    	Node* newnode = (Node*)malloc(sizeof(Node));
    	if (newNode == NULL)
    	{
    		printf("overflow!");
    		exit(1);
    	}
    	newnode->data = item;
    	newnode->next = NULL;
    	return(newnode);
    }
    

    后插运算

    void InsertAfter(Node* p, Node* nextptr)
    {
    	nextptr->next = p->next;
    	p->next = nextptr;
    }
    

    顺序插入(按增序把数据插入链表)

    void Listinsert(Node* head, Type item)
    {
    	Node* p, * newp;
    	p = head;
    	newp = GetNode(item);
    	while (p->next != NULL)
    	{
    		if (p->next ->data < item)
    		{
    			p = p->next;
    		else
    			break;
    		}
    	}
    	InsertAfter(p, newp);
    }
    

    后删

    Node* DeleteAfter(Node* p)
    {
    	Node* t = p->next;
    	if (t != NULL)
    	{
    		p->next = t->next;
    		free(t);
    	}
    }
    

    查找

    Node* FindNode(const Node* h, Type item)
    {
    	Node* p = h->next;
    	while (p != NULL)
    	{
    		if (p->data == item)
    		{
    			break;
    		}
    		p = p->next;
    	}
    	return (p);
    }
    

    清表

    void ListClear(Node* h)
    {
    	Node* p = DeleteAfter(h);//删除数据首结点
    	while (p != NULL)
    	{
    		free(p);//释放空间
    		p = DeleteAfter(h);//继续删除下一个结点
    	}
    }
    

    逆置

    void Revert(Node* head)
    {
    	NOde* rear = head, * del;
    	if (head->next != NULL)
    	{
    		rear = head->next;
    	}
    		while (rear->next != NULL)
    		{
    			del = DeleteAfter(rear);
    			InsertAfter(head, del);
    		}
    }
    
  • 相关阅读:
    Quagga How to use Quagga
    Quagga Case 4
    Quagga Case 3
    Quagga Case 2
    Quagga Routing Suite
    quagga
    quagga 的原理解析 zebra原理解析
    zebra线程管理源码简析
    【习题 4-8 UVA
    【习题 4-7 UVA
  • 原文地址:https://www.cnblogs.com/wangmou-233-1024-com/p/12790412.html
Copyright © 2011-2022 走看看