データ構造--シングルチェーンテーブル実現キュー1
2396 ワード
#include
#include
/* , , , ,
O(n), O(1), , */
int length;//
typedef struct node //
{
int key;
struct node *Next;
}Node;
typedef struct Q
{
struct node *front;
struct node *end;
}Queue;
Node *insert(int num,Node *head,Node *rear) // , ,
{
if(length==0)
printf(" ,
");
else
{
printf(" :%d
",num);
Node *L = NULL;
Node *p = NULL;
L = head;
while (L->Next != rear)
L = L->Next;//
p = (Node *) malloc (sizeof(Node));//
p->key = num;//
p->Next = rear;//
L->Next = p;
length--;
}
return rear;
}
void Print_List(Node *head) //
{
Node *p = head->Next;
printf(" :
");
while(p->Next != NULL)//
{
printf("%d->", p->key);
p = p->Next;
}
printf("
");
}
Node *delete(Node *head)// , head
{
if(length==10)
printf("
");
else{
printf(" :%d
",head->Next->key);
head=head->Next;
length++;
}
return head;
}
Queue *init(Queue *q,Node *head,Node *rear)//
{
head->Next=rear;
rear->Next=NULL;
q->front=head;
q->end=rear;
return q;
}
Queue *EnQueue(Queue *q,int num)// num
{
q->end=insert(num,q->front,q->end);
return q;
}
Queue *DeQueue(Queue *q)//
{
q->front=delete(q->front);
return q;
}
int main()
{
length=10;
int i;
Node *head = (Node *)malloc(sizeof(Node));
Node *rear = (Node *)malloc(sizeof(Node));
Queue *q= (Queue *)malloc(sizeof(Queue));
q=init(q,head,rear);
for(i=0;i<12;i++)
q=EnQueue(q,i);
for(i=0;i<12;i++)
q=DeQueue(q);
system("PAUSE");
return 0;
}
・