موقع طلبة جامعة البحرين

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

تسجيل دخول
  • قسم المكتبة الجامعية مغلق مؤقتا بعد الاستهداف التي تعرضت له بعض خدمات أمازون في البحرين، جاري استرجاع الملفات ونقلها إلى سيرفرات جديدة وسيتم بعدها اعادة افتتاح المكتبة

ITCS215 للفصل الدراسي الأول 2015 - 2016

مشاهدة المرفق 3544
هذا ADTS وهذا السؤوال
1. Define a binary search tree object bst of type bSearchTreeType.
2. Insert the following integers into bst: 100,40, 20, 15, 30, 40, 35, and 77.
3. Display the height of bst.
4. Display the number of nodes of bst.
5. Display the number of leaves of bst.
6. Display the nodes in Preorder, Inorder, and Postorder traversals.
7. Delete the nodes with the content 30 and 35.
8. Display the number of nodes of bst.
مثل ما ذكرت سابقاً ال binary search tree انتهتنز ال binary tree فالمتغير الي عرفناه في المين bst من نوع binary search tree يقدر يستخدم فنكشنات ال binary tree و يقدر يستخدم فنكشنات ال binary search tree انزين نجي لفنكشنات كل وحدة منهم :

فنكشنات ال Binary Tree
كود:
 const binaryTreeType<elemType>& operator=
                 (const binaryTreeType<elemType>&); 
    bool isEmpty();


    void inorderTraversal();
     
    void preorderTraversal();
   
    void postorderTraversal();


    int treeHeight();
   
    int treeNodeCount();
   
    int treeLeavesCount();


    void destroyTree();


    binaryTreeType(const binaryTreeType<elemType>& otherTree); 


    binaryTreeType();   


    ~binaryTreeType();

فنكشنات ال Binary Search Tree
كود:
  bool search(const elemType& searchItem);


    void insert(const elemType& insertItem);
   
    void deleteNode(const elemType& deleteItem);

