0

Hiiiii :) iam new member from saudi arabia and i need help for my program in c language ,the out put dont perform the operation of linklist please help me :rolleyes:

Attachments
#include <stdio.h>
  #include <conio.h>
  #include <stdlib.h>
  #include <ctype.h>
  #define null 0
  #define max 100
typedef struct students{
struct students *last;
int id;
char name[10];
float gpa;
struct students *link;
}student;


typedef struct course {struct course *last;
int id;
int grade ;
float group;
struct course *link;
}courses;


main(){
student *k,*kk,*kkk;
courses *coa,*cob,*p,*pt,*ptr,*ptrt;
student *createstudent();
courses *createcourse();
void displaystudent(  student*) ;
void displaycourse(  courses*);
int lengthstudent (student *);
int lengthcourse (courses *);
student *searchstudent(int ,student*);
courses *searchincourse(int ,courses*);
void insertstudent(int,student*);
void insertcourse(int,courses*);
void deletestudent(int,student*);
void deletecourse(int,courses *);
student *sortstudent(student*);
courses *sortcourse(courses*);
courses * unioncou(courses *a,courses *b);
courses *subtractcourse(courses *a,courses *b);
courses *intersection(courses *a,courses *b);
student *updates(student *);
courses *update(courses *);
courses *project(courses*);
student *select(student *);
int ch,l ,no;
do{
clrscr();
printf ("\n\n\n\t\t PROGRAM TO PERFORM VARIOUS OPREATION");
printf("\n\n\n\t\t*******MENU FOR OPERATION**********");
printf("\n\t\t 1. create student list");
printf("\n\t\t 2.create student course A");
printf("\n\t\t 3.create student course B");
printf("\n\t\t 4.display student list");
printf("\n\t\t 5.display information of course A");
printf("\n\t\t 6.display information of course B");
printf("\n\t\t 7.length of student list");
printf("\n\t\t 8.length of student list in course A");
printf("\n\t\t 9.length of student list in course B");
printf("\n\t\t 10.search for student");
printf("\n\t\t 11.search for student in course A");
printf("\n\t\t 12.search for student in course B");
printf("\n\t\t 13.insert student in list");
printf("\n\t\t 14.insert student in course A");
printf("\n\t\t 15.insert student in course B");
printf("\n\t\t 16.delete student from list");
printf("\n\t\t 17.delete student from list in course A");
printf("\n\t\t 18.delete student from list in course B");
printf("\n\t\t 19.sort student list");
printf("\n\t\t 20.sort student list in course A");
printf("\n\t\t 21.sort student list in course B");
printf("\n\t\t 22.union of two courses");
printf("\n\t\t 23.subtracr two list");
printf("\n\t\t 24.intersection of courses");
printf("\n\t\t 25.update studentlist");
printf("\n\t\t 26.update student course ");
printf("\n\t\t 27.project course ");
printf("\n\t\t 28.select student");
printf("\n  ENTER YOUR CHOICE: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
k=createstudent();
getch ();
break;

case 2:
coa=createcourse();break;
case 3:
cob=createcourse();break;
case 4:
if(k->link !=null)

displaystudent(k);
else
printf("\n listempty");
getch();
break;
case 5:
displaycourse(coa);break;
case 6:
displaycourse(cob);break;
case 7:
l=lengthstudent(k);
printf("length of student list  in course A is %d",l);
case 8:
l=lengthcourse(coa);
printf("length of student list is %d",l);
case 9:
l=lengthcourse(cob);
printf("length of student list is %d",l);
case 10:
printf("\n enter no after which you want to searched for:");
scanf("%d",&no);
kk=searchstudent(no,k);
if(kk==null)
printf("\n\tstudent not in the list");
else
printf("\n\t student is present in the list");
getch();
break;
case 11:
 printf("\n enter no after which you want to searched for:");
scanf("%d",&no);
p=searchincourse(no,coa);
if(p==null)
printf("\n\tstudent not in the list");
else
printf("\n\t student is present in the list");break;
case 12:
 printf("\n enter no after which you want to searched for:");
scanf("%d",&no);
p=searchincourse(no,cob);
if(p==null)
printf("\n\tstudent not in the list");
else
printf("\n\t student is present in the list");break;
case 13:
printf("\n\t enter no you want to insert after: :");
scanf("%d",&no);
insertstudent(no,k);
case 14:
printf("\n\t enter no you want to insert after: ");
scanf("%d",&no);
insertcourse(no,coa);
case 15:
printf("\n\t enter no you want to insert after: ");
scanf("%d",&no);
insertcourse(no,cob);
case 16:
printf("\n\t enter student that you want deleted ");
scanf("%d",&no);
deletestudent(no,k);
case 17:
printf("\n\t enter student that you want deleted in course A ");
scanf("%d",&no);
deletecourse(no,coa);
case 18:
printf("\n\t enter student that you want deleted in course B ");
scanf("%d",&no);
deletecourse(no,cob);
case 19:
kkk=sortstudent(k);
displaystudent(k); break;
case 20:
pt=sortcourse(coa);
displaycourse(coa);break;
case 21:
pt=sortcourse(cob);
displaycourse(cob) ;break;
case 22:
 unioncou (coa,cob); break;
 case 23:
 subtractcourse(coa,cob);break;
 case 24:
 intersection(coa,cob);break;
 case 25:
 updates(k);break;
 case 26:
 update(coa); break;
 case 27:
 project(cob);
 case 28:
 select(k);
}
}while(ch!=-99);
}



