PDA

View Full Version : مبتدی: کمک در لیست های پیوندی در c



mohammaddx
جمعه 03 بهمن 1393, 14:17 عصر
با سلام به همه ی دوستان.
دوستان دو روز دیگه امتحان c دارم اگه زودتر کمکم کنید ممنون میشم
در برنامه


#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#define TRUE 1
#define NIL (struct student *)NULL
struct student {
char name[30] ;
int stno ;
int unit ;
struct student *next ;
} ;
struct student *first , *last , *node ;
void enter ();
void list ();
void del ();


int main ()
{
char s[10] ;
first = NIL ;
while (TRUE) {
clrscr() ;
gotoxy(20, 4) ;
printf("E)enter name to list ");
gotoxy(20, 6) ;
printf("R)remove name from list");
gotoxy(20, 8) ;
printf("L)print the list on CRT");
gotoxy(20, 10) ;
printf("Q)quit from program ") ;
gotoxy(20, 12) ;
printf("enter your select");
printf("( E R L Q):") ;
gets(s) ;
*s = toupper(*s) ;
switch (*s) {
case 'E' :
enter() ;
break ;
case 'L' :
list() ;
getch() ;
break ;
case 'R' :
del() ;
break ;
case 'Q' :
exit(0) ;
}//end of switch
} //end of while
} // end of main
//********************
void enter ()
{
char numstr[30] ;
node = (struct student *)malloc(sizeof(struct student));
node -> next = NIL ;
if (first == NIL)
first = last = node ;
else {
last -> next = node ;
last = node ;
}
printf("\n enter name of student:");
gets(last -> name) ;


printf("\n enter student number :");
gets(numstr) ;
last -> stno = atoi(numstr) ;


printf("\n enter number of unit :");
gets(numstr) ;
last -> unit = atoi(numstr) ;
} /* end of enter */
//********************
void list ( )
{
int i ;
if (first == NIL) {
printf("\n<< the list is empty .>>") ;
getch() ;
return ;
}
last = first ;
clrscr() ;
gotoxy(5,4) ;
printf(" name st.number unit ") ;
gotoxy(5, 5);
printf("--------- --------- -----") ;
i = 6 ;
do {
gotoxy(5, i);
printf("%s", last -> name) ;
gotoxy(25, i);
printf("%d", last -> stno) ;
gotoxy(38, i);
printf("%d", last -> unit) ;
i ++ ;
last=last -> next ;
} while (last != NIL) ;
gotoxy(5, i++) ;
printf("**********************") ;
printf("***************") ;
gotoxy(10, i++) ;
printf("press a key to continue.");
getch() ;
}
//*******************
void del ()
{
int stnumber ;
gotoxy(20, 14) ;
printf("enter student number for delete:");
scanf("%d", &stnumber) ;
last = node = first ;
while (last != NIL)
{
if (last -> stno!=stnumber) {
node = last ;
last = last -> next ;
continue ;
}
else {
if (last == first) {
first=last -> next ;
free(last) ;
free(node) ;
break ;
}//end of if
else {
node -> next=last -> next;
free(last) ;
break ;
}//end of else
}//end of else
}//end of while
}


که مثال 6-7 کتاب سی اقای جعفرنژاد قمی هست مربوط به قرار دادن پرونده دانشجویان در لیست های پیوندی با امکان حذف و اضافه کردن و نمایش ان ها است.
متوجه منظور #define NIL (struct student *)NULL نمیشم.درسته که (struct student *)NULL را میاد به جای NIL قرار میده ولی کلا (struct student *)NULL به چه درد میخوره؟