Data_Struct(LinkList)

  最近在学数据结构,学到链表这节作业有链表,毕竟菜鸟代码基本照看书上算法写的,再加上自己的小修改,这里先记录下来,万一哪天想看了,来看看。

  里面有用到二级指针,还是不太理解,还有就是注释不多,后续有了更好的理解,再来添加

 #define TRUE                    1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define INFEASIBLE -1
#define Status int
#define ElemType int typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode; typedef LNode LinkList;
Status ListInsert_L(LinkList L, int i, ElemType e);//插入函数
Status ListDelete_L(LinkList L, int i, ElemType *e);//删除某个元素
void CreateList_L(LinkList L, int n);//创建一个带头结点的空链表
void MergeList_L(LinkList *La, LinkList *Lb, LinkList **Lc); //合并两个有序链表

LinkList.h

 #include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "LinkList.h" //新建带头结点的空链表
void CreateList_L(LinkList **L, int n)
{
int i; LinkList *p = NULL;
(*L) = (LinkList*) malloc (sizeof (LNode));
(*L) ->next = NULL;
printf("请输入链表元素 :\n");
for(i = n; i > ; --i)
{
p = (LinkList*) malloc (sizeof (LNode));//生成新结点
scanf("%d", &p -> data);//输入元素值
p -> next = (*L) -> next;
(*L) -> next = p;//插入到表头
}
} //插入元素
Status ListInsert_L(LinkList *L, int i, ElemType e)
{
LinkList *p = L;
LinkList *s = NULL;
int j = ;
while (p->next && j < i - )
{
p = p -> next;
++ j;
}
if(!p || j != i - )
return ERROR;
s = (LinkList*) malloc (sizeof(LNode));
s -> data = e;
s -> next = p ->next;
p -> next = s;
return OK;
} //删除元素
Status ListDelete_L(LinkList *L, int i, ElemType *e)
{
LinkList *p = L;
LinkList *q;
int j = ;
while(p -> next && j < i - )
{
p = p -> next;
++ j;
}
if(!(p -> next) || j > i - )
return ERROR;
q = p -> next;
p -> next = q -> next;
*e= q -> data;
free(q);
return OK;
} void MergeList_L(LinkList *La, LinkList *Lb, LinkList **Lc)
{
LinkList *pa = La -> next;
LinkList *pb = Lb -> next;
LinkList *pc = NULL;
(*Lc) = pc = La;
while(pa && pb)
{
if(pa -> data <= pb -> data)
{
pc -> next = pa;
pc = pa;
pa = pa ->next;
}
else
{
pc -> next = pb;
pc = pb;
pb = pb -> next;
}
}
pc -> next = pa ? pa : pb;//插入剩余段
free(Lb);//释放Lb头结点
Lb = NULL; } void Free(LinkList *L)
{
if(L && L -> next)
Free( L -> next);
free(L);
return;
} int main()
{
int selectn,length0,length1,location,e,counti;
LNode *L1 = NULL,*L2 = NULL, *L3 = NULL, *la = NULL, *lc = NULL;
printf("请输入新链表长度 :\n");
scanf("%d", &length0);
CreateList_L(&L1, length0);
printf("请选择 : \n");
printf("1 : 插入元素\n");
printf("2 : 删除元素\n");
printf("3 : 合并链表\n");
scanf("%d", &selectn);
switch (selectn)
{
case :
printf("请输入插入位置 :\n");
scanf("%d", &location);
printf("请输入要插入元素 :\n");
scanf("%d", &e);
if(ListInsert_L(L1, location, e) == OK )
{ //逆序输出
counti = ;
la = L1;
printf("新链表的顺序为 :\n");
while(la -> next)
{
la = la->next;
if(counti++ % )
printf("%-5d",la -> data);
else
printf("%-5d\n",la -> data);
}
}
else
printf("插入异常\n");
printf("\n");
break;
case :
printf("请输入要删除的位置 :\n");
scanf("%d",&location);
if(ListDelete_L(L1, location, &e) == OK)
{
printf("删除成功\n被删除元素为 : %-5d\n", e);
}
else
printf("删除异常\n");
break;
case :
printf("请输入链表2 :\n");
printf("请输入链表长度 :\n");
scanf("%d", &length1);
CreateList_L(&L2, length1);
MergeList_L(L1, L2, &L3);
L2 = NULL;
lc = L3 -> next;
printf("新链表顺序为 :\n");
counti = ;
while(lc)
{ if(counti++ % )
printf("%-5d", lc -> data);
else
printf("%-5d", lc -> data);
lc = lc -> next;
}
printf("\n");
break;
default :
printf("ERROR\n");
break;
}
Free(L1);
L1 = NULL;
L3 = NULL;
system("pause");
return ;
}
上一篇:浅谈大数据神器Spark中的RDD


下一篇:VS 域名绑定IIs 调试