student * createstudent() {
student *head=null;
student *temp=null;
student *newn=null;
int no,i;
up:clrscr();
printf("\n\t enter number of node:");
scanf("%d",&no);
if( no==-99)
goto up;
for(i=1;i< no;i++){

newn=(student*)malloc(sizeof(student));
newn->link=0;
newn->last=0;
head=newn;
printf("\nENTER THE INFORMATION OF STUDENT");
scanf("%d %s %f",&newn->id,&newn->name,&newn->gpa);
if(i==1){
head=temp=newn; }
else
temp= newn;
newn=((student*)malloc(sizeof(student)));
newn->link=null;
newn->last=temp;
temp->link=newn;}fflush(stdin);
free(newn);

return (head);}

courses * createcourse ()
{courses *head=null;
courses *temp=null;
courses *newn=null;
int no,i;
up:clrscr();
printf("\n\t enter number of node:");
scanf("%d",&no);
if( no==-99)
goto up;
for(i=1;i< no;i++)
{
newn =((courses*)malloc(sizeof(courses)));
newn->link=null;
newn->last=null;
head=newn;
printf("\nENTER THE INFORMATION OF STUDENT");
scanf("%d %d %f",(&newn->id),(&newn->group),(&newn->grade));
if(i==1){
head=temp=newn; }
else
temp= newn;
newn=((courses*)malloc(sizeof(courses)));
newn->link=null;
newn->last=temp;
temp->link=newn;}fflush(stdin);
free(newn);
temp->link=null;
return (head);}



void displaystudent(student *k)

  {while(k->link!=null){
  printf("id= %d name=%s gpa=%f",k->link->id,k->link->name,k->link->gpa);
  k=k->link;}}




  void displaycourse(courses *coa)
{
   if(coa==0)
   {
 printf(" \n\n List is Empty ");
 return;
 } printf("\n\tID \t\tGRADE \t\tGROUP");
 while(coa!= 0)
 {
  printf("\n\n%d %d %f",coa->id,coa->grade,coa->group);
  coa = coa->link;
  }
  }
  int lengthstudent( student *current)
  {
  int count=0;
  if(current !=null)
  {
  while(current !=null)
  {
  count++; 
  }}
  return count;}
  int lengthcourse( courses *current)
  {
  int count=0;
  if(current !=null)
  {
  while(current !=null)
  {
  count++;
  current=current->link;
  }}
  return count;}

 student *searchstudent(int id,student *k) {
 int flag=0;
 while(k!=null && !flag){
if(id==k->id)
flag=1;
else
k=k->link;
}
if(flag)
return k;

}

courses *searchincourse(int id,courses *p) {
int flag=0;
while(p!=null && !flag){
if(id==p->id)
flag=1;
else
p=p->link;
}
if(flag)
return p;}
void insertstudent(int id,student *k)
{ int no;
student *t,*kkk=null;
no=lengthstudent(k);
if(no >= max)
{
printf("\n\t list full cannot insert no :%d",id);
getch();
return ;}
t=searchstudent(id,k);
if(t!=null)
{
printf("\n\t ENTER THE STUDENT YOU WANT TO INSERT");
scanf("%d",&no);
kkk=((student*)malloc(sizeof(student)));
kkk->id=no;
kkk->link=0;
kkk->link=t->link;
t->link=kkk;
printf("\n\t Number Inserted Sucessfully");
getch();
}else
{
printf("\n\t cannot inserted no:%d",id);
getch();
}}

