Simple Single Linked List without delete Algoritm in C
#include <stdio.h>
#include <stdlib.h>
typedef struct element
{
int data;
struct element * next;
}element;
element * createdelement(int data)
{
element * buatelement = NULL;
buatelement = (element*)malloc(sizeof(element));
buatelement->data = data;
buatelement->next = NULL;
}
void adddepan(int data, element ** g)
{
element * newlist = createdelement(data);
newlist->next = (*g);
(*g) = newlist;
}
void addtengah(int data,int cari, element * g)
{
element * newlist = createdelement(data);
element * temp = NULL;
temp = g;
g = newlist;
while(temp->data != cari)
{
temp = temp->next;
}
newlist->next = temp->next;
temp->next = newlist;
}
void addbelakang(int data,element * g)
{
element * newlist = createdelement(data);
element * temp = NULL;
temp = g;
while(temp->next != NULL)
{
temp = temp->next;
}
newlist->next = temp->next;
temp->next = newlist;
}
void print(element * g)
{
element * temp = g;
do
{
printf("%d",temp->data);
temp = temp->next;
}while(temp != NULL);
}
int main()
{
element * g;
g = createdelement(6);
adddepan(10,&g);
addtengah(11,6,g);
addbelakang(9,g);
print(g);
return 0;
}
Komentar
Posting Komentar