标签:
#include <stdio.h>
#include <stdlib.h>
#define OVERFLOW -2
#define OK 1
#define ERROR 0
typedef int ElemType;
//单向链表结构体
typedef struct LNode {
ElemType data;
struct LNode *next;
}LNode,*LinkList;
LinkList CreateList_L(LinkList L,int n);
void TraverseList_L(LinkList L);
int GetElem_L(LinkList L,int i,ElemType *e);
LinkList ListInsert_L(LinkList L,int i,ElemType e);
LinkList ListDelete(LinkList L,int i,ElemType *e);
LinkList MergeList_L(LinkList La,LinkList Lb,LinkList Lc);
LocateList_L(LinkList L,ElemType e);
LinkList InverseList_L(LinkList L);
int main()
{
LinkList lin,lin1,lin2;
int n,e,i;
lin->next=NULL;
printf("请输入元素的个数: ");
scanf("%d",&n);
lin = CreateList_L(lin,n);
printf("链表中的元素为: ");
TraverseList_L(lin);
if(GetElem_L(lin,1,&e))
printf("第i个元素为:%d\n",e);
lin = ListInsert_L(lin,1,6);
printf("插入一个元素后的链表为:");
TraverseList_L(lin);
lin = ListDelete(lin,1,&e);
printf("删除一个元素后的链表为:");
TraverseList_L(lin);
printf("删除的元素为:%d\n",e);
printf("请输入元素的个数: ");
scanf("%d",&n);
lin1 = CreateList_L(lin1,n);
//合并后的链表为:
printf("合并后的链表为:");
lin2 = MergeList_L(lin,lin1,lin2);
TraverseList_L(lin2);
//取得某一元素的位序为:
i = LocateList_L(lin2,3);
if(i == 0) {
printf("未找到该元素:\n");
} else {
printf("该元素的位序为:%d\n",i);
}
//将单向链表逆置
printf("逆置后的链表为: ");
lin = InverseList_L(lin);
TraverseList_L(lin);
return 0;
}
//创建一个单向链表
LinkList CreateList_L(LinkList L,int n) {
int i;
LinkList p;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
printf("请输入元素的值:");
for(i=n; i>0; --i) {
p = (LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next = L->next;
L->next = p;
}
return L;
}
//遍历链表
void TraverseList_L(LinkList L) {
LinkList p;
p = L->next;
while(p) {
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
//取得链表上第i个元素
int GetElem_L(LinkList L,int i,ElemType *e) {
LinkList p;
int j;
p = L->next;
j = 1;
while(p && j<i) {
p = p->next;
++j;
}
if(!p||j>i) return ERROR;
*e = p->data;
return OK;
}
//向链表中插入一个元素
LinkList ListInsert_L(LinkList L,int i,ElemType e) {
LinkList p,s;
int j;
p = L;
j = 0;
while(p && j<i-1) {
p = p->next;
++j;
}
if(!p || j>i-1) return ERROR;
s = (LinkList)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
return L;
}
//从链表删除一个元素
LinkList ListDelete(LinkList L,int i,ElemType *e) {
LinkList p,q;
int j;
p = L;
j = 0;
while(p->next && j<i-1) {
++j;
p = p->next;
}
if(!(p->next) || j>i-1) return ERROR;
q = p->next;
p->next = q->next;
*e = q->data;
free(q);
return L;
}
//将两个链表进行归并排序合并
LinkList MergeList_L(LinkList La,LinkList Lb,LinkList Lc) {
LinkList pa,pb,pc;
pa = La->next;
pb = Lb->next;
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);
return Lc;
}
//取得某一个元素的位序
int LocateList_L(LinkList L,ElemType e) {
LinkList p;
int i;
p = L->next;
i = 0;
while(p) {
if(p->data == e) {
++i;
break;
}
p = p->next;
}
if(p == NULL) {
return 0;
} else {
return i;
}
}
//将单向链表逆置
LinkList InverseList_L(LinkList L) {
LinkList pre,cur,next;
pre = L->next;
cur = pre->next;
next = cur->next;
pre->next = NULL;
cur->next = pre;
pre = cur;
cur = next;
while(cur != NULL) {
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
L->next = pre;
return L;
}
标签:
原文地址:http://www.cnblogs.com/chengzi123/p/4330942.html