void insertcourse(int id,courses *f)
{ int no;

courses *t,*p=null;
no=lengthcourse(f);
if(no >= max)
{
printf("\n\t list full cannot insert no :%d",id);
getch();
return ;}
t=searchincourse(id,f);
if(t!=null)
{
printf("\n\t ENTER THE STUDENT YOU WANT TO INSERT");
scanf("%d",&no);
p=((courses*)malloc(sizeof(courses)));
p->id=no;
p->link=0;
p->link=t->link;
t->link=p;
printf("\n\t Number Inserted Sucessfully");
getch();
}else
{
printf("\n\t cannot inserted no:%d",id);
getch();
}}
void deletestudent(int id,student *k)
{
student *t1,*t2;
t2=k;
t1=searchstudent(id,k);
if(t1 !=null)
{
if(t1==k)
{
t2=t2->link;
k=t2;
free(t1);
printf("\n\t first node deleted");
getch();
}
t2=k;
while(t2->link !=t1)
t2=t2->link;
free(t1);
printf("\n \t student containing number %d deleted",id);
 getch();
}
 else
{
printf("\n \t deletion not suceesddfull");

} }
void deletecourse(int id,courses *f)
{
courses *t1,*t2;
t2=f;
t1=searchincourse(id,f);
if(t1 !=null)
{
if(t1==f)
{
t2=t2->link;
f=t2;
free(t1);
printf("\n\t first node deleted");
getch();
}
t2=f;
while(t2->link !=t1)
t2=t2->link;
free(t1);
printf("\n \t student containing number %d deleted",id);
getch();
}
else
{
printf("\n \t deletion not suceesddfull");
getch();
} }
 student *sortstudent(student *k) {
 student *ptr1, *ptr2;
 ptr1 = k ;
 ptr2=k->link;
  while (ptr1 != null && ptr2 !=null){
 {
if (ptr1->id <= ptr2->id) {
ptr2 = ptr2->link;
ptr1 = ptr1->link;
}
ptr1->link=ptr2->link;
ptr2->last=ptr2->last;
if(ptr1->last!=null){
ptr1->last->link=ptr2;
if(ptr2->link!=null)
ptr2->link->last=ptr1;
ptr2->link=ptr1;
if(ptr1==k)
ptr1=k;
ptr2=ptr1->link;}}}
return k;}

 courses *sortcourse(courses *k) {
 courses *ptr1, *ptr2;
 ptr1 = k ;
 ptr2=k->link;
  while (ptr1 != null && ptr2 !=null){
 {
if (ptr1->id <= ptr2->id) {
ptr2 = ptr2->link;
ptr1 = ptr1->link;
}
ptr1->link=ptr2->link;
ptr2->last=ptr2->last;
if(ptr1->last!=null){
ptr1->last->link=ptr2;
if(ptr2->link!=null)
ptr2->link->last=ptr1;
ptr2->link=ptr1;
if(ptr1==k)
ptr1=k;
ptr2=ptr1->link;}}}
return k;}
courses *unioncou (courses *p,courses *q)
{
courses *ptr2 =q;
courses *ptr1 = p;
     /* Second List Pointer */
 while(ptr2 != null){
/* First List Pointer */
while(ptr1 != null)
{
 while(ptr2->id!=ptr1->id)
{
ptr2 = ptr2->link;
}
2
Contributors
2
Replies
3
Views
11 Years
Discussion Span
Last Post by Salem
0

please any one answer because the doctor discuss me on this program :-| :rolleyes: :-| :-|

0

Well I think there are some redundant white space characters you can get rid of for total unreadability :rolleyes:

For example, please reformat this

student *select(student *k)
{ int a;
printf("\n enter chosen");
scanf("%d",&a);
student *t=null;
t=k;
while(k!=null){
if((k->gpa)>a)
printf("\n (%d %s %f ",k->id,k->name,k->gpa);}
k=k->link;
 }

To look more like this

student *select(student *k) {
  int a;
  printf("\n enter chosen");
  scanf("%d",&a);

  student *t=null;    /*!! slipping into C++ mode here */
  t=k;
  while(k!=null) {
    if((k->gpa)>a) /*!! braces here would really make your meaning clear */
      printf("\n (%d %s %f ",k->id,k->name,k->gpa);
  } /*!! I think this brace is in the wrong place */
    /*!! That is, you want to step the list INSIDE the loop */
  k=k->link;

  /*!! You're supposed to return a student* here */
  /*!! but you just fall off the end of the function with nothing */
}

See, already I've spotting that one of your linked list routines will lock up simply by reformatting the code to indicate the flow of the program. And that's just one I picked at random.

This simple act of clearing up your code will no doubt throw up a lot of similar problems.

First off, there is little in your code which cannot be compiled with a C compiler, so the first question is, are you learning C or C++. I'm assuming that the .cpp extension to your filename is you just taking whatever the IDE prompts you with.

The random bits which need C++ I assume are there because your compiler didn't tell you at the time.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.