用两个月的时间好好把数据结构复习一遍,都算不上复习了,最后的图的方面完全是新学,希望能坚持下去。
一、单链表
链表相比较于数组更为灵活,存储方式是链式的,插入删除操作优于数组,但是查询操作优于数组。还是不多介绍了直接上代码吧。(代码参考数据结构与算法分析-c语言版本)
//头文件
#ifndef LIST_H
#define LIST_H
struct Node;
tydedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
typedef int ElementType ;
//return ture if L is empty
int IsEmpty(List L);
//
int IsLast(Position P,List L);
//return position of x in L
Position Find(ElementType x,List L);
//return prveious position of x in L
Position FindPrevious(ElementType x,List L);
//delete x in L
void Delete(ElementType x,List L);
//insert x in L
void Insert(ElementType x,List L);
//delete L
void DeleteList(List L);
struct Node
{
ElementType Element;
Position next;
}
#endif
#include "List.h"
//
int IsEmpty(List L)
{
return L->next==NULL;
}
//
int IsLast(Position P,List L);
{
return P->next==NULL;
}
//return position of x in L
Position Find(ElementType x,List L)
{
Position P;
P=L->next;
while(P!=NULL&&P->Element!=x)
P=P->next;
return P;
}
//return prveious position of x in L
Position FindPrevious(ElementType x,List L)
{
Position P;
P=L;
while(P!=NULL&&P->next->Element!=x)
P=P->next;
return P;
}
//delete x in L
void Delete(ElementType x,List L);
{
Position P,Temp;
P=FindPrevious(x,L);
while(!IsLast(P,L))
{
Temp=P->next;
P->next=Temp->next;
free(Temp);
}
}
//insert x in L
void Insert(ElementType x,List L,Position P)
{
Position Temp;
Temp=malloc(sizeof(struct Node));
if(Temp==NULL)
printf("out of space!!");
Temp->Element=x;
Temp->next=P->next;
P->next=Temp;
}
//delete L
void DeleteList(List L)
{
Position P,Temp;
P=L->next;
L->next=NULL;
while(P!=NULL)
{
Temp=P->next;
free(P);
P=Temp;
}
}
单链表结束。下来应该是双链表。