C语言随笔小算法:单向链表

C语言随笔小算法:单向链表

参考链接:

代码参考:https://blog.csdn.net/go_sann/article/details/80508284

原理参考:https://blog.csdn.net/kangxidagege/article/details/80211225

 

链接文档写的很优秀,作者是以前实现过数据结构,如今就是简单积累回忆下,就不写增删改查和排序了!

代码编译验证OK

代码:

#include "stdlib.h"
#include "stdio.h"
#include "malloc.h"


//    创建单向链表

typedef struct STUDENT
{
    int age;
    int score[3];
    char *name;
	int order;
} student, *_pstudent;


/*
**定义数据域
*/
typedef struct NODE
{
    struct STUDENT element;			//数据类容
    struct NODE *next;				//注意,只能使用节点指针类型struct NODE*
} node, *_pnode;



/******************************************************************************
 *  Function    -  Tracker_Get_IMEI_Req
 *
 *  Purpose     -  创建一个num个节点的单项链表
 *
 *  Description -
 *
 * modification history
  * ----------------------------------------
 * v1.0  , 2011-11-02, ysheng  written
  * ----------------------------------------
 ******************************************************************************/
_pnode initCreate(int num);

/******************************************************************************
 *  Function    -  Tracker_Get_IMEI_Req
 *
 *  Purpose     -  主函数入口
 *
 *  Description -
 *
 * modification history
  * ----------------------------------------
 * v1.0  , 2011-11-02, ysheng  written
  * ----------------------------------------
 ******************************************************************************/


/******************************************************************************
 *  Function    -  Tracker_Get_IMEI_Req
 *
 *  Purpose     -  创建一个num个节点的单项链表
 *
 *  Description -
 *
 * modification history
  * ----------------------------------------
 * v1.0  , 2011-11-02, ysheng  written
  * ----------------------------------------
 ******************************************************************************/
_pnode initCreate(int num)
{
    struct NODE *head = NULL;			//头节点指针
    struct NODE *normal_node = NULL;	//普通节点指针
    struct NODE *tail = NULL;			//尾结点指针

    int i = 1;

   
    /*3.创建其他节点*/
    for(i = 1; i < num + 1; i++)
    {
        normal_node = (node *)malloc(sizeof(node));
		if(head == NULL)
		{
			head = tail = normal_node;
		}
        normal_node->element.order = i;
        printf("initCreate()-->>创建%d个节点!\n", i);
        tail->next = normal_node;	//将新创的节点的地址放到前一个节点的next变量里
        tail = normal_node;			//移动tail指针至新创建的节点
    }
    tail->next = head;				//最后一个节点的指针域存放head头指针的指针域,形成双向链表

    printf("initCreate()-->>创建单项链表完成!\n");

	return head;
}

		//		-->入队
					/************************
				7	|6	|5	|4	|3	|2	|1	|	0                                                                  
					************************/
											//	-->出队


void main(void)
{
	_pnode head = initCreate(6);


}

 

你可能感兴趣的:(c算法,C语言)