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

arrays

مغلق
بدأه العبسى في 22 أبريل 2002 · 2 رد · 386 مشاهدة · في ارشيف قسم C/C++
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

I wrote a program to do the following:

Write a destructor to return the dynamic memory used by the list to the heap.

Write a function which returns the number of items in the list.

Write a function which returns true if the list is full.

Write a function to add one item to the list. The argument of the function contains the item to be added.

Write a function to remove all of the items from the list.

Write a function to print the contents of the list to a report.

write a function to display the contents of the list to the display.

i woul like to write a small program in main to test the array list.

using namespace std;

int main()

{

List a(1);

a.add(1);

return 0;

}

#include

#include

#include

#include

List::List(int max)

{

int current_length=0;

typedef int*item;

item a;

a = new int[max];

if ( a==NULL)

{

cout<< "Error : Insufficient memory.n";

exit(1);

}

Max_number=max;

}

int List::length ()

{

return(current_length);

}

void List::add (int new_item)

{

cout<<"Enter a new item to the list: "<

cin>>current_length;

if(a.full())

{

cout<<"Error:adding to a full list."<

exit(1);

}

else

{

a[current_length]=new_item;

current_length=current_length+1;

}

}

int List::full()

{

return(current_length==Max_number);

}

void List::erase ()

{

current_length=0;

}

List::~List()

{

delete[] a;

}

i want to add items to the list and test it in main but still I have error message which i do not know why please i need help?

#2

Hai Mr. Abssy..

I don't know how did u write that code... And how did u write the implementation of the class members without even declaring the class..

I corrected the code to compile but I am not sure if it's what the instructor asked u..

#include 
#include
#include
#include
using namespace std; 

typedef int *item; 

class List {
public:
	List(int max);
	~List();

	int length ();
	void add (int new_item);
	int full();
	void erase ();

private:
	int Max_number;
	int current_length;
	item a; 
};

int main() 
{ 

	List a(1); 
	a.add(1); 


	return 0; 

} 

List::List(int max) 
{ 
	int current_length=0; 

	a = new int[max]; 
	if ( a==NULL) 
	{ 
		cout<< "Error : Insufficient memory.n"; 
		exit(1); 
	} 
	Max_number=max; 
} 

int List::length () 
{ 

	return(current_length); 

} 

void List::add (int new_item) 
{ 
	cout<<"Enter a new item to the list: "<	cin>>current_length; 

	if(full()) 
	{ 
		cout<<"Error:adding to a full list."<		exit(1); 
	} 
	else 
	{ 

		a[current_length]=new_item; 
		current_length=current_length+1; 
	} 

} 

int List::full() 
{ 
	return(current_length==Max_number); 
} 

void List::erase () 
{ 
	current_length=0; 
} 


List::~List() 
{ 	
	delete[] a; 
}

Coding on the Cloud and for the Cloud!

My Blog

#3

#include

#include

typedef struct node

{

char last_name[15],first_name[15];

char origin[15],detin[15];

char flight_no[9],seat_no[15];

struct node *link;

} *passenger;

/*====================================*/

void insertlist(passenger *h)

{

passenger v,t;

t = (passenger) malloc (sizeof(struct node));

printf("nenter first name ");

scanf("%s",t->first_name);

printf("nenter last name ");

scanf("%s",t->last_name);

printf("nenter origin ");

scanf("%s",t->origin);

printf("nenter detination ");

scanf("%s",t->detin);

printf("nenter flight_no ");

scanf("%s",t->flight_no);

printf("nenter seat_no ");

scanf("%s",t->seat_no);

t->link=NULL;

if (*h==NULL)

*h=t;

else

{

for (v=*h;v->link!=NULL;v=v->link);

v->link=t;

}

}

/*====================================*/

void deletelist(passenger *h)

{

passenger t,v;

for (t=*h;t->link->link!=NULL;t=t->link);

v=t->link;

t->link=v->link;

}

/*====================================*/

void printlist(passenger h)

{

printf("n");

printf("first_name last_name origin detination flight_no seat_no");

printf("n");

while (h!=NULL)

{

printf(" %s",h->first_name);

printf(" ");

printf(" %s",h->last_name);

printf(" ");

printf(" %s",h->origin);

printf(" ");

printf("%s",h->detin);

printf(" ");

printf(" %s",h->flight_no);

printf(" ");

printf(" %s",h->seat_no);

printf("n");

h=h->link;

}

}

/*====================================*/

int menu(void)

{

int n;

printf("n(1) Insert a new last node");

printf("n(2) Delete the last node");

printf("n(3) Print the list");

printf("n(4) edit the node");

printf("n(5) end of program");

printf("nENTER a number:");

scanf("%d",&n);

return n;

}

/*====================================*/

void edit_node(passenger *h)

{

char f,l,s;

passenger v;

v = (passenger) malloc (sizeof(struct node));

printf("nenter first name ");

scanf("%s",&f);

printf("nenter last name ");

scanf("%s",&l);

for (v=*h;v->link!=NULL;v=v->link);

{

if(v->first_name == f || v->last_name ==l)

{

printf(" %s",v->first_name);

printf(" ");

printf(" %s",v->last_name);

printf(" ");

printf(" %s",v->origin);

printf(" ");

printf("%s",v->detin);

printf(" ");

printf(" %s",v->flight_no);

printf(" ");

printf(" %s",v->seat_no);

printf("ndo you wont edit this node (y/n)");

scanf("%c",&s);

if (s=='y')

{

printf("nenter first name ");

scanf("%s",v->first_name);

printf("nenter last name ");

scanf("%s",v->last_name);

printf("nenter origin ");

scanf("%s",v->origin);

printf("nenter detination ");

scanf("%s",v->detin);

printf("nenter flight_no ");

scanf("%s",v->flight_no);

printf("nenter seat_no ");

scanf("%s",v->seat_no);

}

else if(s=='n')

break;

}

}

printf("n not found node");

}

int main()

{

int c;

passenger s=NULL;

while ((c=menu())!=5)

{

switch ©

{

case 1:

insertlist(&s);break;

case 2:

deletelist(&s);break;

case 3:

printlist(s);break;

case 4:

edit_node(&s);

}

}

return 0;

}

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

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