Single Linked List

Beberapa program di bawah ini menggunakan dev c++ dengan menggunakan linked list. Semoga membantu :)

========================================================================
PROGRAM SISIP AWAL
/*sisip awal*/


#include<stdio.h>
#include<conio.h>
#include<malloc.h>

typedef struct simpul
{  int data;
   struct simpul *next;
   } node, *nodeptr;
   //void inisialisasi(nodeptr s) ->list g jadi di inisialisasi

void Inisialisasi(nodeptr *s)//pointer ke nodeptr
{ *s=NULL;
};

nodeptr NodeBaru(int m)
{
nodeptr n;

n=(nodeptr) malloc(sizeof(node));
if(n!=NULL)
  {n->data=m;
  n->next=NULL;
}
return n;
};

void SisipDepan(nodeptr *s,nodeptr p)
{
p->next=*s;
*s=p;
}

main()
{
nodeptr list, baru; //list:yg ditunjuk *s
Inisialisasi (&list); //list menunjuk ke null
baru=NodeBaru(5);
SisipDepan(&list,baru);
baru=NodeBaru(7);
SisipDepan(&list,baru);
baru=NodeBaru(3);
SisipDepan(&list,baru);
printf("nilai dari list adalah %d -> %d -> %d -> NULL \n " , list -> data , list->next->data, list->next->next->data);

return 0;

}

========================================================================
PROGRAM SISIP AKHIR
/*program sisip AKHIR*/

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

typedef struct simpul
{  int data;
   struct simpul *next;
   } node, *nodeptr;
   //void inisialisasi(nodeptr s) ->list g jadi di inisialisasi

void Inisialisasi(nodeptr *s)//pointer ke nodeptr
{ *s=NULL;
};

nodeptr NodeBaru(int m)
{
nodeptr n;
n=(nodeptr) malloc(sizeof(node));
if(n!=NULL)
  {n->data=m;
  n->next=NULL;
}
return n;
};

void SisipDepan(nodeptr *s,nodeptr p)
{
p->next=*s;
*s=p;
}

void SisipAkhir(nodeptr *s,nodeptr p)
{
nodeptr tail;
tail=*s;
while (tail->next!=NULL)
p->next=*s;
*s=p;
}

void CetakList(nodeptr *s)
{
nodeptr temp;
temp=*s;
while (temp->next!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
printf("%d->NULL",temp->data);
}

main()
{
nodeptr list, baru; //list:yg ditunjuk *s
Inisialisasi (&list); //list menunjuk ke null
baru=NodeBaru(5);
SisipDepan(&list,baru);
baru=NodeBaru(7);
SisipDepan(&list,baru);
baru=NodeBaru(3);
SisipDepan(&list,baru);
baru=NodeBaru(20);
SisipAkhir(&list,baru);
CetakList(&list);
//printf("nilai dari list adalah %d -> %d -> %d -> NULL \n " , list -> data , list->next->data, list->next->next->data);
getch();

return 0;

}
========================================================================
PROGRAM HAPUS AWAL
/*operasi hapus simpul awal*/


#include <stdio.h>
#include <conio.h>
#include <malloc.h>

typedef struct simpul{
int data;
struct simpul *next;
}Node, *nodeptr;
void Inisialisasi (nodeptr *s){
*s=NULL;
}

nodeptr NodeBaru(int m){
nodeptr n;
n=(nodeptr) malloc(sizeof(Node));
if(n!=NULL){
n->data=m;
n->next=NULL;
}
return n;
}

void SisipDepan(nodeptr *s, nodeptr p){
p->next=*s;
*s=p;
}

void SisipAkhir(nodeptr *s, nodeptr p)
{
nodeptr tail;
tail=*s;
while (tail->next!=NULL)
tail=tail->next;
tail->next=p;
}

void CetakList (nodeptr *s)
{
nodeptr temp;
temp = *s;
printf("List->");
if (temp==NULL) printf("NULL\n");
else
{
while (temp->next!=NULL)
{
printf ("%d->", temp->data);
temp=temp->next;
}
printf ("%d->NULL\n", temp->data);
}
}

void HapusAwal(nodeptr *s)
{
nodeptr hapus;
hapus=*s;
*s=hapus->next;
free(hapus);
hapus=NULL;
}
int main(){
nodeptr list, baru;
Inisialisasi(&list);
baru=NodeBaru(5);
SisipDepan(&list, baru);
baru=NodeBaru(7);
SisipDepan(&list, baru);
baru=NodeBaru(3);
SisipDepan(&list, baru);
baru=NodeBaru(6);
SisipAkhir(&list, baru);
HapusAwal(&list);
HapusAwal(&list);
HapusAwal(&list);
CetakList (&list);
//printf("Isi dari list adalah %d -> %d -> %d -> %d -> NULL", list->data, list->next->data, list->next->next->data,  list->next->next->next->data);
getch ();
return 0;
}
========================================================================
PROGRAM HAPUS AKHIR
/*operasi hapus simpul akhir*/


#include <stdio.h>
#include <conio.h>
#include <malloc.h>

typedef struct simpul{
int data;
struct simpul *next;
}Node, *nodeptr;
void Inisialisasi (nodeptr *s){
*s=NULL;
}

nodeptr NodeBaru(int m){
nodeptr n;
n=(nodeptr) malloc(sizeof(Node));
if(n!=NULL){
n->data=m;
n->next=NULL;
}
return n;
}

void SisipDepan(nodeptr *s, nodeptr p){
p->next=*s;
*s=p;
}

void SisipAkhir(nodeptr *s, nodeptr p)
{
nodeptr tail;
tail=*s;
while (tail->next!=NULL)
tail=tail->next;
tail->next=p;
}

void CetakList (nodeptr *s)
{
nodeptr temp;
temp = *s;
printf("List->");
if (temp==NULL) printf("NULL\n");
else
{
while (temp->next!=NULL)
{
printf ("%d->", temp->data);
temp=temp->next;
}
printf ("%d->NULL\n", temp->data);
}
}

void HapusAwal(nodeptr *s)
{
nodeptr hapus;
hapus=*s;
*s=hapus->next;
free(hapus);
hapus=NULL;
}

void HapusAkhir(nodeptr *s)
{
nodeptr hapus, before;
before=*s;
while (before->next->next!=NULL)
{
before=before->next;
}
hapus=before->next;
before->next=NULL;
free(hapus);
hapus=NULL;
}
int main(){
nodeptr list, baru;
Inisialisasi(&list);
baru=NodeBaru(5);
SisipDepan(&list, baru);
baru=NodeBaru(7);
SisipDepan(&list, baru);
baru=NodeBaru(3);
SisipDepan(&list, baru);
baru=NodeBaru(6);
SisipAkhir(&list, baru);
HapusAkhir(&list);
CetakList (&list);
//printf("Isi dari list adalah %d -> %d -> %d -> %d -> NULL", list->data, list->next->data, list->next->next->data,  list->next->next->next->data);
getch ();
return 0;
}


Share this:

ABOUT THE AUTHOR

Sekecil apapun ilmu yang kita punya alangkah indahnya bila bermanfaat bagi sesama :)

0 komentar:

Posting Komentar