数据结构之线性表--单链表(自带首尾转换)
SingleList.h 简单的头文件其中Status 我在后面没有用,只是用状态描述了一番
#ifndef SINGLELIST
#define SINGLELIST
typedef int ElemType;
typedef int Status;
typedef struct LinkList
{
ElemType date;
struct LinkList *next;
}LinkNode,*LinkList;//这里只是别名的不同,后来在传参的时候该用指针还得用指针
#endif
#include <stdio.h>
#include <stdlib.h>
#include "SingleList.h"
void InitList( LinkList *ppnode)//初始化单链表
{
*ppnode = (LinkList)malloc(sizeof(LinkNode));//在这里测试将*取消会产生错误
if (!(*ppnode))
{
return -1;
}
(*ppnode)->next = NULL;
printf("初始化成功");
return 0;
}
int ListLength(LinkList *pnode)//返回整个单链表的表长
{
int i = 0;
LinkList p =( *pnode)->next;
while (p)
{
++i;
p = p->next;
}
return i;
}
Status ClearList(LinkList *pnode)//若单链表不为空置空;
{
LinkList p, q;
p = (*pnode)->next;
while (p)
{
q = p->next;
free(p);
p = q;
}
(*pnode)->next = NULL;
return 1;
}
Status ListDisplay(LinkList *pnode)//显示单链表数据
{
LinkList p = (*pnode)->next;
while (p)
{
printf("-> %d", p->date);
p = p->next;
}
printf("
");
return 1;
}
Status GetElem(LinkNode *pnode,int i,ElemType *e)//返回单链表中第i个数据
{
int j;
LinkList p;
p = pnode->next;
j = 1;
while (p&&j < i)
{
p = p->next;
++j;
}
if (!p||j>i)
{
return -1;
}
*e = p->date;
return 1;
}
int LocateElem(LinkList pnode, ElemType e)//定位一个数据
{
int i = 0;
LinkList p = pnode->next;
while (p)
{
++i;
if (p->date == e)
{
return i;
p = p->next;
}
return 0;
}
}
void CreatListHead(LinkList *pnode, int n)//头插法插入数值
{
LinkList p;
int i;
srand(time(0));
(*pnode) = (LinkList)malloc(sizeof(LinkNode));
(*pnode)->next = NULL;
for (int i = 0; i < n;i++)
{
p = (LinkList)malloc(sizeof(LinkNode));
p->date = rand() % 100 + rand() % 50;
p->next = (*pnode)->next;
(*pnode)->next = p;
}
}
void CreateListTail(LinkList *pnode, int n)//尾插数据
{
LinkList p, r;
int i;
srand(time(0));
pnode= (LinkList)malloc(sizeof(LinkNode));
r = pnode;
for (i = 0; i < n; i++)
{
p = (LinkList)malloc(sizeof(LinkNode));
p->date= rand() % 100 + 1;
r->next = p;
r = p;
}
r->next = NULL;
}
Status ListInsert(LinkList pnode, int i, ElemType e)//在i处插入数字e;
{
int j=1;
LinkList p, q;
p = pnode;//节点指向头节点
while (p&&j<i)
{
p = p->next;
++j;
}
if (!p||j>i)
{
return -1;
}
q = (LinkList)malloc(sizeof(LinkNode));
q->date = e;
q->next = p->next;
p->next = q;
return 1;
}
Status ListDelete(LinkList pnode, int i, ElemType *e)//删除i处的e
{
int j = 1;
LinkList p, q;
p = pnode;
while (p||j<i)
{
p = p->next;
++j;
}
if (!(p->next)||j>i)
{
return -1;
}
q = p->next;
p->next = q->next;
*e = q->date;
free(q);
return 1;
}
Status ListReverseMethod(LinkList pnode)//逆序反转单链表
{
LinkList current, pnext, prev;
if ((pnode) == NULL || (pnode)->next == NULL)
return (pnode);
current = pnode->next;
pnext = current->next;
current->next = NULL;
while (pnext)
{
prev = pnext->next;
pnext->next = current;
current = pnext;
pnext = prev;
}
(pnode)->next = current;
return (pnode);
}
Status ListReverseMethod2(LinkList pnode)
{
LinkList current, p;
if ((pnode) == NULL)
{
return NULL;
}
current = (pnode)->next;
while (current->next!=NULL)
{
p = current->next;
current->next = p->next;
p->next = (pnode)->next;
(pnode)->next = p;
ListReverseMethod(pnode);
printf("current= %d ,
", current->date);
}
return pnode;
}
int main(int argc, char **argv)
{
LinkList Pnode;
ElemType e;
InitList(&Pnode);//错误:原因需要传入指针类型因为函数为InitList(LinkList *ppnode);如果为 (LinkList pnode )就可以在这里采用&pnode
CreatListHead(&Pnode, 10);
ListDisplay(&Pnode);
ListReverseMethod(Pnode);
ListDisplay(&Pnode);
system("pause");
}