Another Example about Insert and Delete Linked List


Below is another example about linked list implementation using C++ language. In this source code there are three basic operation described, those are insertion, deletion and show the nodes inside the linked list. You are recommended to modify some unpractical part of the code, one of it is insertion part. The insertion part will be better if you use an iteration to make it more efficient. Thanks for visitingđŸ™‚

#include “iostream.h”
#include “string.h”
struct node{
char name [20];
int distance; //data
struct node *next;//pointer to point to the next node
};
typedef node * nodeptr;
void insert(nodeptr &head, int data, char nama []){
nodeptr temp;
temp=new node;
temp->distance=data;
strcpy(temp->name,nama);
temp->next=NULL;
if (head==NULL)
head=temp;
else{
temp->next=head;
head=temp;
}
};
char * delnode(nodeptr &head){

nodeptr temp;
char hold[20];

if (head!=NULL) {
temp=head;
strcpy(hold,temp->name);
head=temp->next;
delete temp;
return hold;}
else{
cout<<"Link list is empty"<<endl; return(0);}
}

void main()
{ nodeptr head=NULL, ptr;
int jarak;
char kota [20];
insert(head, 15, "surabaya");
insert(head, 12, "mojokerto");
insert(head, 23, "lamongan");
cout<>kota;
cout<>jarak;
insert(head,jarak,kota);
//show the begining
for(ptr=head; ptr!=NULL; ptr=ptr->next)
cout<distance<<" "<name<<endl;
//delete the front node
strcpy(kota,delnode(head));
cout<<"deleted = " <<kota<next)
cout<distance<<" "<name<<endl;
}

2 Responses to Another Example about Insert and Delete Linked List

  1. inka says:

    pak bisa minta tolong source code untuk :

    Diketahui 3 data yaitu 15, 17, 30. Bagaimana cara menghapus data 15 daro depan pada single linked list Head – Tail..

    terimah kasih..

    • bluejundi says:

      kurang lebih spt di bawah ini:

      temp = head;
      head = head.next; //geser head ke data selanjutnya dulu (17) sebelum data 15 dihapus.
      info = temp.data;
      delete(temp);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: