PROGRAM LINKED LIST
PROGRAM 1
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
using namespace std;
typedef struct nod
{
int data;
struct nod *next;
}NOD, *NODPTR;
void CiptaSenarai(NODPTR *s)
{
*s = NULL;
}
NODPTR NodBaru(int m)
{
NODPTR n;
n = (NODPTR)malloc(sizeof(NOD));
if (n != NULL)
{
n->data = m;
n->next = NULL;
}
return n;
}
void SisipSenarai(NODPTR *s, NODPTR t, NODPTR p)
{
if (p == NULL)
{
t->next = *s;
*s = t;
}
else
{
t->next = p->next;
p->next = t;
}
}
void CetakSenarai(NODPTR s)
{
NODPTR ps;
for (ps = s; ps != NULL; ps = ps->next)
printf("%d --> ", ps->data);
printf("NULL\n");
}
int main()
{
NODPTR pel;
NODPTR n;
int i, k, nilai;
CiptaSenarai(&pel);
printf("Masukkan Banyak Data : ");
scanf_s("%d", &k);
for (i = 1; i <= k; i++)
{
printf("Masukkan Banyak Data ke - %d : ", i);
scanf_s("%d", &nilai);
n = NodBaru(nilai);
SisipSenarai(&pel, n, NULL);
}
CetakSenarai(pel);
_getch();
}
PROGRAM 2
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class Simpul
{
public:
string kode;
string nama;
Simpul* berikut;
// Konstruktor
Simpul(string xkode, string xnama)
{
kode = xkode;
nama = xnama;
berikut = NULL;
}
void display()
{
cout << kode << " : " << nama << endl;
}
};
class SenaraiBerantai
{
private:
Simpul* pertama;
public:
// Konstruktor
SenaraiBerantai()
{
pertama = NULL;
}
// Destruktor
~SenaraiBerantai()
{
// Menghapus semua simpul
Simpul* penunjukHapus;
while (pertama != NULL)
{
penunjukHapus = pertama;
pertama = pertama->berikut;
delete penunjukHapus;
}
}
//Memasukkan data ke linked list
//nilai balik tidak ada
void insert(string kode, string nama)
{
Simpul* ptrBaru;
ptrBaru = new Simpul(kode, nama);
ptrBaru->berikut = pertama;
pertama = ptrBaru;
}
// Mencari data
//Nilai balik berupa true kalau yang dicari ketemu
// Dalam hal ini, pendahulu merupakan simpul yang
// terletak di depan simpul yang dicari
bool find(Simpul* &pendahulu, Simpul* &posisiData, string x)
{
posisiData = pertama;
pendahulu = NULL;
bool ditemukan = false;
while (posisiData != NULL)
{
string kode = posisiData->kode;
if (kode.compare(x) == 0)
{
ditemukan = true;
break; // Keluar dari while
}
pendahulu = posisiData;
posisiData = posisiData->berikut;
}
return ditemukan;
}
//Mencari data
//Nilai balik berupa NULL kalau yang dicari tidak ketemu
Simpul* find(string x)
{
Simpul* pendahulu;
Simpul* posisiData;
bool ditemukan = find(pendahulu, posisiData, x);
if (!ditemukan)
return NULL;
else
return posisiData;
}
//Menghapus simpul yang berisi kode x
//Nilai balik berupa :
//-true kalau data berhasil dihapus
//-false kalau data tidak ada
bool remove(string x)
{
Simpul* pendahulu;
Simpul* posisiData;
bool ditemukan = find(pendahulu, posisiData, x);
if (!ditemukan)
{
cout << "data yang akan dihapus tidak ditemukan."
<< endl;
return false;
}
if (pendahulu == NULL)
{
// Data yang dihapus ditunjuk oleh pertama
pertama = pertama->berikut;
delete posisiData;
}
else
{
// Data yang dihapus tidak ditunjuk oleh pertama
pendahulu->berikut = posisiData->berikut;
delete posisiData;
}
}
//Menampilkan data
void display(void)
{
cout << "Isi senarai berantai : " << endl;
Simpul* penunjuk = pertama;
while (penunjuk != NULL)
{
penunjuk->display();
penunjuk = penunjuk->berikut;
}
}
};
int main()
{
SenaraiBerantai senarai;
// Masukkan 5 buah nama
senarai.insert("AMN", "Aminudin");
senarai.insert("ZAS", "Zaskia");
senarai.insert("RIN", "Rina Melati");
senarai.insert("FAR", "Farhan");
senarai.insert("AGN", "Agnes Monica");
cout << "Keadaan awal:" << endl;
senarai.display();
senarai.remove("RIN");
cout << endl;
cout << "Setelah RIN dihapus:" << endl;
senarai.display();
// Cari RIN
string dicari = "RIN";
cout << endl;
cout << "Pencarian " << dicari << endl;
Simpul* posisiData = senarai.find(dicari);
if (posisiData == NULL)
cout << dicari << " tidak ditemukan." << endl;
else
{
cout << "Hasil pencarian: " << endl;
posisiData->display();
}
// Cari FAR
dicari = "FAR";
cout << endl;
cout << "Pencarian " << dicari << endl;
posisiData = senarai.find(dicari);
if (posisiData == NULL)
cout << dicari << "tidak ditemukan." << endl;
else
{
cout << "Hasil pencarian : " << endl;
posisiData->display();
}
_getch();
}
PROGRAM 3
// ProgGraph4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include <conio.h>
#include<cstdlib>
using namespace std;
// Data structure to store adjacency list nodes
struct Node
{
int val;
Node* next;
};
// Data structure to store a graph edge
struct Edge {
int src, dest;
};
class Graph
{
// Function to allocate a new node for the adjacency list
Node* getAdjListNode(int dest, Node* head)
{
Node* newNode = new Node;
newNode->val = dest;
// point new node to the current head
newNode->next = head;
return newNode;
}
int N; // total number of nodes in the graph
public:
// An array of pointers to Node to represent the
// adjacency list
Node **head;
// Constructor
Graph(Edge edges[], int n, int N)
{
// allocate memory
head = new Node*[N]();
this->N = N;
// initialize head pointer for all vertices
for (int i = 0; i < N; i++) {
head[i] = nullptr;
}
// add edges to the directed graph
for (unsigned i = 0; i < n; i++)
{
int src = edges[i].src;
int dest = edges[i].dest;
// insert at the beginning
Node* newNode = getAdjListNode(dest, head[src]);
// point head pointer to the new node
head[src] = newNode;
// uncomment the following code for undirected graph
/*
newNode = getAdjListNode(src, head[dest]);
// change head pointer to point to the new node
head[dest] = newNode;
*/
}
}
// Destructor
~Graph() {
for (int i = 0; i < N; i++) {
delete[] head[i];
}
delete[] head;
}
};
// Function to print all neighboring vertices of a given vertex
void printList(Node* ptr)
{
while (ptr != nullptr)
{
cout << " -> " << ptr->val << " ";
ptr = ptr->next;
}
cout << endl;
}
// Graph implementation in C++ without using STL
int main()
{
// an array of graph edges as per the above diagram
Edge edges[] =
{
// pair `(x, y)` represents an edge from `x` to `y`
{ 0, 1 }, { 1, 2 }, { 2, 0 }, { 2, 1 },
{ 3, 2 }, { 4, 5 }, { 5, 4 }
};
// total number of nodes in the graph
int N = 6;
// calculate the total number of edges
int n = sizeof(edges) / sizeof(edges[0]);
// construct graph
Graph graph(edges, n, N);
// print adjacency list representation of a graph
for (int i = 0; i < N; i++)
{
// print given vertex
cout << i << " - ";
// print all its neighboring vertices
printList(graph.head[i]);
}
_getch();
}