Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- struct TNode
- {
- int data;
- struct TNode *next;
- struct TNode *prev;
- };
- struct TList
- {
- struct TNode *head;
- struct TNode *tail;
- };
- void ListInit(struct TList *L)
- {
- (*L).head = NULL;
- (*L).tail = NULL;
- }
- int ListIsEmpty(struct TList L)
- {
- return (L.head == NULL);
- }
- void ListDisplayForward(struct TList L)
- {
- int counter = 0;
- struct TNode *curr = L.head;
- printf("List (head-->tail): ");
- while(curr != NULL)
- {
- printf("%d -> ",curr->data);
- curr = curr->next;
- counter++;
- }
- printf("NULL \n");
- printf("Liczba wezlow listy : %d \n",counter);
- }
- void ListDisplayBackward(struct TList L)
- {
- int counter = 0;
- struct TNode *curr = L.tail;
- printf("List (tail-->head): ");
- while(curr != NULL)
- {
- printf("%d -> ",curr->data);
- curr = curr->prev;
- counter++;
- }
- printf("NULL \n");
- printf("Liczba wezlow listy : %d \n",counter);
- }
- void ListInsert(struct TList *L,int k)
- {
- struct TNode *x = (struct TNode *)malloc(sizeof(struct TNode )) ;
- x->data = k;
- x->next = (*L).head;
- if((*L).head != NULL)
- (*L).head->prev = x;
- (*L).head = x;
- x->prev = NULL;
- if(x->next == NULL)
- (*L).tail = x;
- }
- void ListDelete(struct TList *L)
- {
- struct TNode *x = (*L).head;
- if((*L).head != NULL)
- {
- if((*L).head->next == NULL)
- (*L).tail = (*L).head->prev;
- (*L).head = x->next;
- if(x->next != NULL)
- x->next->prev = x->prev;
- free(x);
- }
- }
- int ListSplit(struct TList L,struct TList *A,struct TList *B)
- {
- int isEmptyB,swapLists;
- struct TNode *currNode,*prevNode;
- swapLists = 1;
- isEmptyB = 1;
- ListInit(A);
- ListInit(B);
- if(L.head != NULL)
- {
- // Usun wezel z glowy listy L
- //Wstaw wezel za ogon listy A
- currNode = L.head;
- if(L.head->next == NULL)
- L.tail = NULL;
- else
- L.head->next->prev = NULL;
- L.head = L.head->next;
- if((*A).head == NULL)
- (*A).head = currNode;
- else
- {
- (*A).tail->next = currNode;
- currNode->prev = (*A).tail;
- }
- (*A).tail = currNode;
- currNode->next = NULL;
- prevNode = currNode;
- }
- while(L.head != NULL)
- {
- // Usun wezel z glowy listy L
- currNode = L.head;
- if(L.head->next == NULL)
- L.tail = NULL;
- else
- L.head->next->prev = NULL;
- L.head = L.head->next;
- if(prevNode->data > currNode->data)
- swapLists = !swapLists;
- if(swapLists)
- {
- //Wstaw wezel za ogon listy A
- if((*A).head == NULL)
- (*A).head = currNode;
- else
- {
- (*A).tail->next = currNode;
- currNode->prev = (*A).tail;
- }
- (*A).tail = currNode;
- currNode->next = NULL;
- }
- else
- {
- //Wstaw wezel za ogon listy B
- if((*B).head == NULL)
- (*B).head = currNode;
- else
- {
- (*B).tail->next = currNode;
- currNode->prev = (*B).tail;
- }
- (*B).tail = currNode;
- currNode->next = NULL;
- isEmptyB = 0;
- }
- prevNode = currNode;
- }
- return isEmptyB;
- }
- void ListMerge(struct TList A,struct TList B,struct TList *L)
- {
- struct TNode *tmpA, *tmpB;
- struct TNode *poprzedniZtmpA = NULL;
- struct TNode *poprzedniZtmpB = NULL;
- int koniecSeriiWtmpA = 0;
- int koniecSeriiWtmpB = 0;
- ListInit(L);
- if(A.head != NULL)
- {
- // Usun wezel z glowy listy A
- tmpA = A.head;
- if(A.head->next == NULL)
- A.tail = NULL;
- else
- A.head->next->prev = NULL;
- A.head = A.head->next;
- }
- if(B.head != NULL)
- {
- // Usun wezel z glowy listy B
- tmpB = B.head;
- if(B.head->next == NULL)
- B.tail = NULL;
- else
- B.head->next->prev = NULL;
- B.head = B.head->next;
- }
- while(A.head != NULL || B.head != NULL)
- {
- while(!koniecSeriiWtmpA && !koniecSeriiWtmpB)
- if(tmpA->data > tmpB->data)
- {
- // Wstaw wezel z listy A za ogon listy L
- if((*L).head == NULL)
- (*L).head = tmpA;
- else
- {
- (*L).tail->next = tmpA;
- tmpA->prev = (*L).tail;
- }
- (*L).tail = tmpA;
- (*L).tail->next = NULL;
- poprzedniZtmpA = tmpA;
- // Usun wezel z glowy listy A
- tmpA = A.head;
- if(A.head->next == NULL)
- A.tail = NULL;
- else
- A.head->next->prev = NULL;
- A.head = A.head->next;
- if(poprzedniZtmpA->data > tmpA->data)
- koniecSeriiWtmpA = 1;
- }
- else
- {
- // Wstaw wezel z listy B za ogon listy L
- if((*L).head == NULL)
- (*L).head = tmpB;
- else
- {
- (*L).tail->next = tmpB;
- tmpB->prev = (*L).tail;
- }
- (*L).tail = tmpB;
- (*L).tail->next = NULL;
- poprzedniZtmpB = tmpB;
- // Usun wezel z glowy listy B
- tmpB = B.head;
- if(B.head->next == NULL)
- B.tail = NULL;
- else
- B.head->next->prev = NULL;
- B.head = B.head->next;
- if(poprzedniZtmpB->data > tmpB->data)
- koniecSeriiWtmpB = 1;
- }
- while(!koniecSeriiWtmpA)
- {
- // Wstaw wezel z listy A za ogon listy L
- if((*L).head == NULL)
- (*L).head = tmpA;
- else
- {
- (*L).tail->next = tmpA;
- tmpA->prev = (*L).tail;
- }
- (*L).tail = tmpA;
- (*L).tail->next = NULL;
- poprzedniZtmpA = tmpA;
- // Usun wezel z glowy listy A
- tmpA = A.head;
- if(A.head->next == NULL)
- A.tail = NULL;
- else
- A.head->next->prev = NULL;
- A.head = A.head->next;
- if(poprzedniZtmpA->data > tmpA->data)
- koniecSeriiWtmpA = 1;
- }
- while(!koniecSeriiWtmpB)
- {
- // Wstaw wezel z listy B za ogon listy L
- if((*L).head == NULL)
- (*L).head = tmpB;
- else
- {
- (*L).tail->next = tmpB;
- tmpB->prev = (*L).tail;
- }
- (*L).tail = tmpB;
- (*L).tail->next = NULL;
- poprzedniZtmpB = tmpB;
- // Usun wezel z glowy listy B
- tmpB = B.head;
- if(B.head->next == NULL)
- B.tail = NULL;
- else
- B.head->next->prev = NULL;
- B.head = B.head->next;
- if(poprzedniZtmpB->data > tmpB->data)
- koniecSeriiWtmpB = 1;
- }
- koniecSeriiWtmpA = 0;
- koniecSeriiWtmpB = 0;
- poprzedniZtmpA = NULL;
- poprzedniZtmpB = NULL;
- }
- }
- int main()
- {
- int k,n,m;
- struct TList L,L1,L2;
- ListInit(&L);
- srand(time(NULL));
- scanf("%d %d",&n,&m);
- for(k = 1;k <= n;k++)
- ListInsert(&L,rand()%m);
- printf("List L \n");
- ListDisplayForward(L);
- ListDisplayBackward(L);
- ListSplit(L,&L1,&L2);
- printf("List L1 \n");
- ListDisplayForward(L1);
- ListDisplayBackward(L1);
- printf("List L2 \n");
- ListDisplayForward(L2);
- ListDisplayBackward(L2);
- while(!ListIsEmpty(L1))
- ListDelete(&L1);
- while(!ListIsEmpty(L2))
- ListDelete(&L2);
- /*
- ListMerge(L1,L2,&L);
- printf("List L \n");
- ListDisplayForward(L);
- ListDisplayBackward(L);
- while(!ListIsEmpty(L))
- ListDelete(&L);
- */
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement