السلام عليكم ورحمه الله وبركآته
آخوتي ارجو مسآعدتي
أريد كتآبه كود بلغه c++ عن linked List
آنآ كتبت الكود كآمل
بس عندي مشكله بسطيه ابغى مسآعدتكم
وهي المطلوب من السؤال بحث عن قيمه , ثم اضافه node جديده بالمنتصف تحمل قيمه 99
لانني لآ آعرف كيف اضيف node جديده بالمنتصف
وهذا الكود
#include "stdafx.h"
#include <iostream>
using namespace std;
//------ class IntNode for creating new node ----------------//
class IntNode {
public :
IntNode(int el, IntNode *ptr = 0) {info = el; next = ptr;}
int info;
IntNode *next;
};
//------ class IntLList for dealing with nodes --------------//
class IntLList {
public:
IntLList() {head = tail =0; }
void AddToHead(int);
void AddToTail(int);
void DeleteFromHead();
void DeleteFromTail();
void DeleteNode(int);
void FindNode(int);
void addnewNode(int);
void DisplayList();
private:
IntNode *head, *tail;
};
void IntLList::AddToHead(int data)
{
IntNode *newnode;
newnode = new IntNode(data,0);
newnode->next = head;
head = newnode;
if (tail==0)
tail = head;
}
void IntLList::AddToTail(int data)
{
IntNode *newnode;
newnode = new IntNode(data,0);
tail->next = newnode;
tail = newnode;
}
void IntLList::DeleteFromHead()
{
if(head!=0)
head = head->next;
if(head==0)
tail=head;
}
void IntLList::DeleteFromTail()
{
IntNode *current;
current = head;
while(current->next != tail)
{
current=current->next;
}
tail=current;
tail->next=0;
}
void IntLList::FindNode(int val)
{
IntNode *current;
current = head;
while(current != tail->next)
{
if (current->info == val)
cout << current->info << " " << current << "\n";
current=current->next;
}
cout << "----------------------" << "\n";
}
void IntLList::addnewNode(int val)
{
?????????
}
void IntLList::DisplayList()
{
IntNode *current;
current = head;
cout << "head = " << head << "\n";
while(current != tail->next)
{
cout << current->info << " " << current << "\n";
current=current->next;
}
cout << "tail = " << tail << " " << tail->next << "\n";
cout << "----------------------" << "\n";
}
void main()
{
IntLList mag;
mag.AddToHead(50);
mag.AddToHead(90);
mag.AddToHead(60);
mag.DisplayList();
mag.DeleteFromHead();
mag.DisplayList();
mag.AddToTail(88);
mag.AddToTail(77);
mag.AddToHead(66);
mag.AddToHead(50);
mag.DisplayList();
mag.DeleteFromTail();
mag.DisplayList();
mag.FindNode(50);
mag.addnewNode(50);
mag.DisplayList();
}