. , . :
struct TelNum {
TelNum * next; //
long telephon; //
char name[30]; //
};
TelNum *temp = new TelNum; - .
, , . . .
. NULL. - . .
, , , . , .
a)
start
temp
temp->next = start;
start = temp;
b)
start current
temp
temp->next = current->next;
current->next = temp;
a)
end temp
end->next = temp;
end = temp;
end->next = NULL;
a)
start
TelNum *del = start;
start = start->next;
delete del;
b)
current del
TelNum *del = current->next;
current->next = del->next;
delete del;
c)
current end
TelNum *del = end;
current->next=NULL;
delete del;
end = current;
, , - , , () , . , : - , - .
start
NULL
//
#include <stdio.h>
#include <conio.h>
struct TelNum;
int printAllData(TelNum * start);
int inputData(TelNum * n);
struct TelNum
{
TelNum * next;
long number;
char name[30];
};
// :
|
|
int printAllData(TelNum * start){
int c=0;
for(TelNum * t=start;t; t= t->next)
printf("#%3.3i %7li %s\n",++c,t->number,t->name);
return 0;
}
// : n -
// . 1
int inputData(TelNum * n){
printf("?"); scanf("%7li",&n->number);
if(!n->number) return 1;
printf("? "); scanf("%30s",&n->name);
return 0;
}
void main(void){
TelNum * start = NULL; //
TelNum * end = start;
do{ //
TelNum * temp = new TelNum;
if(inputData(temp)) {
delete temp;
break;
}
else {
if(start==NULL) {
temp->next = start;
start = temp;
end = temp;
}
else {
end->next=temp;
end=temp;
end->next=NULL;
}
}
}while(1);
printf("\n :\n");
printAllData(start);
getch();
do{ //
TelNum * deleted = start;
start = start->next;
delete deleted;
printAllData(start);
getch();
}while(start!=NULL);
}
2 . .
. , . .
, , . .
. ( ), ( ). , .
, . - . , 3 :
) ;
) ;
) .
NULL, .
.
.
//
#include <stdio.h>
#include <conio.h>
#include <string.h>
struct TelNum;
void printtreepre(TelNum *); // , ,
void printtreein(TelNum *); // ,,
void printtreepost(TelNum *); // , ,
int inputData(TelNum *);
TelNum * addtree(TelNum *, TelNum *);
struct TelNum
{
TelNum * left, *right;
long number;
char name[30];
};
// :
void printtreepre(TelNum * root)
{
if(!root) return;
if(root->number)
printf(" %7li %s\n",root->number,root->name);
printtreepre(root->left);
printtreepre(root->right);
}
void printtreein(TelNum * root)
{
if(!root) return;
|
|
if(root->number)
printtreein(root->left);
printf(" %7li %s\n",root->number,root->name);
printtreein(root->right);
}
void printtreepost(TelNum * root)
{
if(!root) return;
if(root->number)
printtreepost(root->left);
printtreepost(root->right);
printf(" %7li %s\n",root->number,root->name);
}
// :
int inputData(TelNum * n)
{
printf("?"); scanf("%7li",&n->number);
if(!n->number) return 1;
printf("? "); scanf("%30s",&n->name);
return 0;
}
//
TelNum * addtree(TelNum *root, TelNum *temp) {
if(root==NULL) { //
TelNum * root = new TelNum;
root->number=temp->number;
strcpy(root->name,temp->name);
root->left=NULL;
root->right=NULL;
return root;
}
else {
if(root->number>temp->number)
root->left=addtree(root->left,temp);
else root->right=addtree(root->right,temp);
}
return root;
}
//
//
void searchtree(TelNum *root, int num) {
while (root!=NULL) {
if(root->number==num) {
printf(" %7li %s\n",root->number,root->name);
return;
}
else{
if(root->number>num)
root=root->left;
else root=root->right;
}
}
puts(" ");
return;
}
void main(void)
{
TelNum * start = NULL; //
TelNum * temp = new TelNum;
do{ //
if(inputData(temp))
{delete temp;
break;
}
else
start=addtree(start,temp);
}while(1);
printtreepre(start); //ebcahgi
getch();
printtreein(start); //ighebca
getch();
printtreepost(start); //acbighe
getch();
int num;
puts(" ");
scanf("%d",&num);
searchtree(begin,num);
}