نجي للأسايمنت :
بارت ون نعرف متغير اسمه bst من نوع binary search tree و بكون التمبلت ماله أنتجر لان الي بندخله عبارة عن ارقام
كود:
int main(){
[COLOR=#ff0000]	// Define a binary search tree object bst of type bSearchTreeType[/COLOR]
[COLOR=#008000]	bSearchTreeType<[/COLOR][COLOR=#0000cd]int[/COLOR][COLOR=#008000]> bst; [/COLOR]
	system("pause");
	return 0;
}
بارت تو : نسوي انسيرت ل 8 ارقام الي عطانا اياها بواسطة فنكشن insert الموجودة عندنا في الباينري سيرج تري :
كود:
int main(){
[COLOR=#ff0000]	// Define a binary search tree object bst of type bSearchTreeType[/COLOR]
	[COLOR=#008000]bSearchTreeType<[/COLOR][COLOR=#0000ff]int[/COLOR][COLOR=#008000]> bst; [/COLOR]
	[COLOR=#ff0000]// Insert the following integers into bst: 100,40, 20, 15, 30, 40, 35, and 77.[/COLOR] 
[COLOR=#006400]	bst.insert(100);[/COLOR]
[COLOR=#006400]	bst.insert(40);[/COLOR]
[COLOR=#006400]	bst.insert(20);[/COLOR]
[COLOR=#006400]	bst.insert(15);[/COLOR]
[COLOR=#006400]	bst.insert(30);[/COLOR]
[COLOR=#006400]	bst.insert(40);[/COLOR]
[COLOR=#006400]	bst.insert(35);[/COLOR]
[COLOR=#006400]	bst.insert(75);[/COLOR]


	system("pause");
	return 0;
}
بارت 3 يبغى يعرف ارتفاع التري فبنستخدم فنكشن treeHeight الموجودة في ال Binary tree بس مثل ما نلاحظ ان نوعها int فاذا بنستخدمها بنتحتاج نعرف متغير من نوع int نخزن فيه الارتفاع أو نستخدم cout افضل :
كود:
int main(){
[COLOR=#FF0000]	// Define a binary search tree object bst of type bSearchTreeType[/COLOR]
	[COLOR=#008000]bSearchTreeType<[/COLOR][COLOR=#0000FF]int[/COLOR][COLOR=#008000]> bst; [/COLOR]
	[COLOR=#FF0000]// Insert the following integers into bst: 100,40, 20, 15, 30, 40, 35, and 77.[/COLOR] 
[COLOR=#006400]	bst.insert(100);[/COLOR]
[COLOR=#006400]	bst.insert(40);[/COLOR]
[COLOR=#006400]	bst.insert(20);[/COLOR]
[COLOR=#006400]	bst.insert(15);[/COLOR]
[COLOR=#006400]	bst.insert(30);[/COLOR]
[COLOR=#006400]	bst.insert(40);[/COLOR]
[COLOR=#006400]	bst.insert(35);[/COLOR]
[COLOR=#006400]	bst.insert(75);[/COLOR]
[COLOR=#ff0000]// Display the height of bst. [/COLOR]
[COLOR=#008000]	cout << "The heigth of binary tree is :" <<[/COLOR][COLOR=#0000ff] bst.treeHeigth()[/COLOR][COLOR=#008000] << endl ; [/COLOR]



	system("pause");
	return 0;
}
أتمنى تكون وضحت الفكرة بالنسبة لأنك تقدر تستخدم فنكشنات الbinary tree و ال binary search tree على المتغير bst لان في النهاية ال binary search tree انهرت في ال binary tree فنقدر نستخدم فنكشنات الاثنين على ال متغير bst من نوع binary search tree و بالتوفيق :smile2:
 
مثل ما ذكرت سابقاً ال binary search tree انتهتنز ال binary tree فالمتغير الي عرفناه في المين bst من نوع binary search tree يقدر يستخدم فنكشنات ال binary tree و يقدر يستخدم فنكشنات ال binary search tree انزين نجي لفنكشنات كل وحدة منهم :

فنكشنات ال Binary Tree
كود:
 const binaryTreeType<elemType>& operator=
                 (const binaryTreeType<elemType>&); 
    bool isEmpty();


    void inorderTraversal();
     
    void preorderTraversal();
   
    void postorderTraversal();


    int treeHeight();
   
    int treeNodeCount();
   
    int treeLeavesCount();


    void destroyTree();


    binaryTreeType(const binaryTreeType<elemType>& otherTree); 


    binaryTreeType();   


    ~binaryTreeType();

فنكشنات ال Binary Search Tree
كود:
  bool search(const elemType& searchItem);


    void insert(const elemType& insertItem);
   
    void deleteNode(const elemType& deleteItem);

نجي للأسايمنت :
بارت ون نعرف متغير اسمه bst من نوع binary search tree و بكون التمبلت ماله أنتجر لان الي بندخله عبارة عن ارقام
كود:
int main(){
[COLOR=#ff0000]    // Define a binary search tree object bst of type bSearchTreeType[/COLOR]
[COLOR=#008000]    bSearchTreeType<[/COLOR][COLOR=#0000cd]int[/COLOR][COLOR=#008000]> bst; [/COLOR]
    system("pause");
    return 0;
}
بارت تو : نسوي انسيرت ل 8 ارقام الي عطانا اياها بواسطة فنكشن insert الموجودة عندنا في الباينري سيرج تري :
كود:
int main(){
[COLOR=#ff0000]    // Define a binary search tree object bst of type bSearchTreeType[/COLOR]
    [COLOR=#008000]bSearchTreeType<[/COLOR][COLOR=#0000ff]int[/COLOR][COLOR=#008000]> bst; [/COLOR]
    [COLOR=#ff0000]// Insert the following integers into bst: 100,40, 20, 15, 30, 40, 35, and 77.[/COLOR] 
[COLOR=#006400]    bst.insert(100);[/COLOR]
[COLOR=#006400]    bst.insert(40);[/COLOR]
[COLOR=#006400]    bst.insert(20);[/COLOR]
[COLOR=#006400]    bst.insert(15);[/COLOR]
[COLOR=#006400]    bst.insert(30);[/COLOR]
[COLOR=#006400]    bst.insert(40);[/COLOR]
[COLOR=#006400]    bst.insert(35);[/COLOR]
[COLOR=#006400]    bst.insert(75);[/COLOR]


    system("pause");
    return 0;
}
بارت 3 يبغى يعرف ارتفاع التري فبنستخدم فنكشن treeHeight الموجودة في ال Binary tree بس مثل ما نلاحظ ان نوعها int فاذا بنستخدمها بنتحتاج نعرف متغير من نوع int نخزن فيه الارتفاع أو نستخدم cout افضل :
كود:
int main(){
[COLOR=#FF0000]    // Define a binary search tree object bst of type bSearchTreeType[/COLOR]
    [COLOR=#008000]bSearchTreeType<[/COLOR][COLOR=#0000FF]int[/COLOR][COLOR=#008000]> bst; [/COLOR]
    [COLOR=#FF0000]// Insert the following integers into bst: 100,40, 20, 15, 30, 40, 35, and 77.[/COLOR] 
[COLOR=#006400]    bst.insert(100);[/COLOR]
[COLOR=#006400]    bst.insert(40);[/COLOR]
[COLOR=#006400]    bst.insert(20);[/COLOR]
[COLOR=#006400]    bst.insert(15);[/COLOR]
[COLOR=#006400]    bst.insert(30);[/COLOR]
[COLOR=#006400]    bst.insert(40);[/COLOR]
[COLOR=#006400]    bst.insert(35);[/COLOR]
[COLOR=#006400]    bst.insert(75);[/COLOR]
[COLOR=#ff0000]// Display the height of bst. [/COLOR]
[COLOR=#008000]    cout << "The heigth of binary tree is :" <<[/COLOR][COLOR=#0000ff] bst.treeHeigth()[/COLOR][COLOR=#008000] << endl ; [/COLOR]



    system("pause");
    return 0;
}
أتمنى تكون وضحت الفكرة بالنسبة لأنك تقدر تستخدم فنكشنات الbinary tree و ال binary search tree على المتغير bst لان في النهاية ال binary search tree انهرت في ال binary tree فنقدر نستخدم فنكشنات الاثنين على ال متغير bst من نوع binary search tree و بالتوفيق :smile2:

شكرا لك اخوي ما قصرت والله يعطيك العافية
 
سلام عليكم
ابي اعرف شنو الدروس المهم الي اركز عليها حق فانيل
 
سلام عليكم
ابي اعرف شنو الدروس المهم الي اركز عليها حق فانيل
عليكم السلام
عادةً يركزون على الجباتر الأخيرة :
- ال hashing
- ال binary tree بما فيه الاوردرات الثلاثة ، و كيسات الدليت و الادد
- ال graph الدايركت و الاندايركت ، الأوردارات ثنينهم ( depth , breversl ) ، و طبعاً الويت غراف و باقي الانواع
- ال stack و queue
- و شلون تسوون دات ستركجر لغرض معين باللنكد لست هذا دائما يجي مثل هذا السؤال :
https://www.uob-bh.com/forum/downlo...8879-2794990c18802e1ef472a2654dd53fc13d65f5b0
باقي الاشياء ما يركزون عليها واجد أو ما يركزون اساساً بس راجعيهم من باب الأحتياط
 


السلام عليكم و رحمة الله و بركاته


تم فتح هذا الموضوع لمناقشة مقرر ITCS215


قسم المقرر في المكتبة الجامعية
https://www.uob-bh.com/forum/downloads.php?do=cat&id=5


لأي استفسارات أو مساعدة تتعلق بالمقرر الرجاء طرحها هنا


بالتوفيق للجميع​
 
ممكن احد يشرح اللنك ؟؟؟؟

واشلون يجيبون الاسئله في الامتحان ...:blushing:
 
ممكن احد يشرح اللنك ؟؟؟؟

واشلون يجيبون الاسئله في الامتحان ...:blushing:
اسئلة الإمتحان بتكون عبارة عن كتابة فنكشن بنوعيه ممبر فنكشن و نون ممبر فنكشن ، بالنسبة للشرح صعب انه ينشرح بالكامل لانه بياخذ وقت طويل شوي فإذا تقدرين تحددين الجزئيات الي مو فاهمتنها يكون افضل بالإضافة تبغين اللنكد لست أو اللنكد لستد دبلي او اللنكد لست سيركلر ؟
 
السلام عليكم و رحمة الله و بركاته


تم فتح هذا الموضوع لمناقشة مقرر ITCS215


قسم المقرر في المكتبة الجامعية
https://www.uob-bh.com/forum/downloads.php?do=cat&id=5


لأي استفسارات أو مساعدة تتعلق بالمقرر الرجاء طرحها هنا


بالتوفيق للجميع

 
شباب في أحد يعرف مدرس خصوصي حق مواد itcs215 او مواد الcs بشكل عام
 
1. Declare an object named intList of arrayListType to handle a list of maximum 10 integers.
2. Refer to the header file “arrayList.h” to call the appropriate function to:
a. Insert the following elements in the list: 4, 8, 16, and 32 (use a loop).
b. Insert 12 after 8 and 20, 24, and 28 after 16.
c. Display the size of the list.
d. Print the elements of the list.
e. Test whether the list is full or not.
f. Insert 100 at the 5th location and 0 at the end.
g. Print the elements of the list

أحد يعرف شلون اسوي هدا؟.
 
1. Declare an object named intList of arrayListType to handle a list of maximum 10 integers.
2. Refer to the header file “arrayList.h” to call the appropriate function to:
a. Insert the following elements in the list: 4, 8, 16, and 32 (use a loop).
b. Insert 12 after 8 and 20, 24, and 28 after 16.
c. Display the size of the list.
d. Print the elements of the list.
e. Test whether the list is full or not.
f. Insert 100 at the 5th location and 0 at the end.
g. Print the elements of the list

أحد يعرف شلون اسوي هدا؟.
الاسايمنت الشغل فيه بكون في المين بس
في البداية بتعرفين اوبجت بالأسم الي ذكره ، و بتخلين الحجم 10 و باقي الشغل كله كول فنكشن عادي للتوضيح اكثر بتعرفين الأوبجكت بهاذي الطريقة :
PHP:
arrayList<int> intList(10) ;
و باقي الشغل كله كول فنكشن على سبيل المثال بارت a بنسوي انسيرت للألمنت الي طلبها على سبيل المثال المنت 4
PHP:
intList.insertEnd(4);
و باقي الشغل بكون نفس الطريقة مجرد كول فنكشن و تسوين المطلوب
 
الاسايمنت الشغل فيه بكون في المين بس
في البداية بتعرفين اوبجت بالأسم الي ذكره ، و بتخلين الحجم 10 و باقي الشغل كله كول فنكشن عادي للتوضيح اكثر بتعرفين الأوبجكت بهاذي الطريقة :
PHP:
arrayList<int> intList(10) ;
و باقي الشغل كله كول فنكشن على سبيل المثال بارت a بنسوي انسيرت للألمنت الي طلبها على سبيل المثال المنت 4
PHP:
intList.insertEnd(4);
و باقي الشغل بكون نفس الطريقة مجرد كول فنكشن و تسوين المطلوب

اخوي احين انا سويت الأسايمنت و الكود كامل صح لكن لين اسوي compile يطلع لي هالأرور
(undefined reference to `arrayListType<int>::arrayListType(int

هو يطلع على كل ال functions مو بس هاي

و فالأخير يطلع ld returned 1 exit status
 
هاي الكود
main.cpp
كود:
#include <iostream>
#include "arrayListType.h"



using namespace std;



int main() {
   
int maxSize=10;   
arrayListType<int>intList(maxSize);
int num=4;
int loc1,loc2;

for (int i=0;i<4;i++){

intList.insert(num);
num *=2;
}
loc1=intList.seqSearch(8)+1;
intList.insertAt(loc1,12);


loc2=intList.seqSearch(16)+1;
intList.insertAt(loc2,20);
intList.insertAt(loc2+1,24);
intList.insertAt(loc2+2,28);

cout<<"The size of the list: "<< intList.listSize()<<endl;
cout<<"List elements: ";
intList.print();
cout <<endl;
if(intList.isFull()==true){
    cout <<"List is full.";
}
else {
    cout<<"List is not full."<<endl;
}
intList.insertAt(5,100);
intList.insertEnd(0);
cout <<endl<<"List after adding 0 and 100: ";
intList.print();
cout<<endl;
int max=intList.max();
int min=intList.min();

intList.remove(max);
intList.remove(min);

cout <<endl<<endl<<"The members of intList : "<<endl;
cout <<endl<<"maxSize= "<<maxSize<<endl;
cout <<"Length= "<<intList.listSize()<<endl;
cout <<"The list after removing the max and min elements : ";
intList.print();

    return 0;

}

arrayListType.h
كود:
#ifndef ARRAYLISTTYPE_H
#define ARRAYLISTTYPE_H

template <class elemType>
class arrayListType
{
public:
    elemType max();
    elemType min();
    const arrayListType<elemType>& operator=(const arrayListType<elemType>&);
    bool isEmpty();
    bool isFull();
    int listSize();
    int maxListSize();
    void print() const;
    bool isItemAtEqual(int location, const elemType& item);
    void insertAt(int location, const elemType& insertItem);   
    void insertEnd(const elemType& insertItem);
    void removeAt(int location);
    void retrieveAt(int location, elemType& retItem);
    void replaceAt(int location, const elemType& repItem);
    void clearList();
    int seqSearch(const elemType& item);
    void insert(const elemType& insertItem);
    void remove(const elemType& removeItem);
    arrayListType(int size = 100);
    arrayListType(const arrayListType<elemType>& otherList);
    ~arrayListType();

protected:
    elemType *list;    
    int length;       
    int maxSize;       
};


#endif

arrayListImplement.cpp
كود:
#include "arrayListType.h"
#include <iostream>

using namespace std;



template <class elemType>
elemType arrayListType<elemType>::max(){
    elemType max=list[0];
for (int i=0;i<length;i++){
    if(list[i]>max){
        max=list[i];
    }
}
   
return max;   
}
template <class elemType>
elemType arrayListType<elemType>::min(){
        elemType min=list[0];
for (int i=0;i<length;i++){
    if(list[i]<min){
        min=list[i];
    }
}
   
return min;
   
}

template <class elemType>
bool arrayListType<elemType>::isEmpty()
{
    return (length == 0);
}

template <class elemType>
bool arrayListType<elemType>::isFull()
{
    return (length == maxSize);
}
template <class elemType>
int arrayListType<elemType>::listSize()
{
    return length;
}

template <class elemType>
int arrayListType<elemType>::maxListSize()
{
    return maxSize;
}

template <class elemType>
void arrayListType<elemType>::print() const
{
    for(int i = 0; i < length; i++)
        cout<<list[i]<<" ";
    cout<<endl;
}

template <class elemType>
bool arrayListType<elemType>::isItemAtEqual
                   (int location, const elemType& item)
{
    return(list[location] == item);
}

template <class elemType>
void arrayListType<elemType>::insertAt
                   (int location, const elemType& insertItem)
{
    if(location < 0 || location >= maxSize)
        cerr<<"The position of the item to be inserted "
            <<"is out of range."<<endl;
    else
        if(length >= maxSize)  //list is full
            cerr<<"Cannot insert in a full list"<<endl;
        else
        {
            for(int i = length; i > location; i--)
                list[i] = list[i - 1];    //move the elements down

            list[location] = insertItem;    //insert the item at the
                                         //specified position

            length++;    //increment the length
        }
} //end insertAt

template <class elemType>
void arrayListType<elemType>::insertEnd(const elemType& insertItem)
{
    if(length >= maxSize)  //the list is full
        cerr<<"Cannot insert in a full list."<<endl;
    else
    {
        list[length] = insertItem;    //insert the item at the end
        length++;    //increment length
    }
} //end insertEnd

template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
    if(location < 0 || location >= length)
        cerr<<"The location of the item to be removed "
            <<"is out of range."<<endl;
    else
    {
           for(int i = location; i < length - 1; i++)
             list[i] = list[i+1];

        length--;
    }
} //end removeAt

template <class elemType>
void arrayListType<elemType>::retrieveAt
                     (int location, elemType& retItem)
{
    if(location < 0 || location >= length)
        cerr<<"The location of the item to be retrieved is "
            <<"out of range."<<endl;
    else
        retItem = list[location];
} // retrieveAt

template <class elemType>
void arrayListType<elemType>::replaceAt
                    (int location, const elemType& repItem)
{
    if(location < 0 || location >= length)
        cerr<<"The location of the item to be replaced is "
            <<"out of range."<<endl;
    else
        list[location] = repItem;

} //end replaceAt

template <class elemType>
void arrayListType<elemType>::clearList()
{
    length = 0;
} // end clearList

template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item)
{
    int loc;
    bool found = false;

    for(loc = 0; loc < length; loc++)
       if(list[loc] == item)
       {
        found = true;
        break;
       }

    if(found)
        return loc;
    else
        return -1;
} //end seqSearch

template <class elemType>
void arrayListType<elemType>::insert(const elemType& insertItem)
{
    int loc;

    if(length == 0)                     //list is empty
        list[length++] = insertItem; //insert the item and
                                      //increment the length
    else
        if(length == maxSize)
            cout<<"Cannot insert in a full list."<<endl;
        else
        {
            loc = seqSearch(insertItem);

            if(loc == -1)   //the item to be inserted
                            //does not exist in the list
                list[length++] = insertItem;
            else
                cerr<<"the item to be inserted is already in "
                     <<"the list. No duplicates are allowed."<<endl;
    }
} //end insert

template <class elemType>
void arrayListType<elemType>::remove(const elemType& removeItem)
{
    int loc;

    if(length == 0)
        cerr<<"Cannot delete from an empty list."<<endl;
    else
    {
        loc = seqSearch(removeItem);

        if(loc != -1)
            removeAt(loc);
        else
            cout<<"The tem to be deleted is not in the list."
                <<endl;
    }

} //end remove


template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
    if(size < 0)
    {
        cerr<<"The array size must be positive. Creating "
             <<"an array of size 100. "<<endl;

        maxSize = 100;
     }
     else
        maxSize = size;

    length = 0;

    list = new elemType[maxSize];
}

template <class elemType>
arrayListType<elemType>::~arrayListType()
{
    delete [] list;
}

    //copy constructor
template <class elemType>
arrayListType<elemType>::arrayListType
                   (const arrayListType<elemType>& otherList)
{
   maxSize = otherList.maxSize;
   length = otherList.length;
   list = new elemType[maxSize];     //create the array
   assert(list != NULL);    //terminate if unable to allocate
                             //memory space

   for(int j = 0; j < length; j++)  //copy otherList
        list [j] = otherList.list[j];
}//end copy constructor


template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator=
            (const arrayListType<elemType>& otherList)
{
    if(this != &otherList)    //avoid self-assignment
    {                  
       delete [] list;                  
       maxSize = otherList.maxSize;          
       length = otherList.length;              
     
       list = new elemType[maxSize];          
       assert(list != NULL);              

       for(int i = 0; i < length; i++)      
               list[i] = otherList.list[i];      
    }

    return *this;                             
}
 
هاي الكود
main.cpp
كود:
#include <iostream>
#include "arrayListType.h"



using namespace std;



int main() {
 
int maxSize=10; 
arrayListType<int>intList(maxSize);
int num=4;
int loc1,loc2;

for (int i=0;i<4;i++){

intList.insert(num);
num *=2;
}
loc1=intList.seqSearch(8)+1;
intList.insertAt(loc1,12);


loc2=intList.seqSearch(16)+1;
intList.insertAt(loc2,20);
intList.insertAt(loc2+1,24);
intList.insertAt(loc2+2,28);

cout<<"The size of the list: "<< intList.listSize()<<endl;
cout<<"List elements: ";
intList.print();
cout <<endl;
if(intList.isFull()==true){
    cout <<"List is full.";
}
else {
    cout<<"List is not full."<<endl;
}
intList.insertAt(5,100);
intList.insertEnd(0);
cout <<endl<<"List after adding 0 and 100: ";
intList.print();
cout<<endl;
int max=intList.max();
int min=intList.min();

intList.remove(max);
intList.remove(min);

cout <<endl<<endl<<"The members of intList : "<<endl;
cout <<endl<<"maxSize= "<<maxSize<<endl;
cout <<"Length= "<<intList.listSize()<<endl;
cout <<"The list after removing the max and min elements : ";
intList.print();

    return 0;

}

arrayListType.h
كود:
#ifndef ARRAYLISTTYPE_H
#define ARRAYLISTTYPE_H

template <class elemType>
class arrayListType
{
public:
    elemType max();
    elemType min();
    const arrayListType<elemType>& operator=(const arrayListType<elemType>&);
    bool isEmpty();
    bool isFull();
    int listSize();
    int maxListSize();
    void print() const;
    bool isItemAtEqual(int location, const elemType& item);
    void insertAt(int location, const elemType& insertItem); 
    void insertEnd(const elemType& insertItem);
    void removeAt(int location);
    void retrieveAt(int location, elemType& retItem);
    void replaceAt(int location, const elemType& repItem);
    void clearList();
    int seqSearch(const elemType& item);
    void insert(const elemType& insertItem);
    void remove(const elemType& removeItem);
    arrayListType(int size = 100);
    arrayListType(const arrayListType<elemType>& otherList);
    ~arrayListType();

protected:
    elemType *list;  
    int length;     
    int maxSize;     
};


#endif

arrayListImplement.cpp
كود:
#include "arrayListType.h"
#include <iostream>

using namespace std;



template <class elemType>
elemType arrayListType<elemType>::max(){
    elemType max=list[0];
for (int i=0;i<length;i++){
    if(list[i]>max){
        max=list[i];
    }
}
 
return max; 
}
template <class elemType>
elemType arrayListType<elemType>::min(){
        elemType min=list[0];
for (int i=0;i<length;i++){
    if(list[i]<min){
        min=list[i];
    }
}
 
return min;
 
}

template <class elemType>
bool arrayListType<elemType>::isEmpty()
{
    return (length == 0);
}

template <class elemType>
bool arrayListType<elemType>::isFull()
{
    return (length == maxSize);
}
template <class elemType>
int arrayListType<elemType>::listSize()
{
    return length;
}

template <class elemType>
int arrayListType<elemType>::maxListSize()
{
    return maxSize;
}

template <class elemType>
void arrayListType<elemType>::print() const
{
    for(int i = 0; i < length; i++)
        cout<<list[i]<<" ";
    cout<<endl;
}

template <class elemType>
bool arrayListType<elemType>::isItemAtEqual
                   (int location, const elemType& item)
{
    return(list[location] == item);
}

template <class elemType>
void arrayListType<elemType>::insertAt
                   (int location, const elemType& insertItem)
{
    if(location < 0 || location >= maxSize)
        cerr<<"The position of the item to be inserted "
            <<"is out of range."<<endl;
    else
        if(length >= maxSize)  //list is full
            cerr<<"Cannot insert in a full list"<<endl;
        else
        {
            for(int i = length; i > location; i--)
                list[i] = list[i - 1];    //move the elements down

            list[location] = insertItem;    //insert the item at the
                                         //specified position

            length++;    //increment the length
        }
} //end insertAt

template <class elemType>
void arrayListType<elemType>::insertEnd(const elemType& insertItem)
{
    if(length >= maxSize)  //the list is full
        cerr<<"Cannot insert in a full list."<<endl;
    else
    {
        list[length] = insertItem;    //insert the item at the end
        length++;    //increment length
    }
} //end insertEnd

template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
    if(location < 0 || location >= length)
        cerr<<"The location of the item to be removed "
            <<"is out of range."<<endl;
    else
    {
           for(int i = location; i < length - 1; i++)
             list[i] = list[i+1];

        length--;
    }
} //end removeAt

template <class elemType>
void arrayListType<elemType>::retrieveAt
                     (int location, elemType& retItem)
{
    if(location < 0 || location >= length)
        cerr<<"The location of the item to be retrieved is "
            <<"out of range."<<endl;
    else
        retItem = list[location];
} // retrieveAt

template <class elemType>
void arrayListType<elemType>::replaceAt
                    (int location, const elemType& repItem)
{
    if(location < 0 || location >= length)
        cerr<<"The location of the item to be replaced is "
            <<"out of range."<<endl;
    else
        list[location] = repItem;

} //end replaceAt

template <class elemType>
void arrayListType<elemType>::clearList()
{
    length = 0;
} // end clearList

template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item)
{
    int loc;
    bool found = false;

    for(loc = 0; loc < length; loc++)
       if(list[loc] == item)
       {
        found = true;
        break;
       }

    if(found)
        return loc;
    else
        return -1;
} //end seqSearch

template <class elemType>
void arrayListType<elemType>::insert(const elemType& insertItem)
{
    int loc;

    if(length == 0)                     //list is empty
        list[length++] = insertItem; //insert the item and
                                      //increment the length
    else
        if(length == maxSize)
            cout<<"Cannot insert in a full list."<<endl;
        else
        {
            loc = seqSearch(insertItem);

            if(loc == -1)   //the item to be inserted
                            //does not exist in the list
                list[length++] = insertItem;
            else
                cerr<<"the item to be inserted is already in "
                     <<"the list. No duplicates are allowed."<<endl;
    }
} //end insert

template <class elemType>
void arrayListType<elemType>::remove(const elemType& removeItem)
{
    int loc;

    if(length == 0)
        cerr<<"Cannot delete from an empty list."<<endl;
    else
    {
        loc = seqSearch(removeItem);

        if(loc != -1)
            removeAt(loc);
        else
            cout<<"The tem to be deleted is not in the list."
                <<endl;
    }

} //end remove


template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
    if(size < 0)
    {
        cerr<<"The array size must be positive. Creating "
             <<"an array of size 100. "<<endl;

        maxSize = 100;
     }
     else
        maxSize = size;

    length = 0;

    list = new elemType[maxSize];
}

template <class elemType>
arrayListType<elemType>::~arrayListType()
{
    delete [] list;
}

    //copy constructor
template <class elemType>
arrayListType<elemType>::arrayListType
                   (const arrayListType<elemType>& otherList)
{
   maxSize = otherList.maxSize;
   length = otherList.length;
   list = new elemType[maxSize];     //create the array
   assert(list != NULL);    //terminate if unable to allocate
                             //memory space

   for(int j = 0; j < length; j++)  //copy otherList
        list [j] = otherList.list[j];
}//end copy constructor


template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator=
            (const arrayListType<elemType>& otherList)
{
    if(this != &otherList)    //avoid self-assignment
    {                
       delete [] list;                
       maxSize = otherList.maxSize;        
       length = otherList.length;            
   
       list = new elemType[maxSize];        
       assert(list != NULL);            

       for(int i = 0; i < length; i++)    
               list[i] = otherList.list[i];    
    }

    return *this;                           
}

في ملف الـ arrayListType.h عندك خطأ الاف اندفين والدفين المفروض تنكتب جدي
كود:
#ifndef H_arrayListType
#define H_arrayListType
.
.
.
#endif


ملف الـ arrayListTypeImplement.h
كود:
#include "arrayListType.h"
#include <cassert>
#include <iostream>

وفي الـ main.cpp نسيت تسوي انكلود لملف الامبلمنت,,
كود:
#include <iostream>
#include "arrayListImplement.h"
#include "arrayListType.h"
 
في ملف الـ arrayListType.h عندك خطأ الاف اندفين والدفين المفروض تنكتب جدي
كود:
#ifndef H_arrayListType
#define H_arrayListType
.
.
.
#endif


ملف الـ arrayListTypeImplement.h
كود:
#include "arrayListType.h"
#include <cassert>
#include <iostream>

وفي الـ main.cpp نسيت تسوي انكلود لملف الامبلمنت,,
كود:
#include <iostream>
#include "arrayListImplement.h"
#include "arrayListType.h"

تسلم :RpS_lol:
 
ممكن حل هالسؤال ؟

Write a function InsertAt to insert item before the first even number
 
ممكن حل هالسؤال ؟

Write a function InsertAt to insert item before the first even number
السلام عليكم
اذا ممكن تحطين صورة السؤال يكون افضل ، لان ما اعرف السؤال للاري باسد لست او اللنكد لست ، بالإضافة ما اعرف اذا الفنكشن ممبر او نون ممبر
بالنسبة للسوال ما فيه فكرة معقدة بتسوين نستد لوب ، اللوب الاول للسيرج و فيه شرط بحيث يوقف اذا عدد زوجي ، اللوب الثاني بعد الشرط بكون لوب يسوي شفت لليمين و انسيرت للايتم الي دخله اليوزر
 
Part A (3 points)

  1. Create a new project named LinkedList

  2. Add a new .cpp file named assignment3 to your project.

  3. Declare three linkedListType objects named list1 and list2 to handle integer items.

  4. Insert the following items in list1: 11,9, 7, 20, 3, and 6.

  5. Print the elements of list1.



    Part B (4 points)

  6. Add a member function replaceAt to the class linkedListType to replace the data value in node index position, passed as a parameter, by another parameter newItem. The index of the first node is 0 and increases by 1 for each subsequent node. If the list is empty or the index is invalid then return false, else return true.

    Add the function prototype to the header file linkedList.h and implement it in the header file linkedListImplement.h:

    bool replaceAt(int position, const Type& newItem);
    1. Call the function replaceAt to replace the element at position 0 of list2 by 100.

    2. Call the function replaceAt to replace the element at position -1 of list1 by 100.

    3. Call the function replaceAt to replace the element at position 2 of list1 by 100.

    4. Print the elements of list1.
Part C (3 points)

  1. Add a member function insertAt to the class linkedListType to insert a data value newItem at index position. Index position and data value newItem are passed as parameters to the function. The index of the first node is 0 and increases by 1 for each subsequent node. If the index is invalid (less than zero or greater than count) then the function does not insert newItem and returns false. Else, insert the new item at the given position and returns true after insertion (treat the special cases where index = zero or index = count).

    Add the function prototype to the header file linkedList.h and implement it in the header file linkedListImplement.h:

    bool insertAt(int position, const Type& newItem);


    Example:

    Before insertion

    list: 5 10 20 15 22 33

    position: 2 newItem: 30

    After insertion

    list: 5 10 30 20 15 22 33
    1. Call the function insertAt with the two parameters -2 and 6 for list1.

    2. Call the function insertAt with the two parameters 7 and -1 for list1.

    3. Call the function insertAt with the two parameters 6 and 200 for list1.

    4. Call the function insertAt with the two parameters 0 and -1 for list2.

    5. Display list1 and list2.
 
السؤال

Part A (3 points)
1. Create a new project in which you add the two header files: “linkedList.h” and “linkedListImplement.h” (provided in blackboard).
2. Add a new .cpp file named assignment3 to your project.
3. In the file assignment3-02 include the header file “linkedListImplement.h”.
4. Declare three linkedListType objects named list1 and list2 to handle integer items.
5. Insert the following items in list1: 11,9, 7, 20, 3, and 6.
6. Print the elements of list1.
Part B (4 points)
7. Add a member function replaceAt to the class linkedListType to replace the data value in node index position, passed as a parameter, by another parameter newItem. The index of the first node is 0 and increases by 1 for each subsequent node. If the list is empty or the index is invalid then return false, else return true.
Add the function prototype to the header file linkedList.h and implement it in the header file linkedListImplement.h:
bool replaceAt(int position, const Type& newItem);
a. Call the function replaceAt to replace the element at position 0 of list2 by 100.
b. Call the function replaceAt to replace the element at position -1 of list1 by 100.
c. Call the function replaceAt to replace the element at position 2 of list1 by 100.
d. Print the elements of list1.
Part C (3 points)
8. Add a member function insertAt to the class linkedListType to insert a data value newItem at index position. Index position and data value newItem are passed as parameters to the function. The index of the first node is 0 and increases by 1 for each subsequent node. If the index is invalid (less than zero or greater than count) then the function does not insert newItem and returns false. Else, insert the new item at the given position and returns true after insertion (treat the special cases where index = zero or index = count).
Add the function prototype to the header file linkedList.h and implement it in the header file linkedListImplement.h:
bool insertAt(int position, const Type& newItem);
Example:
Before insertion
list: 5 10 20 15 22 33
position: 2 newItem: 30
After insertion
list: 5 10 30 20 15 22 33
a. Call the function insertAt with the two parameters -2 and 6 for list1.
b. Call the function insertAt with the two parameters 7 and -1 for list1.
c. Call the function insertAt with the two parameters 6 and 200 for list1.
d. Call the function insertAt with the two parameters 0 and -1 for list2.
e. Display list1 and list2.

الجواب ممكن أحد يشوف لي وش الايرور؟؟

PHP:
#include<iostream>
using namespace std;
 
template<class Type>
struct nodeType
{
    Type info;
    nodeType<Type> *link;
};
 
template<class Type>
class linkedListType
{
public:
    const linkedListType<Type>& operator=(const linkedListType<Type>&);
    void initializeList();
    bool isEmpty();
    int length();
    void destroyList();
    Type front();
    Type back();
    bool search(const Type& searchItem);
    void insertFirst(const Type& newItem);
    void insertLast(const Type& newItem);
    void deleteNode(const Type& deleteItem);   
    linkedListType();
    linkedListType(const linkedListType<Type>& otherList);
    ~linkedListType();
    bool print() ;
    bool replaceAt(int position, const Type& newItem);
    bool insertAt(int position ,const Type &Item) ;
    bool retrieveAt(int pos,Type & item) ;
    void insertArray(const Type array[],int size) ;
//protected:
    int count;
    nodeType<Type> *first;
    nodeType<Type> *last;
 
private:
    void copyList(const linkedListType<Type>& otherList);
};
 
template<class Type>
ostream& operator<<(ostream&, const linkedListType<Type>&);
 
 
 
 
#include"linkedList.h"
#include<cassert>
 
template<class Type>
ostream& operator<<(ostream& osObject, const linkedListType<Type>& list)
{
    nodeType<Type> *current; // pointer to traverse the list
    current = list.first;
    while(current != NULL)
    {
        osObject<<current->info<<" ";
        current = current->link;
    }
    return osObject;
}
 
template<class Type>
const linkedListType<Type>& linkedListType<Type>::operator=(const linkedListType<Type>& otherList)
{
    if(this != &otherList) //avoid self-copy
        copyList(otherList);
    return *this;
}
 
template<class Type>
void linkedListType<Type>::initializeList()
{
    destroyList();
}
 
 
template<class Type>
bool linkedListType<Type>::isEmpty()
{
    return(first == NULL);
}
 
template<class Type>
int linkedListType<Type>::length()
{
    return count;
}
 
template<class Type>
void linkedListType<Type>::destroyList()
{
    nodeType<Type> *temp;
    while(first != NULL)
    {
        temp = first;
        first = first->link;
        delete temp;
    }
    last = NULL; 
    count = 0;
}
 
template<class Type>
Type linkedListType<Type>::front()
{
    assert(last != NULL);
    return first->info;
}
 
template<class Type>
Type linkedListType<Type>::back()
{
    assert(last != NULL);
    return last->info;
}
 
template<class Type>
bool linkedListType<Type>::search(const Type& searchItem)
{
    nodeType<Type> *current;
    bool found;
    current = first;
    found = false;
    while(current != NULL && !found)
        if(current->info == searchItem)
              found = true;
        else
              current = current->link;
    return found;
}
 
template<class Type>
void linkedListType<Type>::insertFirst(const Type& newItem)
{
    nodeType<Type> *newNode;
    newNode = new nodeType<Type>;
    assert(newNode != NULL);
    newNode->info = newItem;
    newNode->link = first;
    first = newNode;
    count++;
    if(last == NULL)
        last = newNode;
}
 
template<class Type>
void linkedListType<Type>::insertLast(const Type& newItem)
{
    nodeType<Type> *newNode;
    newNode = new nodeType<Type>;
    assert(newNode != NULL);
    newNode->info = newItem;
    newNode->link = NULL;
    if(first == NULL)
    {
        first = newNode;
        last = newNode;
    }
    else
    {
        last->link = newNode;
        last = newNode;
    }
    count++;
}
 
template<class Type>                               
void linkedListType<Type>::deleteNode(const Type& deleteItem)
{
    nodeType<Type> *current;       // pointer to traverse the list
    nodeType<Type> *trailCurrent;  //pointer just before current
    bool found;
    if(first == NULL)
        cout<<"Cannot delete from an empty list."<<endl;
    else
    {
        if(first->info == deleteItem)
        {
            current = first;
            first = first->link;
            count--;
            if(first == NULL)  // list has only one node
                last = NULL;
            delete current;
        }
        else // search the list for the node with the given info
        {
            found = false;
            trailCurrent = first;
            current = first->link;
            while(current != NULL && !found)
            {
                if(current->info != deleteItem)
                {
                    trailCurrent = current;
                    current = current->link;
                }
                else
                    found = true;
            }
            if(found)
            {
                trailCurrent->link = current->link;
                count--;
                if(last == current) //node to be deleted is the last node
                    last = trailCurrent;
                delete current;
            }
            else
                cout<<"Item to be deleted is not in the list."<<endl;
        }
    }
}
 
template<class Type>
linkedListType<Type>::linkedListType()
{
    first = NULL;
    last = NULL;
    count = 0;
}
 
template<class Type>
linkedListType<Type>::linkedListType(const linkedListType<Type>& otherList)
{
    first = NULL;
    copyList(otherList);
}
 
template<class Type>
linkedListType<Type>::~linkedListType()
{
    destroyList();
}
 
template<class Type>
void linkedListType<Type>::copyList(const linkedListType<Type>& otherList)
{
    nodeType<Type> *newNode;  //pointer to create a node
    nodeType<Type> *current;   //pointer to traverse the list
    if(first != NULL)
        destroyList();
    if(otherList.first == NULL)  //otherList is empty
    {
        first = NULL;
        last = NULL;
        count = 0;
    }
    else
    {
        current = otherList.first;  //current points to the lis to be copied
        count = otherList.count;
        first = new nodeType<Type>; //create the node
        assert(first != NULL);
        first->info = current->info; //copy the info
        first->link = NULL;     //set the link field of the node to NULL
        last = first;            //make last point to the first node
        current = current->link;  //make current point to the next node
        while(current != NULL) //copy the remaining list
        {
            newNode = new nodeType<Type>; //create a node
            assert(newNode != NULL);
            newNode->info = current->info; //copy the info
            newNode->link = NULL;   //set the link of newNode to NULL
            last->link = newNode;     //attach newNode after last
            last = newNode;       //make last point to the actual last node
            current = current->link;   //make current point to the next node
        }
    }
}
 
template<class Type>
bool linkedListType<Type>::print()
{
    nodeType<Type> *current;
    if(first == NULL)
    {
        cout<<"Cannot retrieve in an empty list."<<endl<<endl;
        return false;
    }
    else
    {
        current = first;
        for(int i=0; i<count; i++)
        {       cout << " " << current->info << " "; 
                current = current->link;
  }
        return true;
    }
}
 
template<class Type>
bool linkedListType<Type>::replaceAt(int position, const Type& newItem)
{
    nodeType<Type> *current;
    if(first == NULL)
    {
        cout<<"\n Cannot replace in an empty list."<<endl<<endl;
        return false;
    }
    else if(position < 0 || position > count)
    {
        cout<<"\n Invalid position."<<endl<<endl;
        return false;
    }
    else
    {
        current = first;
        for(int i=0; i<position; i++)
            current = current->link;
        current->info = newItem;
        return true;
    }
}
 
template <class Type>
bool linkedListType<Type>::insertAt(int position ,const Type &Item)
{
     if(position < 0 || position > count )
     {
          cerr << "\n Invalid Position \n" << endl;
          return false ;
     }
    
     nodeType <Type> *current , *newNode ;
     newNode = new nodeType <Type>;
     assert (newNode != NULL);
    
     newNode -> info = Item ;
     newNode -> link = NULL ;
     if ( first == NULL)
     {
          first = newNode ;
          last = newNode;
          count++;
          return true ;
          }
         
     else if ( position == 0 )
     {
          newNode -> link = first ;
          first = newNode ;
          count++;
          return true;
     }
    
     else if ( position == count )
     {
          last->link = newNode ;
          last = newNode;
          count++;
          return true ;
     }
    
     current = first ;
     for (int i = 0 ; i <position-1 ; ++i)
     current = current -> link ;
    
         
}
 
 
 
 
#include <iostream>
#include <cassert>
using namespace std;
 
 
template<class Type>
ostream& operator<<(ostream& osObject, const linkedListType<Type>& list)
{
    nodeType<Type> *current; // pointer to traverse the list
    current = list.first;
    while(current != NULL)
    {
        osObject<<current->info<<" ";
        current = current->link;
    }
    return osObject;
}
 
template<class Type>
const linkedListType<Type>& linkedListType<Type>::operator=(const linkedListType<Type>& otherList)
{
    if(this != &otherList) //avoid self-copy
        copyList(otherList);
    return *this;
}
 
template<class Type>
void linkedListType<Type>::initializeList()
{
    destroyList();
}
 
 
template<class Type>
bool linkedListType<Type>::isEmpty()
{
    return(first == NULL);
}
 
template<class Type>
int linkedListType<Type>::length()
{
    return count;
}
 
template<class Type>
void linkedListType<Type>::destroyList()
{
    nodeType<Type> *temp;
    while(first != NULL)
    {
        temp = first;
        first = first->link;
        delete temp;
    }
    last = NULL; 
    count = 0;
}
 
template<class Type>
Type linkedListType<Type>::front()
{
    assert(last != NULL);
    return first->info;
}
 
template<class Type>
Type linkedListType<Type>::back()
{
    assert(last != NULL);
    return last->info;
}
 
template<class Type>
bool linkedListType<Type>::search(const Type& searchItem)
{
    nodeType<Type> *current;
    bool found;
    current = first;
    found = false;
    while(current != NULL && !found)
        if(current->info == searchItem)
              found = true;
        else
              current = current->link;
    return found;
}
 
template<class Type>
void linkedListType<Type>::insertFirst(const Type& newItem)
{
    nodeType<Type> *newNode;
    newNode = new nodeType<Type>;
    assert(newNode != NULL);
    newNode->info = newItem;
    newNode->link = first;
    first = newNode;
    count++;
    if(last == NULL)
        last = newNode;
}
 
template<class Type>
void linkedListType<Type>::insertLast(const Type& newItem)
{
    nodeType<Type> *newNode;
    newNode = new nodeType<Type>;
    assert(newNode != NULL);
    newNode->info = newItem;
    newNode->link = NULL;
    if(first == NULL)
    {
        first = newNode;
        last = newNode;
    }
    else
    {
        last->link = newNode;
        last = newNode;
    }
    count++;
}
 
template<class Type>                               
void linkedListType<Type>::deleteNode(const Type& deleteItem)
{
    nodeType<Type> *current;       // pointer to traverse the list
    nodeType<Type> *trailCurrent;  //pointer just before current
    bool found;
    if(first == NULL)
        cout<<"Cannot delete from an empty list."<<endl;
    else
    {
        if(first->info == deleteItem)
        {
            current = first;
            first = first->link;
            count--;
            if(first == NULL)  // list has only one node
                last = NULL;
            delete current;
        }
        else // search the list for the node with the given info
        {
            found = false;
            trailCurrent = first;
            current = first->link;
            while(current != NULL && !found)
            {
                if(current->info != deleteItem)
                {
                    trailCurrent = current;
                    current = current->link;
                }
                else
                    found = true;
            }
            if(found)
            {
                trailCurrent->link = current->link;
                count--;
                if(last == current) //node to be deleted is the last node
                    last = trailCurrent;
                delete current;
            }
            else
                cout<<"Item to be deleted is not in the list."<<endl;
        }
    }
}
 
template<class Type>
linkedListType<Type>::linkedListType()
{
    first = NULL;
    last = NULL;
    count = 0;
}
 
template<class Type>
linkedListType<Type>::linkedListType(const linkedListType<Type>& otherList)
{
    first = NULL;
    copyList(otherList);
}
 
template<class Type>
linkedListType<Type>::~linkedListType()
{
    destroyList();
}
 
template<class Type>
void linkedListType<Type>::copyList(const linkedListType<Type>& otherList)
{
    nodeType<Type> *newNode;  //pointer to create a node
    nodeType<Type> *current;   //pointer to traverse the list
    if(first != NULL)
        destroyList();
    if(otherList.first == NULL)  //otherList is empty
    {
        first = NULL;
        last = NULL;
        count = 0;
    }
    else
    {
        current = otherList.first;  //current points to the lis to be copied
        count = otherList.count;
        first = new nodeType<Type>; //create the node
        assert(first != NULL);
        first->info = current->info; //copy the info
        first->link = NULL;     //set the link field of the node to NULL
        last = first;            //make last point to the first node
        current = current->link;  //make current point to the next node
        while(current != NULL) //copy the remaining list
        {
            newNode = new nodeType<Type>; //create a node
            assert(newNode != NULL);
            newNode->info = current->info; //copy the info
            newNode->link = NULL;   //set the link of newNode to NULL
            last->link = newNode;     //attach newNode after last
            last = newNode;       //make last point to the actual last node
            current = current->link;   //make current point to the next node
        }
    }
}
 
template<class Type>
bool linkedListType<Type>::print()
{
    nodeType<Type> *current;
    if(first == NULL)
    {
        cout<<"Cannot retrieve in an empty list."<<endl<<endl;
        return false;
    }
    else
    {
        current = first;
        for(int i=0; i<count; i++)
        {       cout << " " << current->info << " "; 
                current = current->link;
  }
        return true;
    }
}
 
template<class Type>
bool linkedListType<Type>::replaceAt(int position, const Type& newItem)
{
    nodeType<Type> *current;
    if(first == NULL)
    {
        cout<<"\n Cannot replace in an empty list."<<endl<<endl;
        return false;
    }
    else if(position < 0 || position > count)
    {
        cout<<"\n Invalid position."<<endl<<endl;
        return false;
    }
    else
    {
        current = first;
        for(int i=0; i<position; i++)
            current = current->link;
        current->info = newItem;
        return true;
    }
}
 
template <class Type>
bool linkedListType<Type>::insertAt(int position ,const Type &Item)
{
     if(position < 0 || position > count )
     {
          cerr << "\n Invalid Position \n" << endl;
          return false ;
     }
    
     nodeType <Type> *current , *newNode ;
     newNode = new nodeType <Type>;
     assert (newNode != NULL);
    
     newNode -> info = Item ;
     newNode -> link = NULL ;
     if ( first == NULL)
     {
          first = newNode ;
          last = newNode;
          count++;
          return true ;
          }
         
     else if ( position == 0 )
     {
          newNode -> link = first ;
          first = newNode ;
          count++;
          return true;
     }
    
     else if ( position == count )
     {
          last->link = newNode ;
          last = newNode;
          count++;
          return true ;
     }
    
     current = first ;
     for (int i = 0 ; i <position-1 ; ++i)
     current = current -> link ;
    
         
}
 
 
 
template<class Type>
struct nodeType
{
    Type info;
    nodeType<Type> *link;
};
 
template<class Type>
class linkedListType
{
public:
    const linkedListType<Type>& operator=(const linkedListType<Type>&);
    void initializeList();
    bool isEmpty();
    int length();
    void destroyList();
    Type front();
    Type back();
    bool search(const Type& searchItem);
    void insertFirst(const Type& newItem);
    void insertLast(const Type& newItem);
    void deleteNode(const Type& deleteItem);   
    linkedListType();
    linkedListType(const linkedListType<Type>& otherList);
    ~linkedListType();
    bool print() ;
    bool replaceAt(int position, const Type& newItem);
    bool insertAt(int position ,const Type &Item) ;
    bool retrieveAt(int pos,Type & item) ;
    void insertArray(const Type array[],int size) ;
//protected:
    int count;
    nodeType<Type> *first;
    nodeType<Type> *last;
 
private:
    void copyList(const linkedListType<Type>& otherList);
};
 
template<class Type>
ostream& operator<<(ostream&, const linkedListType<Type>&);
 
 
 
 
 
 
 
 
 
 
 
template<class Type>
ostream& operator<<(ostream& osObject, const linkedListType<Type>& list)
{
    nodeType<Type> *current; // pointer to traverse the list
    current = list.first;
    while(current != NULL)
    {
        osObject<<current->info<<" ";
        current = current->link;
    }
    return osObject;
}
 
template<class Type>
const linkedListType<Type>& linkedListType<Type>::operator=(const linkedListType<Type>& otherList)
{
    if(this != &otherList) //avoid self-copy
        copyList(otherList);
    return *this;
}
 
template<class Type>
void linkedListType<Type>::initializeList()
{
    destroyList();
}
 
 
template<class Type>
bool linkedListType<Type>::isEmpty()
{
    return(first == NULL);
}
 
template<class Type>
int linkedListType<Type>::length()
{
    return count;
}
 
template<class Type>
void linkedListType<Type>::destroyList()
{
    nodeType<Type> *temp;
    while(first != NULL)
    {
        temp = first;
        first = first->link;
        delete temp;
    }
    last = NULL; 
    count = 0;
}
 
template<class Type>
Type linkedListType<Type>::front()
{
    assert(last != NULL);
    return first->info;
}
 
template<class Type>
Type linkedListType<Type>::back()
{
    assert(last != NULL);
    return last->info;
}
 
template<class Type>
bool linkedListType<Type>::search(const Type& searchItem)
{
    nodeType<Type> *current;
    bool found;
    current = first;
    found = false;
    while(current != NULL && !found)
        if(current->info == searchItem)
              found = true;
        else
              current = current->link;
    return found;
}
 
template<class Type>
void linkedListType<Type>::insertFirst(const Type& newItem)
{
    nodeType<Type> *newNode;
    newNode = new nodeType<Type>;
    assert(newNode != NULL);
    newNode->info = newItem;
    newNode->link = first;
    first = newNode;
    count++;
    if(last == NULL)
        last = newNode;
}
 
template<class Type>
void linkedListType<Type>::insertLast(const Type& newItem)
{
    nodeType<Type> *newNode;
    newNode = new nodeType<Type>;
    assert(newNode != NULL);
    newNode->info = newItem;
    newNode->link = NULL;
    if(first == NULL)
    {
        first = newNode;
        last = newNode;
    }
    else
    {
        last->link = newNode;
        last = newNode;
    }
    count++;
}
 
template<class Type>                               
void linkedListType<Type>::deleteNode(const Type& deleteItem)
{
    nodeType<Type> *current;       // pointer to traverse the list
    nodeType<Type> *trailCurrent;  //pointer just before current
    bool found;
    if(first == NULL)
        cout<<"Cannot delete from an empty list."<<endl;
    else
    {
        if(first->info == deleteItem)
        {
            current = first;
            first = first->link;
            count--;
            if(first == NULL)  // list has only one node
                last = NULL;
            delete current;
        }
        else // search the list for the node with the given info
        {
            found = false;
            trailCurrent = first;
            current = first->link;
            while(current != NULL && !found)
            {
                if(current->info != deleteItem)
                {
                    trailCurrent = current;
                    current = current->link;
                }
                else
                    found = true;
            }
            if(found)
            {
                trailCurrent->link = current->link;
                count--;
                if(last == current) //node to be deleted is the last node
                    last = trailCurrent;
                delete current;
            }
            else
                cout<<"Item to be deleted is not in the list."<<endl;
        }
    }
}
 
template<class Type>
linkedListType<Type>::linkedListType()
{
    first = NULL;
    last = NULL;
    count = 0;
}
 
template<class Type>
linkedListType<Type>::linkedListType(const linkedListType<Type>& otherList)
{
    first = NULL;
    copyList(otherList);
}
 
template<class Type>
linkedListType<Type>::~linkedListType()
{
    destroyList();
}
 
template<class Type>
void linkedListType<Type>::copyList(const linkedListType<Type>& otherList)
{
    nodeType<Type> *newNode;  //pointer to create a node
    nodeType<Type> *current;   //pointer to traverse the list
    if(first != NULL)
        destroyList();
    if(otherList.first == NULL)  //otherList is empty
    {
        first = NULL;
        last = NULL;
        count = 0;
    }
    else
    {
        current = otherList.first;  //current points to the lis to be copied
        count = otherList.count;
        first = new nodeType<Type>; //create the node
        assert(first != NULL);
        first->info = current->info; //copy the info
        first->link = NULL;     //set the link field of the node to NULL
        last = first;            //make last point to the first node
        current = current->link;  //make current point to the next node
        while(current != NULL) //copy the remaining list
        {
            newNode = new nodeType<Type>; //create a node
            assert(newNode != NULL);
            newNode->info = current->info; //copy the info
            newNode->link = NULL;   //set the link of newNode to NULL
            last->link = newNode;     //attach newNode after last
            last = newNode;       //make last point to the actual last node
            current = current->link;   //make current point to the next node
        }
    }
}
 
template<class Type>
bool linkedListType<Type>::print()
{
    nodeType<Type> *current;
    if(first == NULL)
    {
        cout<<"Cannot retrieve in an empty list."<<endl<<endl;
        return false;
    }
    else
    {
        current = first;
        for(int i=0; i<count; i++)
        {       cout << " " << current->info << " "; 
                current = current->link;
  }
        return true;
    }
}
 
template<class Type>
bool linkedListType<Type>::replaceAt(int position, const Type& newItem)
{
    nodeType<Type> *current;
    if(first == NULL)
    {
        cout<<"\n Cannot replace in an empty list."<<endl<<endl;
        return false;
    }
    else if(position < 0 || position > count)
    {
        cout<<"\n Invalid position."<<endl<<endl;
        return false;
    }
    else
    {
        current = first;
        for(int i=0; i<position; i++)
            current = current->link;
        current->info = newItem;
        return true;
    }
}
 
template <class Type>
bool linkedListType<Type>::insertAt(int position ,const Type &Item)
{
     if(position < 0 || position > count )
     {
          cerr << "\n Invalid Position \n" << endl;
          return false ;
     }
    
     nodeType <Type> *current , *newNode ;
     newNode = new nodeType <Type>;
     assert (newNode != NULL);
    
     newNode -> info = Item ;
     newNode -> link = NULL ;
     if ( first == NULL)
     {
          first = newNode ;
          last = newNode;
          count++;
          return true ;
          }
         
     else if ( position == 0 )
     {
          newNode -> link = first ;
          first = newNode ;
          count++;
          return true;
     }
    
     else if ( position == count )
     {
          last->link = newNode ;
          last = newNode;
          count++;
          return true ;
     }
    
     current = first ;
     for (int i = 0 ; i <position-1 ; ++i)
     current = current -> link ;
    
         
}
 
 
#include<iostream>
using namespace std;
 
template<class Type>
struct nodeType
{
    Type info;
    nodeType<Type> *link;
};
 
template<class Type>
class linkedListType
{
public:
    const linkedListType<Type>& operator=(const linkedListType<Type>&);
    void initializeList();
    bool isEmpty();
    int length();
    void destroyList();
    Type front();
    Type back();
    bool search(const Type& searchItem);
    void insertFirst(const Type& newItem);
    void insertLast(const Type& newItem);
    void deleteNode(const Type& deleteItem);   
    linkedListType();
    linkedListType(const linkedListType<Type>& otherList);
    ~linkedListType();
    bool print() ;
    bool replaceAt(int position, const Type& newItem);
    bool insertAt(int position ,const Type &Item) ;
    bool retrieveAt(int pos,Type & item) ;
    void insertArray(const Type array[],int size) ;
//protected:
    int count;
    nodeType<Type> *first;
    nodeType<Type> *last;
 
private:
    void copyList(const linkedListType<Type>& otherList);
};
 
template<class Type>
ostream& operator<<(ostream&, const linkedListType<Type>&);
 
 
 
#include<cassert>
#include <iostream>
 
using namespace std ;
 
 
 
template<class Type>
ostream& operator<<(ostream& osObject, const linkedListType<Type>& list)
{
    nodeType<Type> *current; // pointer to traverse the list
    current = list.first;
    while(current != NULL)
    {
        osObject<<current->info<<" ";
        current = current->link;
    }
    return osObject;
}
 
template<class Type>
const linkedListType<Type>& linkedListType<Type>::operator=(const linkedListType<Type>& otherList)
{
    if(this != &otherList) //avoid self-copy
        copyList(otherList);
    return *this;
}
 
template<class Type>
void linkedListType<Type>::initializeList()
{
    destroyList();
}
 
 
template<class Type>
bool linkedListType<Type>::isEmpty()
{
    return(first == NULL);
}
 
template<class Type>
int linkedListType<Type>::length()
{
    return count;
}
 
template<class Type>
void linkedListType<Type>::destroyList()
{
    nodeType<Type> *temp;
    while(first != NULL)
    {
        temp = first;
        first = first->link;
        delete temp;
    }
    last = NULL; 
    count = 0;
}
 
template<class Type>
Type linkedListType<Type>::front()
{
    assert(last != NULL);
    return first->info;
}
 
template<class Type>
Type linkedListType<Type>::back()
{
    assert(last != NULL);
    return last->info;
}
 
template<class Type>
bool linkedListType<Type>::search(const Type& searchItem)
{
    nodeType<Type> *current;
    bool found;
    current = first;
    found = false;
    while(current != NULL && !found)
        if(current->info == searchItem)
              found = true;
        else
              current = current->link;
    return found;
}
 
template<class Type>
void linkedListType<Type>::insertFirst(const Type& newItem)
{
    nodeType<Type> *newNode;
    newNode = new nodeType<Type>;
    assert(newNode != NULL);
    newNode->info = newItem;
    newNode->link = first;
    first = newNode;
    count++;
    if(last == NULL)
        last = newNode;
}
 
template<class Type>
void linkedListType<Type>::insertLast(const Type& newItem)
{
    nodeType<Type> *newNode;
    newNode = new nodeType<Type>;
    assert(newNode != NULL);
    newNode->info = newItem;
    newNode->link = NULL;
    if(first == NULL)
    {
        first = newNode;
        last = newNode;
    }
    else
    {
        last->link = newNode;
        last = newNode;
    }
    count++;
}
 
template<class Type>                               
void linkedListType<Type>::deleteNode(const Type& deleteItem)
{
    nodeType<Type> *current;       // pointer to traverse the list
    nodeType<Type> *trailCurrent;  //pointer just before current
    bool found;
    if(first == NULL)
        cout<<"Cannot delete from an empty list."<<endl;
    else
    {
        if(first->info == deleteItem)
        {
            current = first;
            first = first->link;
            count--;
            if(first == NULL)  // list has only one node
                last = NULL;
            delete current;
        }
        else // search the list for the node with the given info
        {
            found = false;
            trailCurrent = first;
            current = first->link;
            while(current != NULL && !found)
            {
                if(current->info != deleteItem)
                {
                    trailCurrent = current;
                    current = current->link;
                }
                else
                    found = true;
            }
            if(found)
            {
                trailCurrent->link = current->link;
                count--;
                if(last == current) //node to be deleted is the last node
                    last = trailCurrent;
                delete current;
            }
            else
                cout<<"Item to be deleted is not in the list."<<endl;
        }
    }
}
 
template<class Type>
linkedListType<Type>::linkedListType()
{
    first = NULL;
    last = NULL;
    count = 0;
}
 
template<class Type>
linkedListType<Type>::linkedListType(const linkedListType<Type>& otherList)
{
    first = NULL;
    copyList(otherList);
}
 
template<class Type>
linkedListType<Type>::~linkedListType()
{
    destroyList();
}
 
template<class Type>
void linkedListType<Type>::copyList(const linkedListType<Type>& otherList)
{
    nodeType<Type> *newNode;  //pointer to create a node
    nodeType<Type> *current;   //pointer to traverse the list
    if(first != NULL)
        destroyList();
    if(otherList.first == NULL)  //otherList is empty
    {
        first = NULL;
        last = NULL;
        count = 0;
    }
    else
    {
        current = otherList.first;  //current points to the lis to be copied
        count = otherList.count;
        first = new nodeType<Type>; //create the node
        assert(first != NULL);
        first->info = current->info; //copy the info
        first->link = NULL;     //set the link field of the node to NULL
        last = first;            //make last point to the first node
        current = current->link;  //make current point to the next node
        while(current != NULL) //copy the remaining list
        {
            newNode = new nodeType<Type>; //create a node
            assert(newNode != NULL);
            newNode->info = current->info; //copy the info
            newNode->link = NULL;   //set the link of newNode to NULL
            last->link = newNode;     //attach newNode after last
            last = newNode;       //make last point to the actual last node
            current = current->link;   //make current point to the next node
        }
    }
}
 
template<class Type>
bool linkedListType<Type>::print()
{
    nodeType<Type> *current;
    if(first == NULL)
    {
        cout<<"Cannot retrieve in an empty list."<<endl<<endl;
        return false;
    }
    else
    {
        current = first;
        for(int i=0; i<count; i++)
        {       cout << " " << current->info << " "; 
                current = current->link;
  }
        return true;
    }
}
 
template<class Type>
bool linkedListType<Type>::replaceAt(int position, const Type& newItem)
{
    nodeType<Type> *current;
    if(first == NULL)
    {
        cout<<"\n Cannot replace in an empty list."<<endl<<endl;
        return false;
    }
    else if(position < 0 || position > count)
    {
        cout<<"\n Invalid position."<<endl<<endl;
        return false;
    }
    else
    {
        current = first;
        for(int i=0; i<position; i++)
            current = current->link;
        current->info = newItem;
        return true;
    }
}
 
template <class Type>
bool linkedListType<Type>::insertAt(int position ,const Type &Item)
{
     if(position < 0 || position > count )
     {
          cerr << "\n Invalid Position \n" << endl;
          return false ;
     }
    
     nodeType <Type> *current , *newNode ;
     newNode = new nodeType <Type>;
     assert (newNode != NULL);
    
     newNode -> info = Item ;
     newNode -> link = NULL ;
     if ( first == NULL)
     {
          first = newNode ;
          last = newNode;
          count++;
          return true ;
          }
         
     else if ( position == 0 )
     {
          newNode -> link = first ;
          first = newNode ;
          count++;
          return true;
     }
    
     else if ( position == count )
     {
          last->link = newNode ;
          last = newNode;
          count++;
          return true ;
     }
    
     current = first ;
     for (int i = 0 ; i <position-1 ; ++i)
     current = current -> link ;
    
         
}
 
 
 
template<class Type>
struct nodeType
{
    Type info;
    nodeType<Type> *link;
};
 
template<class Type>
class linkedListType
{
public:
    const linkedListType<Type>& operator=(const linkedListType<Type>&);
    void initializeList();
    bool isEmpty();
    int length();
    void destroyList();
    Type front();
    Type back();
    bool search(const Type& searchItem);
    void insertFirst(const Type& newItem);
    void insertLast(const Type& newItem);
    void deleteNode(const Type& deleteItem);   
    linkedListType();
    linkedListType(const linkedListType<Type>& otherList);
    ~linkedListType();
    bool print() ;
    bool replaceAt(int position, const Type& newItem);
    bool insertAt(int position ,const Type &Item) ;
    bool retrieveAt(int pos,Type & item) ;
    void insertArray(const Type array[],int size) ;
//protected:
    int count;
    nodeType<Type> *first;
    nodeType<Type> *last;
 
private:
    void copyList(const linkedListType<Type>& otherList);
};
 
template<class Type>
ostream& operator<<(ostream&, const linkedListType<Type>&);
 
 
 
 
int main()
{
    int item ;
    linkedListType<int>list1,list2 ;
    list1.insertLast(11) ;
    list1.insertLast(9) ;
    list1.insertLast(7) ;
    list1.insertLast(20) ;
    list1.insertLast(3) ;
    list1.insertLast(6) ;
 
    list1.print();
   
    cout<<endl;
    list2.replaceAt(0,100);
    list1.replaceAt(-1,100);
    list1.replaceAt(2,100);
   
    cout<<"\nList 1 : ";
    list1.print();
   
    list1.insertAt( -2 , 6 );
    list1.insertAt( 7 , -1 );
    list1.insertAt( 6 , 200 );
    list2.insertAt( 0 , -1 );
   
    cout <<"\nList 1 : " ;
    list1.print();
  
    cout <<"\nList 2 : ";
    list2.print();
   
  cout <<endl <<endl; 
  system("pause");
  return 0 ;
}
 
عودة
أعلى أسفل