الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

الاضافة من الوسط

مغلق
بدأه aimn mohamed في 1 يناير 2008 · 1 رد · 448 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

ياجماعة عندي برنامج يحفظ بيانات الموظفين

طبعا انا خليت الاضفة تكون من النهاية

طلب مننا الدكتور نعيد تكوين الكون بحيث تكون الاضفة من البداية ومن الوسط

الاضافة من البداية سويتها ومشى الحال الحمد لله وكان هذا الكود

this_ptr->next=start
this_ptr=start

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

 #include<iostream.h>
#include<conio.h>
#include<string.h>
#include<memory.h>
#include<stdlib.h>
typedef struct employee_type
{
int code;
char name[25];
char department[15];
float salary;
struct employee_type *next;
}
node;
node *start,*last_ptr;
void input_inf(void);
void display_inf(void);
void delete_inf(void);
int main()
{
int option;
clrscr();
start=last_ptr=(node*)NULL;
while (1)
{
cout<<"\n Select one of the following options:";
cout<<"\n ____________________________________"<<endl;
cout<<"\n   1    Enter new Employee :";
cout<<"\n   2    Display list of Employee:";
cout<<"\n   3    Exit from program :";
cout<<"\n\n Enter your Option (1-3):";
cin>>option;
switch(option)
{
case 1: input_inf() ;break;
case 2: display_inf() ;break;
case 3: delete_inf; exit(1);
}
}
}
// To Add Employee to list
void input_inf(void)
{
node *this_ptr;
int mcode;
char mname[25];
char mdepartment[15];
float msalary;
//input variable to the structure employee_type or node
cout<<"\n Enter employee's code:";  cin>>mcode;
cout<<"\n Enter employee's name:";  cin>>mname;
cout<<"\n Enter employee's department:";    cin>>mdepartment;
cout<<"\n Enter employee's salary:";       cin>>msalary;
this_ptr=(node*)malloc(sizeof(node));
this_ptr->code=mcode;
strcpy(this_ptr->name,mname);
strcpy(this_ptr->department,mdepartment);
this_ptr->salary=msalary;
this_ptr->next=(node*)NULL;
if(start==(node*)NULL)
start=last_ptr=this_ptr;
else
{last_ptr->next=this_ptr;
last_ptr=this_ptr;}
}
void display_inf(void)
{
int i=1;
node *this_ptr;
if(start==(node*)NULL)
{cout<<"\n list is Empty......";
cout<<"\n press any key to continue";
getch();
return;
}
cout<<"\n List of Employee\n\n";
for(this_ptr=start;this_ptr!=(node*)NULL;this_ptr=this_ptr->next)
{
cout<<"\n Employee's code:      "<<i;
cout<<"\n Eployee's code:        "<<this_ptr->code;
cout<<"\n Employee's name:      "<<this_ptr->name;
cout<<"\n Employee's department: "<<this_ptr->department;
cout<<"\n Employee's salary:     "<<this_ptr->salary;
cout<<"\n\n";
cout<<"\n Press any key to continue";
getch();
i++;
}
}
void delete_inf(void)
{node *this_ptr;
while(start!=(node*)NULL)
{
this_ptr=start;
start=start->next;
free(this_ptr);
}
}
#2

السلام عليكم ,,

يمكنك حساب عدد الـ nodes و تقسيمها على 2 للحصول على العنصر الذي في الوسط,

إذا حصلت سيكون شكل الـ Linked List عندك قبل الإضافة كالتالي :

Node(1) -> Node(Middle) -> Node(3)

كل ما عليك هو إنشاء node جديدة و جعل الـ node التي في الوسط تؤشر عليها, بعد ذلك من الـ node الجديد قم بالتأشير على الـ node رقم 3 كما في المثال و ستحل المشكلة بإذن الله :

Node(1) -> Node(Middle) -> (NewNode) - > Node(3)

تحياتي ,,

هذا الموضوع مغلق.

مواضيع مشابهة