Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

creating a linked list with struct


  • Please log in to reply

#1
shibby

shibby

    New Member

  • Member
  • Pip
  • 2 posts
Hi,
I have an assignment for my comp sci class where i have to make a program using a linked list allowing me to enter a students last name, first name, id, and a test score then have several functions, one printing a roster, one a gradebook, one is a search where the user enter's the student ID number and then displays all the info about that student and then one is adding a student by adding a node to the list and deleting a student. My professor just touched briefly on the linked list and being that im a first time programmer i am having difficulties w/this assignament, if anyone could help me out i'd appreciate it, here's the program i have, but its not even running. so anyhelp i would appreciate! Thanks!!

email: [email protected]

#include <iostream>

using namespace std; //introduces namespace std


struct info
{
char lName[50];
char fName[50];
char iD[20];
int score;
info *next;
};

info* classRoster(info *list,int count)
{
info temp;
info *currentNode=list;
for(int i=0;i<count;i++){
while(currentNode->next!=NULL){
if(strcmp(currentNode->lName,(currentNode->next)->lName)>0){
temp=currentNode->lName;
currentNode->lName=(currentNode->next)->lName;
(currentNode->next)->lName=temp;
}//end if
currentNode=currentNode->next;
}//end while
}//end for
currentNode=list;
cout<<"Class Roster:"<<endl;
cout<<"Name"<<"\t\t\t\t\t"<<"ID#"<<endl;
while(currentNode!=NULL){
cout<<currentNode->lName;
cout<<currentNode->iD;
currentNode=currentNode->next;
}//end while
return list;
}

void gradeBook(info *listhead)
{
info *currentNode=listhead;
cout<<"Grade Book:"<<endl;
cout<<"ID#"<<"\t\t\t"<<"Score"<<endl;
while(currentNode!=NULL){
cout<<currentNode->iD;
cout<<"\t\t\t"<<currentNode->score<<endl;
currentNode=currentNode->next;
}//end while
}

void studentRecord(info *listhead)
{
int iD;
info *currentNode=listhead;
cout<<"Enter the students id:"<<endl;
cin>>iD;
while(currentNode!=NULL){
if(currentNode->iD==iD){
cout<<currentNode->lname;
cout<<currentNode->fname<<endl;
cout<<currentNode->iD<<endl;
cout<<currentNode->score<<endl;
}
currentNode=currentNode->next;
}
}

info* addAStudent(info *listhead)
{
info* newNode;

newNode=new info;
cout<<"Enter the student's last name:";
cin>>newNode->lName,50;
cout<<"Enter the student's first name:";
cin>>newNode->fName,50;
cout<<"Enter the student's id number:";
cin>>newNode->iD;
cout<<"Enter the student's score:";
cin>>newNode->score;
newNode->next=listhead;
listhead=newNode;
cout<<endl;
return listhead;
}

info *deleteNode(node* list,int n)
{
node *ptr,*dptr;
cout<<"Enter the id of the student you want to delete:";
cin>>n;
ptr=list;
if(n==1)
list=list->next;
if(n==2)
list->next=ptr->next;
if(n>2)
for(int i=0;i<n-1;i++)
ptr=ptr->next;
dptr=ptr->next;
ptr->next=dptr->next;
return list;
}


int main()
{
info *head=NULL;
info *newNode;
int numberOfStudents,a;
cout<<"Enter the number of students in the class:"<<endl;
cin>>numberOfStudents;
for(int i=0;i<numberOfStudents;i++){
newNode= new info;
cout<<"Enter the student's last name:";
cin>>newNode->lName,50;
cout<<"Enter the student's first name:";
cin>>newNode->fName,50;
cout<<"Enter the student's id number:";
cin>>newNode->iD;
cout<<"Enter the student's score:";
cin>>newNode->score;
cout<<endl;

}
do{
cout<<"1. Print class roster."<<endl;
cout<<"2. Print grade book."<<endl;
cout<<"3. Print a student record."<<endl;
cout<<"4. Add a student."<<endl;
cout<<"5. Delete a student."<<endl;
cout<<"6. Quit."<<endl;
cin>>a;

if(a==1)
head=classRoster(head,numberOfStudents);
cout<<endl;
if(a==2)
gradeBook(head);
cout<<endl;
if(a==3)
studentRecord(head);
cout<<endl;
if(a==4)
head=addAStudent(head);
if(a==5)
head=deleteAStudent(head);
}while(a!=6);
cout<<"Program Terminated!"<<endl;
return 0;
}
  • 0

