头指针链表

来源:互联网 发布:webstorm运行js代码 编辑:程序博客网 时间:2024/05/16 06:42
#include <stdio.h>
#include <stdlib.h>


#define TRUE   1
#define FALSE  0




typedef int LinkData;    // 链表的数据类型
typedef struct _node
{
LinkData data;       // 链表的数据
struct _node *next;  // 指向链表下一个结点的指针
}Node;


// 链表的头插
int Insert_Head(Node **h, LinkData data)
{
if (h == NULL)
return FALSE;

// 创建新节点
Node* node = (Node*)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
{
return FALSE;
}

// 给结点成员变量赋值
node->data = data;
node->next = *h;

// 让新节点变为链表的第一个结点
*h = node;

return TRUE;
}




// 尾插
int Insert_Last(Node **h, LinkData data)
{
if (h == NULL)
{
return FALSE;
}

// 创建新节点
Node* node = (Node*)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
{
return FALSE;
}

// 给结点成员变量赋值
node->data = data;
node->next = NULL;

// 找最后一个结点
Node * tmp = *h;  // 指向第一个结点
if (tmp == NULL)  // 空表
{
*h = node;
}
else
{
while (tmp->next)
{
tmp = tmp->next;
}

tmp->next = node;
}


return TRUE;
}




// 在第 pos 个节点处插入数据,链表结点从1开始,没有第0个结点
int Insert_Pos (Node** h, int pos, LinkData data)
{
if (h == NULL || pos < 1)
return FALSE;

// 创建新节点
Node* node = (Node*)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
{
return FALSE;
}

// 给结点成员变量赋值
node->data = data;



// 空表的状态下,只能插入在第一个结点处
if (*h == NULL)  
{
if (pos != 1)
{
printf ("当前为空表,无法在第 %d 结点处插入数据\n", pos);
free(node);
return FALSE;
}
node->next = NULL;
*h = node;
}
else  // 非空表,需要找到插入位置的前一个结点
{
if (pos == 1)
{
node->next = *h;
*h = node;
}
else
{
int i;
Node *tmp = *h;     // tmp 开始的时候指向第一个结点
for (i = 0; i < pos-2; i++)
{
if (tmp == NULL)  // 如果 pos 太大,会造成越界
break;
tmp = tmp->next;
}

if (tmp == NULL)
{
printf ("插入位置越界\n");
free(node);
return FALSE;
}

node->next = tmp->next;
tmp->next = node;
}
}

return TRUE;
}


int Delete_Pos(Node** h, int pos)
{
if (h == NULL || *h == NULL || pos < 1)
return FALSE;

Node *tmp = *h;
if (pos == 1)
{
*h = tmp->next;
free(tmp);
}
else
{
int i;
for (i = 0; i < pos-2; i++)
{
if (tmp->next == NULL)  // 如果 pos 太大,会造成越界
break;
tmp = tmp->next;
}


if (tmp->next == NULL)
{
printf ("删除位置越界\n");
return FALSE;
}
Node* p = tmp->next;
tmp->next = p->next;
free(p);
}


return TRUE;
}


int Reverse_List(Node **h)
{
// *h == NULL 空表 (*h)->next == NULL 只有一个元素
if (h == NULL || *h == NULL || (*h)->next == NULL)
return FALSE;

Node *pre = *h;
Node *cur = (*h)->next;
Node *tmp;

while (cur)
{
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}

(*h)->next = NULL;
*h = pre;

return TRUE;
}




void Display(Node *h)
{
if (h == NULL)
return;
int count = 0;
while (h)
{
if (count++ % 4 == 0)
printf ("\n");
printf ("%8d", h->data);

h = h->next;
}
printf ("\n");
}


int main()
{
Node * head = NULL;   // 指向链表第一个结点的指针(头指针)

// 插入元素
int i;


for (i = 0; i < 10; i++)
{
//Insert_Head(&head, i);
Insert_Last(&head, i);
}
#if 0
Insert_Pos(&head, 1, 1000);
Insert_Pos(&head, 10, 2000);
Insert_Pos(&head, 13, 3000);
Insert_Pos(&head, 15, 3000);
Insert_Pos(&head, 0, 1000);
#endif
//Delete_Pos(&head, 11);
Display(head);

Reverse_List(&head);
Display(head);

return 0;
}
原创粉丝点击