Ps:每段代码中,添加了Solo署名的是博主自己写的,其余来自课本或老师。
//单链表存储结构
typedef struct Node //结点类型定义
{
ElemType data;
struct Node *next; //LinkList为结构体指针类型
} Node, *LinkList;
//初始化单链表
InitList(LinkList *L)
{
*L = (LinkList)malloc(sizeof(Node)); //建立头结点
(*L)->next = NULL; //建立空的单链表L
}
//头插法
void CreateFromHead(LinkList L)
/*L是带头结点的空链表头指针,通过键盘输入表中元素值,利用头插法建立单链表L*/
{
Node *s;
char c;
while((c = getchar()) != '$') /*设$元素为输入结束字符*/
{
s = (Node*)malloc(sizeof(Node));
s->data = c;
s->next = L->next;
L->next = s;
}
}
//尾插法
void CreateFromTail()
/*L未初始化,无头结点,通过键盘输入表中元素值,利用尾插法建立单链表L*/
{
LinkList L;
Node *r, *s;
char c;
L = (Node *)malloc(sizeof(Node));
L->next = NULL; //以上包含L的初始化
r = L; //r指针动态指向当前表尾,以便于做尾插入,其初值指向头结点
while((c = getchar()) != '$')
{
s = (Node*)malloc(sizeof(Node));
s->data = c;
r->next = s;
r = s;
}
r->next = NULL;
return L;
}
//在单链表中查找第i个结点
Node * Get(LinkList L, int i)
/*在带头节点的单链表L中查找第i个结点,若找到(1<=i<=n),则返回该结点的存储位置,否则返回NULL*/
{
int j;
Node *p;
if(i <= 0) return NULL;
p = L; j = 0; //从头结点开始扫描
while((p->next != NULL) && j < i)
{
p = p->next; //扫描下一结点
j++; //已扫描结点计数器
}
if(i==j) return p; //找不到,i>n
else return NULL;
}
//在单链表L中查找值为key的结点
Node * Locate(LinkList, ElemType key)
/*在带头结点的单链表L中查找其结点值等于key的第一个结点,若找到则返回该结点的位置p,否则返回NULL*/
{
Node p;
p = L->next;
while(p != NULL)
if(p->data != key) p = p->next;
else break;
return p;
}
//求单链表的长度
int ListLength(LinkList)
{
Node *p;
p = L->next;
j = 0;
while(p != NULL)
{
p = p->next;
j++;
}
return j;
}
//单链表的插入操作
#define OK 1
#define ERROR 0
void InsList(LinkList, ElemType)
/*在带头结点的单链表L中第i个位置插入值为e的新结点*/
{
Node *pre, *s;
int k;
if(i <= 0) return ERROR;
pre = L; k = 0;
while(pre != NULL && k < i-1)
{
pre = pre->next;
k++;
}
if(pre == NULL || k > i-1)
{
printf("插入位置不合理!");
return ERROR;
}
s = (Node *)malloc(sizeof(Node));
s->data = a;
s->next = pre->next;
pre->next;
pre->next = s;
return OK;
}
//单链表删除操作
int DelList(LinkList L; int i; ElemType *e)
/*在带头结点的单链表中删除第i个元素,并将删除的元素保存在*e中*/
{
Node *pre, *r;
int k;
pre = L; k = 0;
while(pre->next != NULL && k < i-1)
/*寻找被删除的结点i的前驱结点i-1使pre指向它*/
{
pre = pre->next;
k++;
}
if(pre->next == NULL || k > i-1)
/*while循环是因为p->next=NULL或i<1而跳出的,因为pre->next为空*.没有找到合法的前驱位置,说明删除位置不合法*/
{
printf("删除结点位置的i不合理");
return ERROR;
}
r = pre->next; //pre指向i-1 r指向i
pre->next = r->next; //使i-1的next指向i+1结点
*e = r->data;
free(r);
return OK;
}
//合并两个有序的单链表
LinkList MergeLinkList(LinkList LA, LinkList LB)
/*将递增有序的单链表LA和LB合并成一个递增有序的单链表LC*/
{
Node *pa, *pb;
LinkList LC;
/*将LC初始置空表。pa和pb分别指向两个单链表LA和LB中的第一个结点,r初值为LC且r始终指向LC的表尾*/
pa = LA->next;
pb = LB->next;
LC = LA;
LC->next = NULL; r = LC;
/*当两个表中均为处理完时,比较选择将较小值存入新表LC中*/
while(pa != NULL && pb != NULL)
{
if(pa->data <= pb->data)
{r->next = pa; r = pa; pa = pa->next;}
else {r-next = pb; r = pb; pb = pb->next}
if(pa) r->next = pa;
else r->next = pb;
}
free(LB);
return(LC);
}
/*删除有序单链表中值重复的结点——Solo*/
int Delsame(LinkList L)
{
Node *pre,*p,*r;
pre = L->next;
p = pre->next;
while(p->next != NULL)
{
if(p->data = pre->data)
{r=p; p=p->next; pre->next = p; free(r)}
else
{pre = p; p = p->next; }
}
return OK;
}
zhihu:Solo | weibo@从流域到海域