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

emample of simple linked list

مغلق
بدأه CoMPuTeR-Q8 في 11 يوليو 2006 · 2 رد · 1,005 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام

هذا برنامج عن simple linked list with head node ليستفيد منه الأعضاء

ماذا اقصد simple linked list with head node

هذا مثال عليها

10<-4<-3<-11<-1-<5

اول عصر من list من اليسار الذي هو رقم 5 يحتوي على عدد عناصر list ويعتبر head node of list وعند الطباعه لايطبع head node

شرح البرنامج ؟

=======

طريقة ادخال input تكون عبر ملف اسمه hw03.txt نقوم بوضع الأوامر التي نريدها ويقوم البرنامج بالعمليات التاليه :

======================================================

a x

which main add integer x to list

d y

which mean delete integer y from list

p

which mean print the current list

s

which mean print the summation of current list

c

which mean print number of element in list

n

which mean create new list

g z

which mean create list of first z prime number

e.g

when z=4

the list is

7<-5<-3<-2<-4

وهذا مثال للأوامر التي تكون داخل الملف hw03.txt

========================

a 3

ادخل الرقم 3 داخل list في نهاية list واذا كانت موجود لاتدخله

d 3

احذف العنصر 3 من list واذا لم يكن موجود اطبع مسج انه غير موجود

p

اطبع list

s

اطبع مجموع العناصر داخل list

c

اطبع عدد العناصر داخل list

q

خروج من البرنامج

g 3

ادخل اول ثلاث عناصر تكون prime وتكون بدايه من رقم 2

n

أنشأ خليه جديده

وحبيت اقولكم على شغلتين :

=============

1-قبل بداية البرنامج يحب ان أدخل على while loop يجب ان انشأ خليه جديده لتنفيذ عليها عمليات مختلفه.

2-عند القيام بادخال n or g 3يجب مسح جميع محتويات الlist سابقه بواسطة الداله deleteAllList

مثال على ذلك:

عندما نقرا من الملف hw03.txt الاوامر التاليه

a 1

a 2

a 3

هناك تتكون لدينا list من 3 عناصر كالتالي

3<-2<-1<-3 حيث الرقم 3 من اليسار هو الذي يحتوي على عدد عناصر list

وعندما ادخل بعد a 3

g 3

تختفي محتويات list السابقه وتتكون لدينا الجديده 5<-3<-2<-3 حيث تحتوي على اول 3 ارقام أوليه( prime)

وكذلك ينطبق الحال عدنما ادخل n.

وفي شفله بعد

وهي موجوده بالبرنامج

int AC=0; // how many times allocate was called.

int DC=0; // how many times deallocate() was called.

هالعنصرين يحسبون جم سوينا new & delete بالبرنامج في نهاية البرنامج ينطبعون

وكل مانريد نعمل new or delete بالبرنامج تكون عبر الدالتين التاليتين:

deallocate for delete operation

allocate for new operation

وأي سؤال انا حاضر للشرح

يلا

سلملم ;)

CoMPuTeR_Q8.zip

#2
/***************************************************************
Program describtion :
=====================
This program will maintains and operates on a simple linked list 
of integers with jead node, the program read instruction from 
text file hw03.txt, each instruction is in a separate line.

Created by :
=============
Name : CoMPuTeRQ8
MaJor : CoMPuTeR-SCIeNCe
Kuwait University
*****************************************************************/

/****************************************************************
Necessary Header files used in program.
*****************************************************************/

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <conio.h>

/****************************************************************
data structure used in program.
*****************************************************************/

struct Node {
	   int data;
	   Node *next;
	   };

/****************************************************************
Function Prototype.
*****************************************************************/

void openFile						   ();
void printList						  (Node *);
Node *startNewList					  ();
void addInteger						 (Node *,Node *,Node *,int);
int  deleteInteger					  (Node *,int);
int  sumOfElement					   (Node *);
void numberOfElement					(Node *);
void startNewListMadeOfPrimeNumber	  (Node *,int,int&,int);
Node *allocate						  ();
void deallocate						 (Node *);
int  isPrime							(int,int);
void deleteAllList					  (Node *);

/****************************************************************
File pointer for accessing the file & global variables.
*****************************************************************/

FILE *fp;			   // fp = hw03.txt file pointer.

int AC=0;			   // how many times allocate was called.
int DC=0;			   // how many times deallocate() was called.

