You are on page 1of 5

STRUKTUR DATA 1

NAMA : URRATUL AQYUNI


NIM : 24010316120041
1. Prints the elements of a linked list
void Print(Node *head)
{ Node *bantu;
bantu=head;
if (head==NULL) {
}
else {
while(bantu!=NULL){
cout<<bantu->data<<endl;
bantu=bantu->next;
}
}
}

2. Insert a node at the head of a linked list


Node* Insert(Node *head,int data)
{
Node *baru;
baru = new Node;
baru->data = data;
baru->next = NULL;

if (head==NULL){
head=baru;
head->next = NULL;
return head;
}else{
baru->next=head;
head=baru;
return head;
}
}

3. Insert a node at the tail of a linked list


Node* Insert(Node *head,int data)
{
Node *baru,*bantu;
baru = new Node;
baru->data = data;
baru->next = NULL;

if (head==NULL){
head=baru;
head->next=NULL;
return head;
}else{
bantu=head;
while(bantu->next!=NULL){
bantu=bantu->next;
}
bantu->next=baru;
return head;
}
}

4. Insert a node at specific position in a linked list


Node* InsertNth(Node *head, int data, int position)
{
Node *baru = new Node;
baru -> data = data;

if (head == NULL) {
return baru;
}
else {
Node *akhir = head;
int p = 0;

if (position == 0) {
baru -> next = head;
return baru;
}

while (akhir != NULL) {


if (p == position - 1) {
baru -> next = akhir -> next;
akhir -> next = baru;
} else {
akhir = akhir -> next;
}
p = p + 1;
}
return head;
}
}

5. Delete a node
Node* Delete(Node *head, int position)
{
Node *bantu, *hapus;
bantu=head;
if(head->next==NULL){
hapus=head;
head=NULL;
delete hapus;
return head;
}else{
if (position==0){
hapus=head;
head=head->next;
delete hapus;
return head;
}else{
while(position>0){
hapus=bantu;
bantu=bantu->next;
position--;
}
hapus->next=bantu->next;
delete bantu;
return head;
}
}
}

6. Print in Reverse
void ReversePrint(Node *head)
{

if (head == NULL)
return;

//else
ReversePrint(head->next);

cout<<head->data<<endl;
}

7. Reverse a linked list


Node* Reverse(Node *head)
{ Node *remaining;
if (head == NULL || head->next == NULL) {
return head;
}
remaining = Reverse(head->next);
head->next->next = head;
head->next = NULL;
return remaining;
}

8. Compare two linked list


int CompareLists(Node *headA, Node* headB)
{

if (headA == NULL && headB == NULL) {


return 1;
}else
if (headA == NULL || headB == NULL) {
return 0;}
if (headA->data == headB->data) {
return CompareLists(headA->next, headB->next);
} else {
return 0;
}}

9. Inserting a Node into a sorted doubly linked list


Node* SortedInsert(Node *head,int data)
{
{
Node *bantu = head;
Node *baru = new Node();
baru->data = data;
baru->next = NULL;
baru->prev = NULL;

if(bantu == NULL){
bantu = baru;
head = bantu;
return head;
}
while((baru->data > bantu->data) && bantu->next != NULL) {
bantu = bantu->next;
}

if(bantu->next == NULL && baru->data > bantu->data ){


baru->prev = bantu;
bantu->next = baru;
return head;
}
baru->prev = bantu->prev;
bantu->prev = baru;
baru->next = bantu;

if(baru->prev != NULL) {
baru->prev->next = baru;
}
if(baru->prev == NULL){
bantu = baru;
return bantu;
}
return head;
}
}

10. Reverse a doubly linked list


Node* Reverse(Node* head)
{
if(head==NULL){
return head;
}
else if(head->next==NULL){
head->next=head->prev;
head->prev=NULL;
return head;
}
Node *prevPointer=head->prev;
Node *nextPointer=head->next;
head->next=prevPointer;
head->prev=nextPointer;
return Reverse(nextPointer);
}

You might also like