C语言链表
来源:互联网 发布:大数据销售成功案例 编辑:程序博客网 时间:2024/06/07 08:46
// list.h头文件
#ifndef _List_H
struct Node;typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
List MakeEmpty(List L);
int IsEmpty(List L);
int IsLast(Position p,List L);
Position Find(int x,List L);
void Delete(List L,int x);
void Insert(int x,List L,Position p);
void DeleteList(List L);
Position FindPrevious(int x,List L);
Position Header(List L);
Position First(List L);
Position Advance(Position P);
int Retrieve(Position P);
void printlist(List L);
#endif
#include "stdio.h"
#include "stdlib.h"
#include "list.h"
struct Node
{
int element;
Position next;
};
// 判断链表是否为空
int isEmpty(List L)
{
return L->next==NULL;
}
int IsLast(Position p,List L)
{
return p->next==NULL;
}
Position Find(int x,List L)
{
Position p=L->next;
while(p!=NULL&&p->element!=x)
p=p->next;
return p;
}
// 删除
void Delete(List L,int x)
{
Position p,TmpCell;
p=FindPrevious(x,L);
if(!IsLast(p,L))
{
TmpCell=p->next;
p->next=TmpCell->next;
}
}
// 查找前一项
Position FindPrevious(int x,List L)
{
Position p=L;
while(p->next!=NULL&&p->next->element!=x)
p=p->next;
return p;
}
// 插入
void Insert(int x,List L,Position p)
{
Position TmpCell;
TmpCell=(Position)malloc(sizeof(struct Node));
if(TmpCell==NULL)
{
printf("error");
return;
}
TmpCell->element=x;
TmpCell->next=p->next;
p->next=TmpCell;
}
// 删除链表
void DeleteList(List L)
{
Position p,Tmp;
p=L->next;
L->next=NULL;
while(p!=NULL)
{
Tmp=p->next;
free(p);
p=Tmp;
}
}
// 打印
void printlist(List L)
{
Position p=L->next;
while(p!=NULL)
{
printf("%d",p->element);
p=p->next;
}
}
main()
{
List L;
Position p;
L=(Position)malloc(sizeof(struct Node));
if(L==NULL)
{
printf("error");
return;
}
L->next=NULL;
p=(Position)malloc(sizeof(struct Node));
p->element=1;
p->next=L->next;
L->next=p;
Insert(2,L,p);
printlist(L);
}
- C语言链表
- c语言链表
- C语言链表
- c语言链表
- C语言链表
- c语言链表
- C语言链表
- C语言链表
- C语言链表
- c语言链表
- c语言链表
- c语言链表
- c语言链表
- c语言链表
- C语言链表
- c语言链表
- C语言链表
- c语言链表
- 【Spring】 (7)加载外部资源txt,获取 当前bean名称
- [随机] UOJ#79. 一般图最大(随机)匹配
- CMake快速入门教程:实战
- mysql给列的属性赋值,修改列属性,删除记录,删除所有记录,查询表的记录,去除重复的,总和查询,条件查询,模糊查询,关联查询,范围查询
- 指针和引用
- C语言链表
- 软件版本GA,RC,alpha,beta,Build 含义
- const的几种用法
- java程序员从笨鸟到菜鸟之(十四)StringBuffer类
- 在VMware Workstation里 安装 Ubunto 64 位 的参考步骤
- 网络安全-XSS跨站脚本攻击
- cpu的工作原理
- Tesseract-OCR 4.0+ 安装
- mysql排序查询,多条件排序查询,聚合函数的使用,分组统计查询,