Tugas Double Linked List
March20
#include<stdio.h> #include<stdlib.h> #pragma warning(disable: 4996) typedef struct tnode{ struct tnode *next; struct tnode *prev; int x; }; struct tnode *head, *connector, *temp; int main(){ // fungsi untuk mencetak angka 1 dan 3 dalam bentuk double linked list head = (struct tnode*)malloc(sizeof(struct tnode)); head->next = NULL; head->prev = NULL; head->x = 1; connector = head; connector->next = (struct tnode*)malloc(sizeof(struct tnode)); connector = connector->next; connector->next = NULL; connector->x = 3; connector->prev = head->next; connector = head; if(connector != NULL){ while(connector->next != NULL){ printf("%d ", connector->x); connector = connector->next; } printf("%d", connector->x); } // fungsi untuk menambahkan angka 2 diantara 1 dan 3 connector = head; if(connector != NULL){ while(connector->next != NULL){ if(connector->x == 1 && connector->next->x == 3){ temp = (struct tnode*)malloc(sizeof(struct tnode)); temp->x = 2; temp->next = connector->next; temp->prev = head->next; connector->next = temp; } connector = connector->next; } } printf("\n\n"); connector = head; if(connector != NULL){ while(connector->next != NULL){ printf("%d ", connector->x); connector = connector->next; } printf("%d", connector->x); } // fungsi untuk menambahkan angka 0 didepan liat data temp = (struct tnode*)malloc(sizeof(struct tnode)); temp->next = head; temp->x = 0; temp->prev = NULL; head = temp; printf("\n\n"); connector = head; if(connector != NULL){ while(connector->next != NULL){ printf("%d ", connector->x); connector = connector->next; } printf("%d", connector->x); } // fungsi untuk mencetak angka 5 di paling akhir connector->next = (struct tnode*)malloc(sizeof(struct tnode)); connector = connector->next; connector->next = NULL; connector->x = 5; connector->prev = head->next->next->next->next->next; printf("\n\n"); connector = head; if(connector != NULL){ while(connector->next != NULL){ printf("%d ", connector->x); connector = connector->next; } printf("%d", connector->x); } printf("\n\n\n"); printf("Created by Alvin Okavianus\n"); printf("1701293004\n"); printf("32PPT\n\n"); system("PAUSE"); return 0; }