Advertisements


#2
BuRnEr

BuRnEr

    Member

  • Member
  • PipPip
  • 48 posts
first of all , i did not uderstand anything from your codes...but nice try
second i m not going to write you any single code because you need to study about linked lists and obviosly you are using your array knowledge but it is wrong arrays more far from the linked lists,

i know that you re student, need a help and you want to learn this concept and programming and thats why you re being study, but things is little different if you want to learn how to program, you have to strugle, that s wyh homeworks existed.(and please dont be :tazz: me and others)

i can write this whole thing,send it to you and you ll get A but you ll not get and learn anything please search and strugle.And whenever you see you have no way to go then ask about it.You are student thats wyh i m writing like this to you.you re there for learning something not for getting good marks.

in your codes first of all you did not creat your start pointer which shouldnt move around newer, if you move it you loose at the beginning of the list. And your functions are not ready for used. you re trying to get output everywhere in your code(with cout), I didnt get also that why you trying to pass parameters to functions by pointer and valu ???? and you newer checked if your list is empty, there is one node, and more than 1 node. Addnode() and deletenode() are ???
no distructor(~) u are used (memory leaking!!!!!)

i can only tell you you have to take look at linked list subject if you want to use it.
i can help you this way good tutorial at:
http://www.geekstogo...ers-t19611.html
http://richardbowles...st/linklist.htm
if you finish this tuto you will at least understand how to add,remove, node and get info from node.
and also when you say linked list are really pice of cake so there is another challange for you double linked lists ;), and using "recursion" for linked list.

and believe me linked list and recursion are still problem for me and for others

keep practice
  • 0

#3
StarHawk

StarHawk

    Member

  • Member
  • PipPipPip
  • 189 posts
Iwould suggest The book Algorithms in C Third edition by Robert Sedgewick. It is a common book and a GREAT book. Look in your University Library for this or any other book on Algorithms by Sedewick. It has great info on linked list recursion and more. I know its C and you seem to be using C++ but changes in this case would be minor.

I agree with BuRnEr

i m not going to write you any single code because you need to study about linked lists and obviosly you are using your array knowledge but it is wrong arrays more far from the linked lists,

i know that you re student, need a help and you want to learn this concept and programming and thats why you re being study, but things is little different if you want to learn how to program, you have to strugle, that s wyh homeworks existed.(and please dont be ranting.gif me and others)

i can write this whole thing,send it to you and you ll get A but you ll not get and learn


you learn to code by Coding. No other way to do it that i know of. reading books and reading other peoples code helps but nothing beats coding and debugging!!!!!

I also recommend The Practice Of Programming by Rob Pike and Brian W Kernighan esp for good info on debugging not covered in most books and classes.

Besides ya wouldn't want me writing it since i use complex pointer manipulations far too much esp function pointers and pointers to pointers to pointers .....

But your code as it is could be fixed, give it a shot. :tazz:
  • 0

#4
shibby

shibby

    New Member

  • Topic Starter
  • Member
  • Pip
  • 2 posts
haha, thanks you guys...i know i'm in school to learn and trust me i take it very seriously but for one i hate my programming class, and this is the only one i have to take so yah all i do want is an A, but anyways i know that program was waaay wrong, i was just throwin it together anyways i got it to work except for my delete node function, so if you can at least take a look at that i'd appreciate it! by the way i already had to turn it in w/o that working, so i just wanna know for my own info. here it is: everyfunction works but the deleteNode

#include <iostream>
#include <cstring>

using namespace std; //introduces namespace std


struct info
{
char lname[50];
char fname[50];
char iD[20];
int score;
info *next;
};

info* classRoster(info *list,int count)
{
info temp;
info *currentNode=list;
for(int i=0;i<count;i++){
while(currentNode->next!=NULL){
if(strcmp(currentNode->lname,(currentNode->next)->lname)>0){
strcpy(temp.lname,currentNode->lname);
strcpy(temp.fname,currentNode->fname);
strcpy(temp.iD,currentNode->iD);
temp.score=currentNode->score;

strcpy(currentNode->lname,(currentNode->next)->lname);
strcpy(currentNode->fname,(currentNode->next)->fname);
strcpy(currentNode->iD,(currentNode->next)->iD);
currentNode->score=(currentNode->next)->score;

strcpy((currentNode->next)->lname,temp.lname);
strcpy((currentNode->next)->fname,temp.fname);
strcpy((currentNode->next)->iD,temp.iD);
(currentNode->next)->score=temp.score;
}//end if
currentNode=currentNode->next;
}//end while
}//end for
currentNode=list;

return list;
}

