المساعد الشخصي الرقمي

عرض الإصدار الكامل : Link List


ونة حزن
07-15-2006, 02:40 AM
Link List

كيفية كتابة برنامج لإضافة عناصر إلى السلسلة من بداية السلسلة ومن منتصفها ومن نهايتها وايضا حذف عناصر بنفس الطريقة من البداية ومن المنتصف ومن النهاية...


الكود بدالة main
#ifndef NODE_H
#define NODE_H
#include <iostream>
using std::cout;
class node
{
friend class mylist;
public:
node(int);
node();
private:
int data;
node *next;
};
#endif
//#include "node.h"
node::node(int x)
{
cout<<"\nconstru Node par\n";
data=x;
next=0;
}
node::node()
{
cout<<"\n Node.....\n";
next = 0;
}
#ifndef MYLIST_H
#define MYLIST_H
//#include "node.h"
class mylist
{
public:
mylist();
~mylist();
void insert (int);
void insert_last(char);
bool insert_first(char );
bool insert_sorted(int);
bool remove_first(char &);
bool remove_last(char &);
void print();
private:
node *head;
};
#endif
//#include "mylist.h"
#include <iostream>
using namespace std;
mylist::mylist()
{
cout<<"\nconstructor My list\n";
head =0;
}
mylist::~mylist()
{
cout<<"\nDe sructor\n";
node *p;
while (head != 0)
{
p = head;
head =head->next;
delete p;
}
}
void mylist::insert( int x)
{
node *p= new node;
p->data = x;
p->next = head;
head = p;
}
void mylist::insert_last(char x)
{
node *p= new node(x);
if (head == 0)
{
head =p;
}
else
{
node *q= head;
while( q->next !=0)
{
q= q->next;
}
q->next=p;
}
}
bool mylist::insert_first(char x)
{
if (head != 0)
{
node *p= new node(x);
p->next=head;
head= p;
return true;
}
else
return false;
}
bool mylist::insert_sorted(int x)
{
node *p= new node(x);
if (head == 0)
head =p;
else
if (head->data >=x)
{
p->next=head;
head=p;
}

else
{
node *r=head;
node *q=head;
while ((q->data < x) && (q->next !=0))
{
r=q;
q=q->next;
}
if ((q->data>x) || ( q->next != 0))
{
r->next=p;
p->next=q;
}
else
q->next=p;
}
return true;
}
bool mylist::remove_first(char & x)
{
if (head != 0 )
{
node *p=head;
head=head->next;
x=p->data;
delete p;
return true;
}
else
return false;
}
bool mylist::remove_last(char & x)
{
if (head != 0)
{
node *q=head;
while( q->next!= 0)
{
q=q->next;
}
x=q->next->data;
delete q->next;
return true;
}
else
return false;
}

void mylist::print()
{
node *p = head;
while (p != 0)
{
cout<<p->data;
p = p->next;
}
}

MSHAER
07-15-2006, 02:04 PM
الله يعطيك الف عافيه اختي ^_^

ونة حزن
07-15-2006, 07:25 PM
يعافيك خيوووو * _ ^

الرادار
07-18-2006, 06:00 AM
حركات يامشرفتنا يعطيك العافيه

ونة حزن
07-26-2006, 02:21 AM
يسلمووووووووووو

ممرض محترف
07-26-2006, 09:09 AM
الله يعطيك العافيه خيتوه

ونة حزن
07-26-2006, 05:46 PM
يعافيك خيو