数据结构单链表代码:
为了避免同一个头文件被包含(include)多次,C/C++中有两种宏实现方式:一种是#ifndef方式;另一种是#pragma once方式。
#pragma once
//或者
#ifndef __AAAA__
#define __AAAA__
/*
*/
#endif
#pragma once
#include <stdio.h>
#include <stdlib.h>
typedef int Status;
typedef int Elemtype;
#define OK 1
#define FAIL 0
#define EXITFLOW -1
typedef struct CirCalList
{
Elemtype data;
struct CirCalList* next;
}CirCalList,*CirCalLNode;
//初始化单链表
CirCalLNode Init_list();
//毁坏单链表
Status Destroy_list(CirCalLNode L);
//清除单链表
Status Clear_list(CirCalLNode L);
//获得单链表第i个值
Status Get_Element(CirCalLNode L, int i, Elemtype* e);
//单链表头插法
Status InsertHead_list(CirCalLNode L, int n);
//单链表尾插法
Status InsertTrail_list(CirCalLNode L, int n);
//单链表翻转
Status Traverse_list(CirCalLNode L);
//单链表前驱元素
Status Prior_list(CirCalLNode L, int i, int* e);
//单链表后驱元素
Status Next_list(CirCalLNode L, int i, int* e);
//单链表是否为空
Status IsEmpty_list(CirCalLNode L);
//单链表长度
Status Length_list(CirCalLNode L, Elemtype* e);
//单链表遍历
Status Read_list(CirCalLNode L);
#include "CirclalList.h"
//初始化单链表
CirCalLNode Init_list()
{
CirCalLNode L = (CirCalLNode)malloc(sizeof(CirCalList));
if (!L)
exit(EXITFLOW);
L->next = NULL;
return L;
}
//毁坏单链表
Status Destroy_list(CirCalLNode L)
{
CirCalLNode q = L->next;
CirCalLNode p = NULL;
while (q)
{
p = q->next;
free(q);
q = p;
if (p->next==NULL)
{
break;
}
p = p->next;
}
free(p);
free(L);
return OK;
}
//清除单链表意思是删除其余节点只剩下头结点
Status Clear_list(CirCalLNode L)
{
CirCalLNode q = L->next;
CirCalLNode p = NULL;
while (q)
{
p = q->next;
free(q);
q = p;
p = p->next;
}
free(p);
free(q);
L->next = L;
return 0;
}
//获得单链表第i个值
Status Get_Element(CirCalLNode L,int i,Elemtype *e)
{
CirCalLNode p = L;
for (int j=0;j<i;j++)
{
p = p->next;
}
*e = p->data;
return OK;
}
//单链表头插法
Status InsertHead_list(CirCalLNode L,int n)
{
if (!L)
{
exit(EXITFLOW);
}
L->next = NULL;
for (int i=0;i<n;i++)
{
CirCalLNode P = (CirCalLNode)malloc(sizeof(CirCalList));
if (!P)
{
exit(EXITFLOW);
}
scanf_s("%d", &P->data);
P->next = L->next;
L->next = P;
}
return OK;
}
//单链表尾插法
Status InsertTrail_list(CirCalLNode L,int n)
{
CirCalLNode q = L;
CirCalList* P = NULL;
for (int i = 0; i < n; i++)
{
P = (CirCalLNode)malloc(sizeof(CirCalList));
if (!P)
{
exit(EXITFLOW);
}
scanf_s("%d", &P->data);
q->next = P;
q = P;
}
P->next = NULL;
return OK;
}
//单链表翻转,采用头插法将列表翻转
Status Traverse_list(CirCalLNode L)
{
if (L->next==NULL)
{
exit(EXITFLOW);
}
CirCalLNode p, q;
p = L->next;
q = p->next;
L->next = NULL;
while (p)
{
p->next = L->next;
L->next = p;
p = q;
if (p->next==NULL)
{
break;
}
q = q->next;
}
q->next = L->next;
L->next = q;
return OK;
}
//单链表前驱元素
Status Prior_list(CirCalLNode L,int i,int *e)
{
if (L->next == NULL)
{
exit(EXITFLOW);
}
CirCalLNode p = L->next;
CirCalLNode q =NULL;
while (p->data!=i)
{
q = p;
p = p->next;
}
*e = q->data;
return 0;
}
//单链表后驱元素
Status Next_list(CirCalLNode L,int i,int *e)
{
if (L->next==NULL)
{
exit(EXITFLOW);
}
CirCalLNode p = L->next;
while (p->data != i)
{
p = p->next;
}
*e = p->next->data;
return OK;
}
//单链表是否为空
Status IsEmpty_list(CirCalLNode L)
{
if (L->next ==NULL)
{
return OK;
}
return FAIL;
}
//单链表长度
Status Length_list(CirCalLNode L,Elemtype *e)
{
int i = 0;
CirCalLNode p = L;
while (p)
{
p = p->next;
i++;
}
*e = i;
return OK;
}
//单链表遍历
Status Read_list(CirCalLNode L)
{
CirCalLNode p;
p = L->next;
while (p)
{
printf("%d", p->data);
p = p->next;
}
return OK;
}
来源:CSDN
作者:心寒语录
链接:https://blog.csdn.net/jiyukun1/article/details/104340477