队列采用链式存储结构,实现如下任务:
(1)初始化链队列InitQueue_L(LinkQueue &Q);
(2)入队EnQueue_L(LinkQueue &Q, ElemType e);
(3)取队头元素GetHead_L(LinkQueue Q, ElemType &e);
(4)出队DeQueue_L(LinkQueue &Q, ElemType &e);
(5)输出链队列元素TraverseQueue_L(LinkQueue Q);
(6)销毁链队列DestroyQueue_L(LinkQueue Q)。
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
//存储结构
typedef struct LNode
{
ElemType data; //数据
struct LNode *next; //下一个指针
}LNode;
typedef LNode *LinkList;
void InitList_L(LinkList &L) //初始化
{
L = new LNode;
L->next = NULL;
}
void CreateList_L(LinkList &L) //创建单链表
{
int n,i;
scanf("%d",&n);
LinkList d=L;
LinkList p;
for(i=0;i<n;i++)
{
p = new LNode;
scanf("%d",&p->data);
d->next = p;
d=p;
}
d->next=NULL;
}
void GetElem_L(LinkList L,int i,ElemType &e) //求第i个元素
{
LinkList p=L->next;
int j=1;
while(p&&(j<i))
{
p = p->next;
j++;
}
if(!p||(j>i)) printf("Position Error!\n");
else
{
e=p->data;
printf("Element of location %d: %d\n",i,e);
}
}
LinkList LocateElem_L(LinkList L,ElemType e) //求给定元素的位置
{
LinkList p=L->next;
int i;
for(i=1;(p->data!=e);i++)
{
p=p->next;
if(p==NULL) break;
}
if(p==NULL) printf("Element %d is not exist\n",e);
else printf("The %d at location %d\n",e,i);
return p;
}
void PreElem_L(LinkList L,ElemType e) //求出给定元素的前驱元素
{
LinkList p=L->next;
LinkList p1;
int i;
for(i=1;p->data!=e;i++)
{
p1=p;
p=p->next;
}
if(p==L->next) printf("The %d's previous element is not exist\n",e);
else printf("The %d's previous element is %d\n",e,p1->data);
}
void NextElem_L(LinkList L,ElemType e) //求出给定元素的后继元素
{ LinkList p=L->next;
LinkList p1;
int i;
for(i=1;p&&(p->data!=e);i++)
{
p=p->next;
}
p1=p->next;
if(p1==NULL) printf("The %d's next element is not exist\n",e);
else printf("The %d's next element is %d\n",e,p1->data);
}
LinkList ListInsert_L(LinkList &L,int i,ElemType e) //插入第i个元素
{
LinkList p1= new LNode;
LinkList p2= new LNode;
LinkList p=L;
int j;
for(j=0;j<i-1;++j)
{
p = p->next;
}
if(!p||(j>i-1)) printf("Position Error!\n");
else
{
p2 = p->next;
p->next = p1;
p1->data = e;
p1->next = p2;
}
return p;
}
LinkList ListDelete_L(LinkList &L,int i,ElemType &e) //删除第i个元素
{
LinkList p,q;
int j=0;
p = L;
while(p&&j<i-1)
{
p = p->next;
++j;
}
if(!(p->next)||(j>i-1))
{
printf("Position Error!\n");
return NULL;
}
else
{
q=p->next;
e=q->data;
p->next=q->next;
delete q;
return p;
}
}
void TraverseList_L(LinkList L) //输出单链表元素
{
LinkList p;
p=L->next;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void DestroyList_L(LinkList L) //销毁单链表
{
LinkList p;
while(L)
{
p=L;
L=L->next;
delete p;
}
}
int main()
{
LinkList L;
ElemType e;
int i;
InitList_L(L);
CreateList_L(L);
printf("CreateList_L: ");
TraverseList_L(L);
scanf("%d",&i); //待取元素位置
GetElem_L(L,i,e);
scanf("%d",&e); //待查找的元素
if(LocateElem_L(L,e))
{
PreElem_L(L,e);
NextElem_L(L,e);
}
scanf("%d%d",&i,&e); //插入位置和元素
if(ListInsert_L(L,i,e))
{
printf("LinkList after Insert: ");
TraverseList_L(L);
}
scanf("%d",&i); //删除位置
if(ListDelete_L(L,i,e))
{
printf("Deleted element: %d\n",e);
printf("LinkList after Delete: ");
TraverseList_L(L);
}
DestroyList_L(L);
return 0;
}