int main() {

	Node  *head=NULL;	 // head of list of integers.
	char  instruction;	// instruction name.
	int   x;			  // integer will added to list.
	int   y;			  // integer will deleted from list.
	int   z;			  // first z prime # will added to list.
	int   k=1;			// index variable to access prime #.


	openFile();					  

	fscanf(fp,"%c",&instruction);   // read instruction.

	head=startNewList();

	while (!feof(fp) || instruction!='q' || instruction=='Q')  {

	/* print current list */
	if (instruction=='p' || instruction=='P') {
		printf("\nThe List are :\n");
		printf("===============\n");
		if (head->next!=NULL) {
			printList(head->next);
		}
		else {
			printf("\nThe list is empty.\n");
		}
		printf("\n");
	}

	/* start new empty list */
	else if (instruction=='n' || instruction=='N') {
		 if (head!=NULL) {
			 deleteAllList(head);	  // delete old list.	   
			 printf("\n----------------------------------------\n");
		 }
		 head=startNewList();
	}

	/* add integer to list */
	else if (instruction=='a' || instruction=='A') {
		fscanf(fp,"%d",&x);
		addInteger(head,head->next,head,x);	
	}

	/* delete integer from list */
	else if (instruction=='d' || instruction=='D') {
		fscanf(fp,"%d",&y);
		if (deleteInteger(head,y)) {
			head->data--;
		}
	}

	/* print the # of element in the list */
	else if (instruction=='c' || instruction=='C') {
		numberOfElement(head);
	}

	/* print the sum of element in the list */
	else if (instruction=='s' || instruction=='S') {
		printf("\nSum of element = %d\n",sumOfElement(head->next));
	}

	/* start new list made of the first z prime # */
	else if (instruction=='g' || instruction=='G') {
	 	 if (head!=NULL) {
			 deleteAllList(head);		 // delete old list.
			 printf("\n----------------------------------------\n");
		 }
	 	 fscanf(fp,"%d",&z);
		 head=startNewList();
		 startNewListMadeOfPrimeNumber(head,2,k,z);
		 head->data=z;
	}
		 fscanf(fp,"%c",&instruction);
   }
	deleteAllList(head);
	fclose(fp);
	printf("\n----------------------------------------\n");
	printf("\nallocate() was called %d times.\n",AC);
	printf("deallocate(Node *p) was called %d times.\n",DC);

	getch();
	return 0;
}

/****************************************************************
this function open input sourse file.
*****************************************************************/

void openFile() {
   fp=fopen("hw03.txt","r");   // provide path for hw03.txt here.
   if(fp==NULL) {
	  printf("!!!Can't open input file - hw03.txt!!!");
	  getch();
	  exit(0);
   }
}

/****************************************************************
this function create new node.
*****************************************************************/

Node *allocate() {
	AC++;
	return new Node;
}

/****************************************************************
this function create new node.
*****************************************************************/

void deallocate(Node *p) {
	DC++;
	delete p;
}

/****************************************************************
this function delete all nodes in list.
*****************************************************************/

void deleteAllList (Node *p) {
	if (p!=NULL) {
		Node *q=p;
		p=p->next;
		deallocate(q);
		deleteAllList(p);
	}
}

/****************************************************************
this function print the current list.
*****************************************************************/

void printList (Node *p) {
	if (p!=NULL) {
		printf("%d ",p->data);
		printList(p->next);
	}
}

/****************************************************************
this function start new empty list.
*****************************************************************/

Node *startNewList () {
	Node *head=allocate();
	head->data=0;
	head->next=NULL;
	return head;
}

/****************************************************************
this function add integer to list.
*****************************************************************/

void addInteger (Node *head,Node *current,Node *previous,int x) { 
	 if (current!=NULL) {
		 if (current->data==x) {
			 printf("\nCan not add %d in the list because is in list.\n",x);
		 }	
		 else {
			 addInteger(head,current->next,current,x);
		 }
	 } 
	 else if (current==NULL) {
		 Node *newNode=allocate(); // add at last node.
		 newNode->data=x;
		 newNode->next=NULL;
		 previous->next=newNode;
		 newNode->next=current;
		 head->data++;
	 }
}

/****************************************************************
this function delete integer from list.
*****************************************************************/

int deleteInteger (Node *p,int y) {
	if (p->next!=NULL && p->next->data==y) {
		Node *q=p->next;
		if (q->next==NULL) {  // if we delete last node
			deallocate(q);	// make the before last null.
			p->next=NULL;
		} else {
			p->next=q->next;
			deallocate(q);
		}
		return 1;
	}
	else if (p->next!=NULL) {
		return deleteInteger(p->next,y);
	}
	else {
		printf("\nNot found %d in the list.\n",y);
		return 0;
	}
}

/****************************************************************
this function print the # of element in the list.
*****************************************************************/

void numberOfElement (Node *p) {
	printf("\nThe # of element in the list = %d\n",p->data);
}

/****************************************************************
this function print the sum of element in the list.
*****************************************************************/
int sumOfElement (Node *p) {
	if (p==NULL) {
		return 0;
	}
	else {
		return p->data+sumOfElement(p->next);
	}
}

/****************************************************************
this function start new list made of the first z prime #.
*****************************************************************/

void startNewListMadeOfPrimeNumber (Node *head,int i,int &j,int z) {
	if (j<=z) {
	   if (isPrime(2,i)) {
		   addInteger(head,head->next,head,i);		   
		   j++;
	   }
	   startNewListMadeOfPrimeNumber(head,i+1,j,z);
	}
}

/****************************************************************
this function check if number is prime or not prime.
*****************************************************************/

int isPrime (int i,int j) {
	 if (j%i==0 && i!=j) {
		 return 0;
	 }
	 else if (i<j) {
		 return isPrime(i+1,j);
	 }
	 else {
		 return 1;
	 }
}

وinput داخل الملف CoMPuTeR-Q8.zip موجود

#3

جميع العمليات سويتهابواسطة recurtion

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

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