线性表-链表

#include
#include


typedef int ElemType;
typedef struct node {
ElemType data;
struct node *next;
}LNode, *LinkList;


LinkList CreateLinkList1(); //头插法
LinkList CreateLinkList2(); //尾插法
void LengthLinkList(); //表长
LinkList Find1(); //按序号查找
LinkList Find2(); //按值查找
void Print(); //输出链表(不输出第一个空值)
void Print1(); //输出链表(输出第一个值)
int InsertLinkList(); //链表的插入(指定位置的后面)
int DeleteLinkList(); //链表的删除
void Reverse(); //链表倒置
void PurLinkList(); //删除重复节点
void Difference(); //两个集合的差集


void main () {
LinkList H = (LinkList)malloc(sizeof(LNode));
// LinkList s = (LinkList)malloc(sizeof(LNode));
// H = CreateLinkList1();
H = CreateLinkList2();
Print(H);
LengthLinkList(H);
// H = Find1(H, 5); //位置 : 5
// printf("%d",InsertLinkList(H, 5, 111)); //位置 : 5
// printf("%d",DeleteLinkList(H, 0)); //位置 : 1
Reverse(H);
Print(H);
}




LinkList CreateLinkList1 () {
int x ;
LinkList s;
LinkList H = (LinkList)malloc(sizeof(LNode));
H->next = NULL;
scanf("%d",&x);
while (x != -1) {
s = (LinkList)malloc(sizeof(LNode));
s->data = x;
s->next = H->next;
H->next = s;
scanf("%d",&x);
}
return H;
}


LinkList CreateLinkList2 () {
int x;
LinkList H, r, s;
H = (LinkList)malloc(sizeof(LNode));
H->next = NULL;
r = H;
scanf("%d",&x);
while (x != -1) {
s = (LinkList)malloc(sizeof(LNode));
s->data = x;
s->next = r->next;
r->next = s;
r = s;
scanf("%d",&x);
}
return H;
}


void LengthLinkList(LinkList l) {
int i = 0 ;
LinkList p = l;
printf("\n*********输出链长***********\n");
while (p = p->next) i++;
printf("表长 = %d\n",i);
}




LinkList Find1 (LinkList l, int k) {
int i = 0;
LinkList p = l;
while (p->next != NULL && i < k) {
p = p->next;
i++;
}
if (i == k) 
return p;
else 
return NULL;
}


LinkList Find2 (LinkList l, ElemType x) {
LinkList p = l->next;
while (p != NULL && p->data != x) 
p = p->next;
return p;
}




int InsertLinkList(LinkList l, int k, ElemType x) {
LinkList p, s;
if (l == NULL) {
printf("该链表为空\n");
return -1;
}
else {
p = Find1(l, k);
if (p == NULL) {
printf("第%d个节点不存在\n",k);
return 0;

else {
s = (LinkList)malloc(sizeof(LNode));
s->data = x;
s->next = p->next;
p->next = s;
return 1;
}

}
}




int DeleteLinkList (LinkList l, int k) {
LinkList p, q;
printf("\n***************链表删除****************\n");
if (l == NULL) {
printf("该链表为空\n");
return -1;
}
else {
p = Find1(l, k);
if (p == NULL) {
printf("第%d个节点不存在\n",k);
return -1;
}
else {
q = p->next;
p->next = q->next;
free(q);
return 1;
}
}
}


void Reverse (LinkList l) {
LinkList p, q;
p = l->next;
l->next =NULL;
while (p) {
q = p;
p = p->next;
q->next = l->next;
l->next = q;
}
}


void PurLinkList (LinkList l) {
LinkList p, q, r;
p = l->next;
if (p != NULL) 
while (p->next) {
q = p;
while (q->next) {
if (q->next->data == p->data) {
r = q->next;
q->next = r->next;
free(r);
}
else 
q = q->next;
}
p = p->next;
}
}


void Difference (LinkList la, LinkList lb) {
LinkList pre, p , r;
pre = la;
p = la->next;
while (p != NULL) {
r = lb->next;
while (r != NULL && r->data != r->data)
r = r->next;
}
}


void Print (LinkList l) {
int i = 1;
LinkList p = l;
printf("\n*********输出链表***********\n");
if (p == NULL)
printf("sdfasfdf");
else
while (p = p->next) {
printf("第%d位 : %d\n",i , p->data);
i++;
}
}


void Print1 (LinkList l) {
int i = 1;
LinkList p = l;
printf("\n*********输出链表***********\n");
while (p) {
printf("第%d位 : %d\n",i , p->data);
p = p->next;
i++;
}
}

你可能感兴趣的:(线性表-链表)