مساعد في كتابة برنامج القوائم المترابطة Linked List.
عندي خطأ في عرض القائمه بعد ما ادخل رقم في نهاية القائمه.
مع جزيل الشكر
#include <iostream>
#include <conio.h>
using namespace std;
struct my_list
{
int num;
struct my_list *next;
struct my_list *previos;
};
typedef struct my_list node;
void createlist(node *p)
{
int data;
node *first,*temp;
first = p;
for( ; ; )
{
cout<<"Enter Number: ";
cin>>data;
if(data == 0)
{
p->next = NULL;
break;
}
else
{
p->num = data;
if(first == p)
{
p->previos = NULL;
}
else
{
p->previos = temp;
}
p->next = new node;
temp = p;
p = p->next;
}
}
}
void display(node *p)
{
while(p->next != NULL)
{
cout<<p->num<<endl;
p = p->next;
}
}
int countlist(node *p)
{
int count = 0;
while(p->next !=NULL)
{
count++;
p = p->next;
}
return count;
}
void insertatend(node *p)
{
node *temp2;
node *temp = new node;
cout<<"\nEnter number at end: ";
cin>> temp->num;
temp->next = NULL;
if (p ==NULL)
{
p = temp;
}
else
{
temp2=p;
while(temp2->next !=NULL)
temp2 = temp2->next;
temp2->next = temp;
}
}
void sort(node *p)
{
node *p1,*p2;
int i,j,temp,n;
n = countlist(p);
for(i = 1; i<n; i++)
{
p1 = p;
p2 = p->next;
for(j = 1; j<=(n-i); j++)
{
if(p1->num > p2->num)
{
temp = p2->num;
p2->num = p1->num;
p1->num = temp;
}
p1 = p1->next;
p2 = p2->next;
}
}
}
void main()
{
node *mynodes = new node;
cout<<"\nCreate list:\n";
createlist(mynodes);
cout<<"\nDisplay the list:\n";
display(mynodes);
cout<<"\n\nInsert new:\n";
insertatend(mynodes);
cout<<"\nDisplay all after insert:\n";
display(mynodes);
cout<<"\nSort list:\n";
sort(mynodes);
cout<<"\nDisplay after sort :\n";
display(mynodes);
getch();
}

