Struktur Data : Doubly Linked List

NAMA : Riyn Winesdyo W.
NIM : 1701292866
KELAS : 32PPT

#include <stdio.h>
#include <stdlib.h>

struct tnode{
	int data;
	struct tnode *next;
	struct tnode *prev;
}*node;

void print(){
	if(node!=0){
		while(node->next!=0){
			printf("%d ",node->data);
			node=node->next;
			}printf("%d",node->data);
	}

	printf("\n");
}

int main(){
	struct tnode *head = 0;
	struct tnode *tail = 0;
	struct tnode *newNode=0;
	//1
	head=(struct tnode*)malloc(sizeof(struct tnode));
	head->data=1;
	head->next=NULL;
	head->prev=NULL;
	node=head;
	tail=head;
	//3
	node->next=(struct tnode*)malloc(sizeof(struct tnode));
	node=node->next;
	node->data=3;
	node->next=NULL;
	node->prev=head;
	tail=node;

	node=head;
	print();
	//add number 2 between 1 and 3
	newNode=(struct tnode*)malloc(sizeof(struct tnode));
	node=newNode;
	newNode->data=2;
	node->prev=head;
	node->next=tail;
	head->next=node;
	tail->prev=node;
	

	node=head;
	print();
	//add 0 before 1
	newNode=(struct tnode*)malloc(sizeof(struct tnode));
	node=head;
	head=newNode;
	head->data=0;
	head->prev=NULL;
	head->next=node;
	
	node=head;
	print();
	//add 5 after 3
	newNode=(struct tnode*)malloc(sizeof(struct tnode));
	node=tail;
	tail=newNode;
	tail->data=5;
	tail->prev=node;
	tail->next=NULL;
	node->next=tail;
	
	node=head;
	print();

	getchar();
	return 0;
}

Leave a Reply