You are on page 1of 12

Nama : Putra Ramadandi

NIM : 121130048

Kelas : RD

Mata Kuliah : PMC

Jawaban:

1. Nama Lengkap (Putra Ramadandi)


#include <stdio.h>

#include <stdlib.h>

struct Node

char huruf;

struct Node *next;

} * head, *tail, *newNode, *temp, *temp2, *del;

void tambah(char huruf);

void cetak();

int main()

head = tail = NULL;

tambah('P');

tambah('u');

tambah('t');

tambah('r');

tambah('a');

tambah(' ');

tambah('R');

tambah('a');

tambah('m');

tambah('a');

tambah('d');

tambah('a');

tambah('n');

tambah('d');

tambah('i');
printf("\nNama lengkap : ");

cetak();

return 0;

void tambah(char huruf)

newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->huruf = huruf;

newNode->next = NULL;

if (head == NULL)

head = newNode;

tail = newNode;

else

tail->next = newNode;

tail = newNode;

int countNode()

int count = 0;

temp = head;

while (temp != NULL)

temp = temp->next;

count++;

return count;

void cetak()
{

temp = head;

while (temp != NULL)

printf("%c", temp->huruf);

temp = temp->next;

2. NIM (121130048)
#include <stdio.h>
#include <stdlib.h>
struct Node
{
char angka;
struct Node *next;
} * head, *tail, *newNode, *temp, *temp2, *del;
void tambah(char angka);
void cetak();
int main()
{
head = tail = NULL;
tambah('1');
tambah('2');
tambah('1');
tambah('1');
tambah('3');
tambah('0');
tambah('0');
tambah('4');
tambah('8');

printf("\nNIM : ");
cetak();
return 0;
}
void tambah(char angka)
{
newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->angka = angka;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
tail = newNode;
}
else
{
tail->next = newNode;
tail = newNode;
}
}
int countNode()
{
int count = 0;
temp = head;
while (temp != NULL)
{
temp = temp->next;
count++;
}
return count;
}
void cetak()
{
temp = head;
while (temp != NULL)
{
printf("%c", temp->angka);
temp = temp->next;
}
}

3. Delete node yang ke-3 dari nama (Putra Ramadandi= yang ke-3 yaitu huruf “t”)
#include <stdio.h>

#include <stdlib.h>

struct Node

char huruf;
struct Node *next;

} * head, *tail, *newNode, *temp, *temp2, *del;

void tambah(char huruf);

void hapus(int urutan);

void cetak();

int main()

head = tail = NULL;

tambah('P');

tambah('u');

tambah('t');

tambah('r');

tambah('a');

tambah(' ');

tambah('R');

tambah('a');

tambah('m');

tambah('a');

tambah('d');

tambah('a');

tambah('n');

tambah('d');

tambah('i');

printf("\nNama lengkap : ");

cetak();

printf("\nHapus urutan ke-3: ");

hapus(3);

printf("\nNama lengkap : ");

cetak();

return 0;

void tambah(char huruf)

{
newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->huruf = huruf;

newNode->next = NULL;

if (head == NULL)

head = newNode;

tail = newNode;

else

tail->next = newNode;

tail = newNode;

int countNode()

int count = 0;

temp = head;

while (temp != NULL)

temp = temp->next;

count++;

return count;

void hapus(int urutan)

if (head != NULL && urutan > 1 && urutan < countNode())

int count = 1;

temp = head;

temp2 = head;

while (count <= urutan)

{
if (count == urutan)

del = temp;

if (count == urutan - 1)

temp2 = temp;

temp = temp->next;

count++;

temp2->next = temp;

free(del);

void cetak()

temp = head;

while (temp != NULL)

printf("%c", temp->huruf);

temp = temp->next;

}
4. Delete node yang ke-4 dari NIM (121130048 = yang ke-4 yaitu angka “1”)
#include <stdio.h>

#include <stdlib.h>

struct Node

char angka;

struct Node *next;

} * head, *tail, *newNode, *temp, *temp2, *del;

void tambah(char angka);

void hapus(int urutan);

void cetak();

int main()

head = tail = NULL;

tambah('1');

tambah('2');

tambah('1');

tambah('1');

tambah('3');

tambah('0');

tambah('0');

tambah('2');

tambah('8');

printf("\nNIM : ");

cetak();

printf("\nHapus urutan ke-4: ");

hapus(4);

printf("\nNIM : ");

cetak();

return 0;
}

void tambah(char angka)

newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->angka = angka;

newNode->next = NULL;

if (head == NULL)

head = newNode;

tail = newNode;

else

tail->next = newNode;

tail = newNode;

int countNode()

int count = 0;

temp = head;

while (temp != NULL)

temp = temp->next;

count++;

return count;

void hapus(int urutan)

if (head != NULL && urutan > 1 && urutan < countNode())

int count = 1;

temp = head;
temp2 = head;

while (count <= urutan)

if (count == urutan)

del = temp;

if (count == urutan - 1)

temp2 = temp;

temp = temp->next;

count++;

temp2->next = temp;

free(del);

void cetak()

temp = head;

while (temp != NULL)

printf("%c", temp->angka);

temp = temp->next;

You might also like