List.h
typedef int ElemType;
typedef int Status;
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
void inputList(LinkList &L,int i);
Status GetElem(LinkList L,int i,int m);
Status ListInsert(LinkList &L,int i,ElemType e,int m);
void ListDelete(LinkList &L,int i,ElemType &e,int &m);
void MergeList(LinkList &La,LinkList &Lb,LinkList &Lc);
void DestoryList(LinkList &L);
Status JudgeList(LinkList L);
void invertedList(LinkList &L,LinkList &L1,int i);
List.cpp
#include "StdAfx.h"
#include "LinkList.h"
void inputList(LinkList &L,int i)
{
LinkList p,tail;
int d,j;
L=tail=(LinkList)malloc(sizeof(LNode));
if(i==1)
{
scanf("%d",&d);
L->data=d;
L->next=NULL;
}
else
{
for(j=1;j<=i;j++)
{
scanf("%d",&d);
p=(LinkList)malloc(sizeof(LNode));
p->data=d;
tail->next=p;
tail=p;
}
tail->next=NULL;
}
}
Status GetElem(LinkList L,int i,int m)
{
LinkList p;
p=L->next;
int j=1,e;
while(p&&j<i)
{
p=p->next;
j++;
}
if(j>m)
return ERROR;
else
e=p->data;
return e;
}
Status ListInsert(LinkList &L,int i,ElemType e,int m)
{
LinkList p,s;
p=L->next;
int j=1;
s=(LinkList)malloc(sizeof(LNode));
s->data=e;
if(i==1)
{
L->next=s;
s->next=p;
return (m+1);
}
else
{
while(p&&j<=i-2)
{
p=p->next;
j++;
}
s->next=p->next;
p->next=s;
return (m+1);
}
}
void ListDelete(LinkList &L,int i,ElemType &e,int &m)
{
LinkList p,q;
p=L->next;
int j=1;
while(p->next&&j<i-1)
{
p=p->next;
++j;
}
if(!(p->next)||j>i-1)
printf("ERROR\n");
q=p->next;
p->next=q->next;
e=q->data;
m--;
free(q);
}
void MergeList(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);
}
void DestoryList(LinkList &L)
{
LinkList q,p;
p=L->next;
q=p->next;
while(p)
{
free(p);
p=q;
q=q->next;
}
}
Status JudgeList(LinkList L)
{
LinkList q;
q=L;
if(q->next==NULL)
return OK;
else
return ERROR;
}
void invertedList(LinkList &L,LinkList &L1)
{
LinkList p,q,s;
int j=1;
q=L1;
p=L->next;
while(p)
{
s=(LinkList)malloc(sizeof(LNode));
s->data=p->data;
s->next=L1->next;
L1->next=s;
j++;
p=p->next;
}
}
主文件:
// LinkList.cpp : Defines the entry point for the console application.
//
#include "StdAfx.h"
#include "LinkList.h"
int main()
{
printf("Hello World!\n");
int m,i,n,d,q,k,e,t;
LinkList List,p,Listb,Listc;
printf("输入你想建立的单链表的链长度:\n");
scanf("%d",&m);
inputList(List,m);
p = List->next;
printf("输出链表:\n");
for (i = 0; i < m; i++)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n输入你想要得到的值:\n");
scanf("%d",&d);
n=GetElem(List,d,m);
printf("与%d数序相同的数是:%d\n",d,n);
printf("输入你想要放到几个位置,以及该数为多少;\n");
scanf("%d%d",&q,&k);
m=ListInsert(List,q,k,m);
p = List->next;
printf("输出链表:\n");
for (i = 0; i < m; i++)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n输入你想要删除的值:\n");
scanf("%d",&q);
ListDelete(List,q,e,m);
printf("删除元素的值为:%d\n",e);
printf("删除后的链表:\n");
p = List->next;
for (i = 0; i < m; i++)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n长度为%d\n",m);
printf("输入你想建立的单链表的链长度:\n");
scanf("%d",&t);
printf("建立Lb链表:\n");
inputList(Listb,t);
MergeList(List,Listb,Listc);
printf("输出Listc\n");
p=Listc->next;
for (i = 0; i < m+t; i++)
{
printf("%d ",p->data);
p = p->next;
}
return 0;
}