【c语言】数据结构与算法
标记位:标记更新位置
链表完结
引言
参考教材:王导数据结构
注
:此文不再像Java语言或框架的学习一样,补充很多语句帮助理解
此处更多的是做代码笔记,以便快速复习数据结构常见考察的代码。
结构体
- 定义方式
- 定义一个student类型的结构体
struct student{
int age;
String name;
};
- 定义一个student类型的结构体,并声明一个结构体类型的变量s1;
struct student{
int age;
String name;
}s1;
- 定义一个匿名结构体,并声明一个结构体类型的变量s1
struct{
int age;
String name;
}s1;
- 程序结构
- 把匿名结构体重命名为date
typedef struct{
int age;
String name;
}date;
- 把student结构体重命名为date
typedef struct student{
int age;
int name;
}date;
- 结构体的使用
#include<Stdio.h>
#define MaxSize 10
typedef struct{
int data;
int people;
}date;
typedef struct{
int data[MaxSize];
int length;
} Sqlist;
int main(){
//1.结构体data
date a;
a.data=1;
a.people=2;
printf(" data is %d",a.data);
printf(" people is %d",a.people);
//2.结构体SqList
Sqlist b;
b.length=0;
for(int i=0;i<MaxSize;i++){
b.data[i]=i;
b.length++;
}
for(int i=0;i<MaxSize;i++){
printf("data is %d\n",b.data[i]);
if(i==MaxSize-1) printf("length is %d\n",b.length);
}
}
顺序表
- 线性表
静态初始化
定义顺序表
#define MaxSize 10
typedef struct{
int elem[MaxSize];
int length;
}SqList;
initSqList(SqList & l); 初始静态化顺序表
void initSqList(SqList & l){
for(int i=0;i<MaxSize;i++){
l.elem[i]=0;
}
l.length=0;
}
动态初始化
定义顺序表
#define InitSize 10
typedef struct{
int *data;
int length;
int maxsize;
}SqList;
initSqList(SqList & l); 初始化动态顺序表
//初始化一个顺序表
void initSqList(SqList & l){
l.data=(int *)malloc(sizeof(int)*InitSize);
l.length=0;
l.maxsize=InitSize;
}
increaseSqList(SqList & l,int len); 增加顺序表长度
//动态增长一个顺序表
void increaseSqList(SqList & l,int len){
int *p=(int *)malloc(sizeof(int)*(InitSize+len));
l.maxsize=InitSize+len;
for(int i=0;i<l.length;i++){
l.data[i]=p[i];
}
free(p);
}
基本操作
ListInsert(SqList &L,int index,int element); 将元素e插入到L的第i个位置
bool ListInsert(SqList &L,int index,int element){
if(i<1||i>L.length+1) return false;
for(int i=L.length;i>=index;i--){
L.data[i]=l.data[i-1]
}
L.data[i-1]=element;
L.length++;
return true;
}
ListDelete(SqList &L,int index,int element); 删除L第i个位置的元素删除,并返回e
ListDelete(SqList &L,int index,int element){
if(i<1||i>L.length+1) return false;
element=l.data[index-1]
for(int i=index;i<L.length;i++){
L.data[i-1]=l.data[i]
}
return true;
}
GetElement(SqList &L,int index); 获取表L第i个位置上的元素
int GetElement(SqList &L,int index){
return L.data[index-1];
}
GetElement(SqList &L,int element); 查找表L上第一个元素值等于e的元素,并返回其下标
int GetElement(SqList &L,int element){
for(int i=0;i<L.length;i++){
if(l.data[i]==element) return i+1;
}
return 0;
}
- 单链表
定义
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
上文等价于
struct LNode{
int data;
struct LNode *next;
}
typedef struct LNode LNode;
typedef struct LNode *LinkList;
注
:LNode *L :声明一个指向单链表第一个元素的指针
或:LinkList L :声明一个指向单链表第一个元素的指针
不带头结点的链表
InitList(LinkList &L); 初始化
InitList(LinkList &L){
L=null;
return true;
}
isEmpty(LinkList &L); 判断链表是否为空
bool isEmpty(LinkList &L){
return (L==null);
}
ListInsert(LinkList &L,int index,int element); 在第i个位置插入元素e
bool ListInsert(LinkList &L,int index,int element){
if(index<1) return false;
if(index==1){
LNode *s=(LNode *)malloc(sizeof(LNode));
s->data=element;
s->next=L;
L=s;
return true;
}
LNode *p;
int j=0;
p=L;
while(p!=null&&j<index-1){
p=p->next;
j++;
}
if(p==null) return false;
LNode *s=(LNode *)malloc(sizeof(LNode));
s->data=element;
s->next=p->next;
p->next=s;
return true;
}
带头结点的链表
InitList(LinkList &L); 初始化
bool InitList(LinkList &L){
L=(LNode *)malloc(sizeof(LNode));
if(L==null) return false;
L->next=null;
return true;
}
isEmpty(LinkList &L); 判断链表是否为空
bool isEmpty(LinkList &L){
return (L->next==null);
}
ListInsert(LinkList &L,int index,int element); 在第i个位置插入元素e
bool ListInsert(LinkList &L,int index,int element){
if(index<1) return false;
LNode *p;
int j=0;
p=L;
while(p!=null&&j<index-1){
p=p->next;
j++;
}
if(p==null) return false;
LNode *s=(LNode *)malloc(sizeof(LNode));
s->data=element;
s->next=p->next;
p->next=s;
return true;
}
ListDelete(LinkList &l,int index,int element); 按位序删除
bool ListDelete(LinkList &l,int index,int element){
if(i<1)
return false;
int j=0;
LNode *p=L;
while(j<i-1&&p!=NULL){
j++;
p=p->next;
}
if(p==NULL)
return false;
if(p->next==NULL)
return false;
LNode *q=p->next;
e-q->data;
p->next=q->next;
free(q);
return true;
}
GetElem(LinkList L,int index); 按位查找
LNode * GetElem(LinkList L,int index){
int j=0;
LNode *p=(LNode*)malloc(sizeof(LNode));
p=L;
while(j<i;p!=null){
p=p->next;
j++;
}
return p;
}
LocateElem(LinkList L,int index); 按值查找
LNode *LocateElem(LinkList L,int element){
LNode *p=L->next;
while(p!=NULL && p->data!=e)
p=p->next;
return p;
}
Length(LinkList L); 求表长
int Length(LinkList L){
int length=0;
LNode *p=L->next;
while(L!=NULL){
p=p->next;
length++;
}
return length;
}
LinkList_Create(LinkList &L); 尾插法建立单链表
LinkList LinkList_Create(LinkList &L){
int x;
L=(LinkList)malloc(sizeof(LNode));
scanf("%d",&x);
LNode *s,p=L;
while(x!=999){
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
s->next=p->next;
p=s;
scanf("%d",&x);
}
p->next=NULL;
return L;
}
LinkList_Create(LinkList &L); 尾插法建立单链表
LinkList LinkList_Create(LinkList &L){
int x;
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
scanf("%d",&x);
LNode*s;
while(x!=999){
s=(LNode *)malloc(sizeof(LNode));
s->data=x;
s->next=L->next;
L->next=s;
scanf("%d",&x);
}
return L;
}
均适用
insertNextNode(LNode * p,int element); p结点后插入元素e;
bool insertNextNode(LNode *p,int element){
if(p==NULL)
return false;
LNode *s=(LNode *)malloc(sizeof(LNode));
if(s==null)
return false;
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
insertPriorNode(LNode * p,int element); p结点前插入元素e;
bool insertPriorNode(LNode *p,int element){
if(p==NULL)
return false;
LNode *s=(LNode *)malloc(sizeof(LNode));
if(s==null)
return false;
LNode *q=(LNode*)malloc(sizeof(LNode));
q->data=p->data;
q->next=p->next;
p->next=q;
p->data=element;
return true;
}
InsertPriorNode(LNode * p,LNode * s); 在p结点前插入s
bool InsertPriorNode(LNode *p,LNode *s){
if(s==NULL||p==NULL)
return false;
int x=p->data;
s->next=p->next;
p->next=s;
p->data=s->data;
s->data=x;
return true;
}
DeleteNode(LNode * p); 指定结点删除
bool DeleteNode(LNode *p){
if(p==NULL)
return false;
LNode *q=p->next;
p->next=q->next;
free(q);
return true;
}
双链表
1.定义双链表
typedef struct DNode{
int data;
struct DNode *next,*prior;
}DNode,*DLinkList;
2.初始化双链表
bool initDLinkList(DLinkList &L){
L=(LNode *)malloc(sizeof(LNode));
if(L==NULL)
return false;
L->next=NULL;
L->prior=NULL:
return true;
}
3.判空
bool isEmpty(DLinkList L){
return (L->next==NULL);
}
InsertNextNode(LNode * p,LNode * s); 在p结点后插入s结点
bool InsertNextNode(LNode * p,LNode * s){
if(p==NULL||s==NULL)
return false;
s->next=p->next;
if(p->next!=NULL)
p->next->prior=s;
p->next=s;
s->prior=p;
return true;
}
deleteNextNode(LNode * p); 删除结点p的后继结点
bool deleteNextNode(LNode * p){
if(p==NULL)
return false;
LNode *s=p->next;
if(s==NULL)
return false;
p->next=s->next;
if(s->next!=NULL)
s->next->prior=p;
free(s);
return true;
}
destoryList(DLinkList &L); 销毁双链表L
bool destoryList(DLinkList &L){
if(L==NULL)
return false;
while(L->next!=NULL)
deleteNextNode(L);
free(L);
return true;
}
循环单链表
1.定义循环单链表
typedef struct LNode{
int data;
struct LNode * next;
}
initList(LinkList &L); 初始化循环单链表
bool initList(LinkList &L){
L=(LNode *)malloc(sizeof(LNode));
if(L==NULL)
return false;
L->next=L;
return true;
}
isEmpty(LinkList L); 判断循环单链表是否为空
bool isEmpty(LinkList L){
return (L->next==L);
}
isTail(LinkList L,LNode * p); 判断结点p是否为循环单链表表尾结点
bool isTail(LinkList L,LNode * p){
return (p->next==L);
}
循环双链表
1.定义循环双链表
typedef struct DNode{
int data;
struct DNode *next ,*prior;
}DNode,*DLinkList;
initDList(DLinkLust &L); 初始化循环双链表
bool initDList(DLinkLust &L){
L=(DNode*)malloc(sizeof(DNode));
if(L==NULL)
return false;
L->next=L;
L->prior=L;
return true;
}
isEmpty(DLinkList L); 判断循环双链表是否为空
bool isEmpty(DLinkList L){
return (L->next==L);
}
isTail(DinkList L,DNode * p); 判断结点p是否为循环双链表表尾结点
bool isTail(DLinkList L,DNode * p){
return (p->next==L);
}
insertNextNode(DNode * p,DNode * s); 在p结点之后插入s结点
bool insertNextNode(DNode *p,DNode *s){
if(p==NULL||s==NULL)
return false;
s->next=p->next;
p->next->prior=s;
p->next=s;
s->prior=p;
return true;
}
deleteNextNode(DNode * p); 删除p的后继结点q
bool deleteNextNode(DNode * p){
LNode *q=p->next;
p->next=q->next;
q->next->prior=p;
free(q);
return true;
}