データ構造-チェーンの基本的な操作が実現されます.
3498 ワード
/* */
#include
#include
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OK 1
#define OVERFLOW -2
typedef int status;
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
#include"hsczl.h"
int main()
{
LinkList head,head1,head2;
ElemType m,e;
ElemType d;
int n,i,x,o;
InitList(head);
InitList(head1);
InitList(head2);
printf("h
");
printf(" :");
scanf("%d",&x);
CreateList(head,x);
printList(head);
printf("h1
");
printf(" :");
scanf("%d",&o);
CreateList(head1,o);
printList(head1);
printf(" :");
scanf("%d",&i);
scanf("%d",&m);
InsertList(head,i,m);
printList(head);
printf(" :");
scanf("%d",&i);
DeleteList(head,i);
printList(head);
printf(" :");
scanf("%d",&e);
FindList(head,e);
printf(" :");
scanf("%d",&n);
printf(" :");
scanf("%d",&d);
AlterList(head,n,d);
printList(head);
MergeList(head2,head,head1);
printList(head2);
return 0;
}
/* */
status InitList(LinkList &L) //
{
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
return OK;
}
status CreateList(LinkList &L,int j)//
{
LinkList p;
ElemType e;
for(int i=j;i>0;--i)
{
p=(LinkList)malloc(sizeof(LNode));
scanf("%d",&e);
p->data=e;
p->next=L->next;L->next=p;
}
return OK;
}
void printList(LinkList &L)//
{
LinkList cur1;
cur1=L->next;
while(cur1!=NULL)
{
printf("%d
",cur1->data);
cur1=cur1->next;
}
}
status InsertList(LinkList &L,int i,ElemType t)//
{
int j=0,k=1;
LinkList cur=L,cur2=L;
while(cur->next!=NULL)
{
cur=cur->next;
j++;
}
//free(cur);
if(i<1||i>j)return ERROR;
LinkList s;
s=(LinkList)malloc (sizeof(LNode));
s->data=t;
while(knext;
k++;
}
s->next=cur2->next;
cur2->next=s;
return OK;
}
status DeleteList(LinkList &L,int i)//
{
LinkList Lcur1=L;int j=0,flag=0;
LinkList Lcur2=L;
while(jnext;
j++;flag=1;
}
else
{
Lcur1=Lcur1->next;
Lcur2=Lcur2->next;
j++;
}
}
Lcur2->next=Lcur1->next;
free(Lcur1);
return OK;
}
status FindList(LinkList &L,ElemType e) //
{
LinkList scur;
scur=L->next;
int i=1;
if(scur==NULL)return ERROR;
while(scur!=NULL)
{
if(scur->data==e)
{printf("%d",i);
break;
}
else
i++;
scur=scur->next;
}
if(scur==NULL)
printf(" !");
return OK;
}
status AlterList(LinkList &L,int n,ElemType y)//
{
LinkList Pcur;
Pcur=L->next;int i=1;
if(Pcur==NULL)return ERROR;
while(inext;
i++;
}
Pcur->data=y;
return OK;
}
status MergeList(LinkList &L,LinkList &L1,LinkList &L2) //
{
LinkList Lcur;
LinkList L1cur;
LinkList L2cur;
Lcur=L;
L1cur=L1->next;
L2cur=L2->next;
while(L1cur!=NULL&&L2cur!=NULL)
{
if(L1cur->data>L2cur->data)
{
Lcur->next=L1cur;
Lcur=L1cur;
L1cur=L1cur->next;
}
else
{
Lcur->next=L2cur;
Lcur=L2cur;
L2cur=L2cur->next;
}
}
Lcur->next=L1cur?L1cur:L2cur;
return OK;
}