void showList(info *listhead)
{
info *currentNode=listhead;
cout<<"Class Roster"<<endl;
cout<<"Name"<<"\t\t\t"<<"ID"<<endl;
while(currentNode!=NULL){
cout<<currentNode->lname;
cout<<"\t\t\t"<<currentNode->iD<<endl;
currentNode=currentNode->next;
}//end while
}
void gradeBook(info *listhead)
{
info *currentNode=listhead;
cout<<"Grade Book:"<<endl;
cout<<"ID#"<<"\t\t\t"<<"Score"<<endl;
while(currentNode!=NULL){
cout<<currentNode->iD<<"\t\t\t"<<currentNode->score<<endl;
currentNode=currentNode->next;
}

}

void studentRecord(info *listhead)
{
char v[20];
info *currentNode=listhead;
int d;
d=cin.get();
cout<<"Enter the students id:"<<endl;
cin.getline(v,20);
while(currentNode!=NULL){
if(strcmp(currentNode->iD,v)==0){
cout<<"Name:"<<currentNode->lname<<" ";
cout<<currentNode->fname<<endl;
cout<<"ID:"<<currentNode->iD<<endl;
cout<<"Score:"<<currentNode->score<<endl;
}
currentNode=currentNode->next;
}
}

info* addAStudent(info *listhead)
{
info* newNode;

newNode=new info;
cout<<"Enter the student's last name:";
cin>>newNode->lname,50;
cout<<"Enter the student's first name:";
cin>>newNode->fname,50;
cout<<"Enter the student's id number:";
cin>>newNode->iD;
cout<<"Enter the student's score:";
cin>>newNode->score;
newNode->next=listhead;
listhead=newNode;
cout<<endl;
return listhead;
}

info *deleteAStudent(info* list)
{
info *ptr,*dptr;
ptr=list;
int n=0,a=1,d;
char v[20];
info *currentNode=list;
d=cin.get();
cout<<"Enter the student id you want to delete:"<<endl;
cin.getline(v,20);
while(currentNode!=NULL){
if(strcmp(currentNode->iD,v)==0){
n=a;
}
a++;
}

if(n==1)
list=list->next;
if(n==2)
list->next=ptr->next;
if(n>2)
for(int i=0;i<n-1;i++)
ptr=ptr->next;
dptr=ptr->next;
ptr->next=dptr->next;
return list;
}


int main()
{
info *head=NULL;
info *newNode;

int numberOfStudents,a,d;
cout<<"Enter the number of students in the class:"<<endl;
cin>>numberOfStudents;
for(int i=0;i<numberOfStudents;i++){
d=cin.get();
newNode= new info;
cout<<"Enter the student's last name:";
cin.getline(newNode->lname,50);
cout<<"Enter the student's first name:";
cin.getline(newNode->fname,50);
cout<<"Enter the student's id number:";
cin.getline(newNode->iD,20);
cout<<"Enter the student's score:";
cin>>newNode->score;
newNode->next=head;
head=newNode;
cout<<endl;


}
cout<<"1. Print class roster."<<endl;
cout<<"2. Print grade book."<<endl;
cout<<"3. Print a student record."<<endl;
cout<<"4. Add a student."<<endl;
cout<<"5. Delete a student."<<endl;
cout<<"6. Quit."<<endl;
cin>>a;
while(a!=6){
if(a==1){
head=classRoster(head,numberOfStudents);
showList(head);
}
cout<<endl;
if(a==2)
gradeBook(head);
cout<<endl;
if(a==3)
studentRecord(head);
cout<<endl;
if(a==4)
head=addAStudent(head);
showList(head);
if(a==5){
head=deleteAStudent(head);
showList(head);
}
cout<<"1. Print class roster."<<endl;
cout<<"2. Print grade book."<<endl;
cout<<"3. Print a student record."<<endl;
cout<<"4. Add a student."<<endl;
cout<<"5. Delete a student."<<endl;
cout<<"6. Quit."<<endl;
cin>>a;
}
cout<<"Program Terminated!"<<endl;
return 0;
}
  • 0

#5
stu_design

stu_design

    Member

  • Member
  • PipPipPip
  • 217 posts
For this kinda stuff thers a post from b4 on this from Burner

http://www.geekstogo...ers-t19611.html

Stu Design
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP