#include<stdio.h>
#include<stdlib.h>
typedef struct Queue
{
int data;
struct Queue *next;
}Queue,* LinkQueue;
Queue *InitQueue()
{
Queue *queue = (Queue *)malloc(sizeof(Queue));
if(queue == NULL)
{
printf("create fail \n");
return 0;
}
queue->next = NULL;
return queue;
}
void EnQueue(Queue *queue , int data)
{
Queue *tmp = (Queue *)malloc(sizeof(Queue));
while(queue->next != NULL)
{
queue = queue->next;
}
tmp->data = data ;
queue->next = tmp;
tmp->next = NULL;
}
void DeQueue(Queue *queue,int *data)
{
if(queue->next != NULL)
{
*data = queue->next->data;
queue->next = queue->next->next;
}
}
int main()
{
int i ;
int a[] = {13,27,38,49,65,76,97};
int b[] = {1,2,3,49,70,75,97};
Queue *list1 = InitQueue();
Queue *list2 = InitQueue();
for(i = 0 ; i < sizeof(a)/sizeof(a[0]);i++)
{
EnQueue(list1,a[i]);
EnQueue(list2,b[i]);
}
while(list1->next != NULL && list2 ->next != NULL)
{
if(list1->next->data > list2->next->data)
{
list2 = list2->next ;
}
else if(list1->next->data < list2->next->data )
{
list1 = list1->next;
}
else
{
printf("%5d ",list1->next->data);
list1 = list1->next ;
list2 = list2->next ;
}
}
return 0;
}
运行结果
|