Pertemuan ke III : Data Structure Linked List | C Programming Languange
Dari blog saya yang sebelumnya, kita lebih mengenal tentang Linked List, Perbedaan Linked List dengan Array, dan Jenis-Jenis Linked List yang ada. Nah, Sekarang ayo kita mencoba mempraktekkan membuat sebuah singly linked list di salah satu programming languange yang populer, C Programming Languange.
#include <stdio.h>
#include <stdlib.h>
struct Node{
int val;
struct Node* next;
}*head,*tail,*curr, *temp;
void pushmiddle(int val,int a){
curr = (struct Node*)malloc(sizeof(struct Node));
curr->val = val;
curr->next = NULL;
temp = head;
for(int i = 1; i < a; i++){
temp = temp->next;
}
if(temp->next==NULL)
{
printf("ERROR");
}
curr->next=temp->next;
temp->next=curr;
}
void pushhead(int val){
curr = (struct Node*)malloc(sizeof(struct Node));
curr->val = val;
curr->next = NULL;
if(head == NULL)
{
head = tail = curr;
}
else
{
curr->next=head;
head = curr;
}
}
void pushtail(int val)
{
curr = (struct Node*)malloc(sizeof(struct Node));
curr->val = val;
// curr->next = NULL;
if(head == NULL)
{
head = tail = curr;
tail->next=NULL;
}
else
{
tail->next = curr;
curr->next = NULL;
tail = curr;
}
}
void print()
{
curr = head;
while(curr!=NULL){
printf("%d ", curr->val);
curr = curr->next;
}
}
void poptail(){
curr = head;
while(curr->next!=tail)
{
curr = curr->next;
}
free(tail);
tail = curr;
tail->next=NULL;
}
void pophead(){
curr = head;
curr=curr->next;
free(head);
head = curr;
}
void popmiddle(int a)
{
temp = head;
for(int i = 1; i < a-1; i++){
temp=temp->next;
}
curr = temp->next;
temp->next = curr->next;
free(curr);
}
int main()
{
pushtail(10);
pushtail(20);
pushtail(30);
popmiddle(1);
print();
}
#include <stdio.h>
#include <stdlib.h>
struct Node{
int val;
struct Node* next;
}*head,*tail,*curr, *temp;
void pushmiddle(int val,int a){
curr = (struct Node*)malloc(sizeof(struct Node));
curr->val = val;
curr->next = NULL;
temp = head;
for(int i = 1; i < a; i++){
temp = temp->next;
}
if(temp->next==NULL)
{
printf("ERROR");
}
curr->next=temp->next;
temp->next=curr;
}
void pushhead(int val){
curr = (struct Node*)malloc(sizeof(struct Node));
curr->val = val;
curr->next = NULL;
if(head == NULL)
{
head = tail = curr;
}
else
{
curr->next=head;
head = curr;
}
}
void pushtail(int val)
{
curr = (struct Node*)malloc(sizeof(struct Node));
curr->val = val;
// curr->next = NULL;
if(head == NULL)
{
head = tail = curr;
tail->next=NULL;
}
else
{
tail->next = curr;
curr->next = NULL;
tail = curr;
}
}
void print()
{
curr = head;
while(curr!=NULL){
printf("%d ", curr->val);
curr = curr->next;
}
}
void poptail(){
curr = head;
while(curr->next!=tail)
{
curr = curr->next;
}
free(tail);
tail = curr;
tail->next=NULL;
}
void pophead(){
curr = head;
curr=curr->next;
free(head);
head = curr;
}
void popmiddle(int a)
{
temp = head;
for(int i = 1; i < a-1; i++){
temp=temp->next;
}
curr = temp->next;
temp->next = curr->next;
free(curr);
}
int main()
{
pushtail(10);
pushtail(20);
pushtail(30);
popmiddle(1);
print();
}
Komentar
Posting Komentar