您的位置:首页 > 其它

单链表(表头结点)

2017-08-04 20:38 211 查看
#include <stdio.h>
#include <stdlib.h>

#define OK              0
#define ERROR          -1
#define MALLOC_ERROR   -2

typedef int ElementType;       //c语言中没有ElementType这个数据类型,讲数据结构时,用这个来泛指某一种数据类型
typedef struct node
{
ElementType data;          //结点的数据
struct node *next;         //结点指针
}Node;
typedef Node *PNode;           //重命名结点指针类型

//头插法创建链表
int Create_List_Head(PNode h, ElementType data)
{
if (h == NULL)
{
return ERROR;
}

PNode node =  (PNode)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
{
return MALLOC_ERROR;
}
node->data = data;
node->next = h->next;
h->next = node;

return OK;
}

//尾插法创建链表
int Create_List_Tail(PNode h, ElementType data)
{
if (h == NULL)
{
return ERROR;
}

PNode node = (PNode)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
{
return MALLOC_ERROR;
}
node->data = data;
node->next = NULL;

//找最后一个结点
PNode temp = h;
while (temp->next)
{
temp = temp->next;
}
temp->next = node;

return OK;
}

//在第pos个结点处插入一个数据
int Insert_Node(PNode h, int pos, ElementType data)
{
PNode temp = h;
int k = 0;

//找pos-1个结点
while (temp && k < pos-1)
{
temp = temp->next;
k++;
}

if (temp == NULL)        //越界
{
printf ("无效的插入点\n");
return ERROR;
}

PNode node = (PNode)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
{
return MALLOC_ERROR;
}
node->data = data;
node->next = temp->next;
temp->next = node;

return OK;
}

//在链表中删除第pos个结点
int Delete_Node(PNode h, int pos)
{
PNode temp = h;
int k = 0;

//找到删除的结点前一个结点,就是temp
while (temp && k < pos-1)
{
temp = temp->next;
k++;
}

if (temp == NULL || temp->next == NULL)        //越界
{
printf ("删除位置越界\n");
return ERROR;
}

PNode p = temp->next;
temp->next = p->next;
free(p);
p = NULL;

return OK;
}

//链表逆序
int Inverse_List(PNode h)
{
//h == NULL    头结点开辟失败,为空
//h->next == NULL      空表
//h->next->next == NULL    只有一个结点
if (h->next == NULL || h->next->next == NULL)
{
return OK;
}

PNode pre = h->next;
PNode cur = pre->next;
PNode next = NULL;

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

/* h->next->next == NULL;      //两个等于号!!!,将原来的头指针的指针变为尾指针置空
h->next = pre;              //将头结点指向现在的第一个结点,重置头结点的next

return OK; */

h->next->next = NULL;
h->next = pre;              //将头结点指向现在的第一个结点,重置头结点的next

return OK;
}

//查找链表中的元素,找到后返回该结点的指针
PNode Search(PNode h, ElementType data)
{
if (h == NULL)
{
return NULL;
}

PNode temp = h->next;
while(temp)
{
if (temp->data == data)
{
return temp;
}
temp = temp->next;
}

return NULL;
}

//计算链表的长度
int ListLen(PNode h)
{
int len = 0;
PNode temp = h->next;
while(temp)
{
temp = temp->next;
len++;
}

return len;
}

//打印
void Display(PNode h)
{
if (h == NULL)
{
return;
}
PNode temp = h->next;       //链表第一个结点指针
while (temp)
{
printf ("%4d",temp->data);
temp = temp->next;
}

printf ("\n");
}

int main()
{
PNode head_node = (PNode)malloc(sizeof(Node)/sizeof(char));
if (head_node == NULL)
{
return MALLOC_ERROR;
}
head_node->next = NULL;      //空链表

int i = 0;
for (i = 0; i < 10; i++)
{
/* //头插法创建链表
if (Create_List_Head(head_node,i) != OK)
{
return ERROR;
} */

//尾插法创建链表
if (Create_List_Tail(head_node,i) != OK)
{
return ERROR;
}

}

/* //在第pos个结点处插入一个数据
if (Insert_Node(head_node, 6, 7) != OK)
{
return ERROR;
}

//在链表中删除第pos个结点
if (Delete_Node(head_node,4) != OK)
{
return ERROR;
} */

//将链表逆序
if (Inverse_List(head_node) != OK)
{
return ERROR;
}

Display(head_node);

//查找元素
PNode p = Search(head_node, 5);
if(p == NULL)
{
printf("无此结点\n");
}
else
{
printf("%d\n",p->data);
}

//计算长度
int len = ListLen(head_node);
printf("%d\n",len);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: