پیاده سازی لیست پیوندی یک طرفه - با قابلیت افزودن و زدودن و جستجوی گره و فرمت لیست با ساختار
#include "stdafx.h"#include <iostream>
#include <conio.h>
using namespace std;
struct node
{
int data;
char name [30];
char family[30];
char number[30];
node *next;

};
node *first;
node *last;
void add(int);
void del(int);
void show();
void format();
void searchlist(int);
int _tmain(int argc, _TCHAR* argv[])
{
int ans;
int a;
int d;
while(1)
{
cout<<"\n 1-add 2-del 3-show 4-format list 5-searchlist (Enter |6| key to Exit) : ";
cin>>ans;
if(ans==1)
{

cout<<"\nEnter Number for add : ";
cin>>a;
add(a);
}
else if(ans==2)
{
cout<<"\nEnter Number for del : ";
cin>>d;
del(d);
}
else if (ans==3)
{
show();
}
else if (ans==4)
{
format();
cout<<"END Life this linked list ";
_getch();
break;
}
else if(ans==5)
{
int l;
cout<<"\nEnter Number for search : ";
cin>>l;
searchlist(l);
}
else
{
break;
}
}
_getch();
return 0;
}


void add(int x)
{
if(first==NULL)
{
node *temp=new node;
first=temp;
first->data=x;
cout<<"\nEnter Name : ";
cin>>first->name;
first->next=NULL;
last=first;
}
else
{
node *temp=new node;
last->next=temp;
last=temp;
last->data=x;
cout<<"\nEnter Name : ";
cin>>last->name;
last->next=NULL;
}
}
void del(int y)
{
if(y==first->data)
{
node *temp;
temp=first->next;
delete first;
first=temp;
}
else if(y==last->data)
{
node *temp,*temp1;
temp=temp1=first;
while(1)
{
temp1=temp;
temp=temp->next;
if(temp==last)
{
delete last;
last=temp1;
last->next=NULL;
break;
}
}


}
else
{
node *temp,*temp1;
temp=temp1=first;
while(1)
{
temp1=temp;
temp=temp->next;
if(temp->data==y)
{
temp1->next=temp->next;
delete temp;
break;
}
}
}

}
void show()
{
node *n = first;
while ( n ) {
cout << n->data <<" "<<n->name<< "\n";
n = n->next;
}
cout << '\n';

}


void format()
{
node *n = first;
node *e = first;
while ( n ) {
e=n;
n = n->next;
delete e;
}
}
void searchlist(int l)
{
node *n = first;
while ( 1 ) {

if (n->data==l)
{
cout << n->data <<" "<<n->name<< "\n";
break;
}
else
{
n = n->next;
}
}
}