Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- /* Struktur Node */
- typedef struct dnode_t {
- int data;
- struct dnode_t \
- *next,
- *prev;
- } DListNode;
- /* Struktur ADT List */
- typedef struct dlist_t {
- DListNode \
- *_head,
- *_tail;
- unsigned _size;
- } List;
- /* DAFTAR FUNGSI YANG TERSEDIA */
- DListNode* __dlist_createNode(int value);
- void dlist_init(List *list);
- bool dlist_isEmpty(List *list);
- void dlist_pushFront(List *list, int value);
- void dlist_pushBack(List *list, int value);
- void dlist_insertAt(List *list, int value);
- void dlist_popFront(List *list);
- void dlist_popBack(List *list);
- void dlist_remove(List *list, int element);
- void dlist_removeAt(List *list, int index);
- /* Function definition */
- DListNode* __dlist_createNode(int value)
- {
- DListNode *newNode = \
- (DListNode*) malloc (sizeof(DListNode));
- if (!newNode) return NULL;
- newNode->data = value;
- newNode->next = NULL;
- newNode->prev = NULL;
- return (DListNode*) newNode;
- }
- void dlist_init(List *list)
- {
- list->_head = list->_tail = NULL;
- list->_size = (unsigned) 0;
- }
- bool dlist_isEmpty(List *list) {
- return (list->_head == NULL && \
- list->_tail == NULL);
- }
- void dlist_pushFront(List *list, int value)
- {
- DListNode *newNode = __dlist_createNode(value);
- if (newNode) {
- list->_size++;
- if (dlist_isEmpty(list)) {
- list->_head = newNode;
- list->_tail = newNode;
- return;
- }
- newNode->next = list->_head;
- list->_head->prev = newNode;
- list->_head = newNode;
- }
- }
- void dlist_pushBack(List *list, int value)
- {
- DListNode *newNode = __dlist_createNode(value);
- if (newNode) {
- list->_size++;
- if (dlist_isEmpty(list)) {
- list->_head = newNode;
- list->_tail = newNode;
- return;
- }
- list->_tail->next = newNode;
- newNode->prev = list->_tail;
- list->_tail = newNode;
- }
- }
- void dlist_insertAt(List *list, int value)
- {
- if(list->_tail==NULL)
- {
- dlist_init(list);
- dlist_pushBack(list, value);
- return;
- }
- if (value < list->_head->data ) {
- dlist_pushFront(list, value);
- return;
- }
- else if (value >= list->_tail->data|| list->_head==NULL) {
- dlist_pushBack(list, value);
- return;
- }
- DListNode *newNode = __dlist_createNode(value);
- if (newNode) {
- if (dlist_isEmpty(list)) {
- list->_head = newNode;
- list->_tail = newNode;
- return;
- }
- DListNode *temp = list->_head;
- DListNode *temp1 = list->_head;
- unsigned _i = 1;
- while (temp->data < value && temp->next != NULL) {
- _i++;
- temp = temp->next;
- }
- // printf("%d%d",_i,list->_size);
- if(_i == list->_size)
- {
- newNode->next == NULL;
- newNode->prev= temp;
- temp->next= newNode;
- list->_tail->next= newNode;
- newNode->prev = list->_tail;
- list->_tail=newNode;
- //list->_head=newNode;
- //printf("%d",temp->next->data);
- } else if(_i==1){
- dlist_pushFront(list, value);
- return;
- }
- else{
- newNode->next = temp;
- newNode->prev = temp->prev;
- temp->prev->next=newNode;
- list->_size++;
- }
- }
- }
- void dlist_popFront(List *list)
- {
- if (!dlist_isEmpty(list)) {
- DListNode *temp = list->_head;
- if (list->_head == list->_tail) {
- list->_head = NULL;
- list->_tail = NULL;
- free(temp);
- }
- else {
- list->_head = list->_head->next;
- list->_head->prev = NULL;
- free(temp);
- }
- list->_size--;
- }
- }
- void dlist_popBack(List *list)
- {
- if (!dlist_isEmpty(list)) {
- DListNode *temp;
- if (list->_head == list->_tail) {
- temp = list->_head;
- list->_head = NULL;
- list->_tail = NULL;
- free(temp);
- }
- else {
- temp = list->_tail;
- list->_tail = list->_tail->prev;
- list->_tail->next = NULL;
- free(temp);
- }
- list->_size--;
- }
- }
- void printlist(List *list)
- {
- DListNode *temp= list->_head;
- while(temp)
- {
- printf("%d ",temp->data);
- temp=temp->next;
- }
- printf("\n");
- }
- void dlist_removeAt(List *list, int index)
- {
- if(index== 0 )return;
- // if (dlist_isEmpty(list))
- // dlist_init(list);
- /* Kasus apabila posisinya melebihi batas */
- if (index >= list->_size-1 ) {
- //printf("1");
- dlist_popBack(list);
- return;
- }
- else if (index == 1 || index < 0) {
- dlist_popFront(list);
- //printf("2");
- return;
- }
- int _i = 0;
- if(index < list->_size/2)
- {
- DListNode *tempH = list->_head;
- while (tempH->next != NULL && _i < index) {
- tempH = tempH->next;
- _i++;
- }
- DListNode *nextTo = tempH->next->next;
- free(tempH->next);
- nextTo->prev=tempH;
- tempH->next = nextTo;
- list->_size--;
- } else
- {
- _i=list->_size;
- DListNode *tempT = list->_tail;
- while (tempT->prev != NULL && _i >= index) {
- tempT = tempT->prev;
- _i--;
- }
- DListNode *nextTo = tempT->next->next;
- free(tempT->next);
- nextTo->prev=tempT;
- tempT->next = nextTo;
- list->_size--;
- }
- }
- void dlist_remove(List *list, int value)
- {
- if (!dlist_isEmpty(list)) {
- DListNode *temp, *prev;
- temp = list->_head;
- if (temp->data == value) {
- dlist_popFront(list);
- return;
- }
- while (temp != NULL && temp->data != value) {
- prev = temp;
- temp = temp->next;
- }
- if (temp == NULL) return;
- prev->next = temp->next;
- free(temp);
- list->_size--;
- }
- }
- int main(int argc, char const *argv[])
- {
- char com[8];
- int n=0;
- // Buat objek List
- List myList;
- // PENTING! Jangan lupa diinisialisasi
- dlist_init(&myList);
- // Gunakan operasi linked list
- // untuk mengisi data linked list
- // dlist_pushBack(&myList, 10);
- // dlist_pushBack(&myList, 22);
- // dlist_pushBack(&myList, 41);
- // dlist_pushBack(&myList, 59);
- printf("DATA DALAM LINKED LIST:\n");
- printlist(&myList);
- printf("COMANND (ins / del / deLNode), 0 untuk berhenti:\n");
- scanf("%s",com);
- while(strcmp(com,"0"))
- {
- if(!strcmp(com,"ins"))
- { printf("Input number: ");
- scanf("%d",&n);
- dlist_insertAt(&myList, n);
- printf("DATA DALAM LINKED LIST:\n");
- printlist(&myList);
- //printf("%d*",myList._size);
- printf("COMANND (ins / del / deNode), 0 untuk berhenti:\n");
- scanf("%s",com);
- }
- else if(!strcmp(com,"delNode"))
- {
- printf("Input loc. Number (start form 1): ");
- scanf("%d",&n);
- dlist_removeAt(&myList, n);
- printf("DATA DALAM LINKED LIST:\n");
- printlist(&myList);
- //("%d*",myList._size);
- printf("COMANND (ins / del / deLNode), 0 untuk berhenti:\n");
- scanf("%s",com);
- }else if(!strcmp(com,"del"))
- {
- printf("Input Number: ");
- scanf("%d",&n);
- dlist_remove(&myList, n);
- printf("DATA DALAM LINKED LIST:\n");
- printlist(&myList);
- //("%d*",myList._size);
- printf("COMANND (ins / del / deLNode), 0 untuk berhenti:\n");
- scanf("%s",com);
- }
- else
- {
- printf("WRONG COMMAND\nCOMANND (ins / del / deLNode), 0 untuk berhenti:\n");
- scanf("%s",com);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement