Kod: Zaznacz cały
#include <stdio.h>
struct Node {
int Value;
struct Node *Next;
};
struct KOLEJKA {
struct Node *Head;
struct Node *Tail;
};
int Push (struct KOLEJKA *K, int V)
{
struct Node *p;
p = (struct Node *) malloc (sizeof(struct Node));
if (!p) return 0;
p -> Value = V;
if (K -> Head == NULL)
{
K -> Head = p;
K -> Tail = p;
}
else
{
p -> Next = NULL;
K -> Tail -> Next = p;
K -> Tail = p;
}
return 1;
}
void List (struct KOLEJKA K)
{
struct Node *p;
p = K.Head;
if (!p) {printf("
EMPTY
"); return;}
printf("HEAD -> ");
while (p) {
printf(" %d ", p->Value);
p = p->Next;
}
printf(" <- TAIL
");
}
int Pop (struct KOLEJKA *K)
{
struct Node *p;
p = K->Head;
if (p == NULL) return 0;
K -> Head = K -> Head -> Next;
p -> Next = NULL;
free(p);
return 1;
}
void Clear (struct KOLEJKA *K)
{
while (Pop(K));
}
int Length (struct KOLEJKA K)
{
int i = 0;
struct Node *p = K.Head;
if(p == NULL) return 0;
while(p) {
i++;
p = p->Next;
}
return i;
}
int Copy (struct KOLEJKA Kopiowana, struct KOLEJKA *Wynikowa)
{
struct Node *p = Kopiowana.Head;
//Wynikowa -> Head = NULL;
while (p) {
Push(Wynikowa, p->Value);
p = p->Next;
}
return 1;
}
int Compare (struct KOLEJKA K1, struct KOLEJKA K2)
{
struct Node *p = K1.Head;
struct Node *q = K2.Head;
if (Length(K1) != Length(K2)) return 0;
while (p){
if (p->Value != q->Value) return 0;
p = p->Next;
q = q->Next;
}
return 1;
}
main()
{
struct KOLEJKA kolejka;
kolejka.Head = NULL;
kolejka.Tail = NULL;
struct KOLEJKA kolejka2;
kolejka2.Head = NULL;
kolejka2.Tail = NULL;
... itd ...
}