Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,688 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,504 people online right now. Registration is fast and FREE... Join Now!




C++ Linked List Implementation

 
Reply to this topicStart new topic

C++ Linked List Implementation

otiseoj
post 6 Dec, 2005 - 07:10 PM
Post #1


New D.I.C Head

*
Joined: 28 Feb, 2005
Posts: 18

Please help with programming project i have due for tomorrow!
Here are the instructions and what I have so far!



Filename: cs201pj2.cpp

(use all lowercase letters in your filename, and make sure it is located in your cs201 directory, also all lowercase)

Write a program that will use a array to store a linked list. Your linked list should contain an Student number and an the student's last name. You linked list should use the student number as the key field to order the list and contain a maximum of 8 students. Each line will start with a number that will indicate the operation. The data necessary to perform the operation will follow. Each operation shoud print out a message indicating that it was performed correctly. Your program should process the following operations:

Initialize an empty list

This operation should initialize the list to an empty list

0

Add an student to the list:

This operation should add this student to the linked list

1 4521 Smith

Delete an student from the list:

Delete this student from the linked list. If the student is not in the list, print a message.

2 1000

Print the list in Logical Order

3

Find an student in the list:

Find this student from the linked list. If the student is not in the list, print a message.

4 1000

Print the list in Physical Order

This operation should print the physical contents of the list along with all of the links, list and free.

5



Your program should read the input from a data file by redirecting the input to the program ($ a.out < datafile).



Example data file:

0
1 3000 Hill
1 1000 Smith
1 2000 Green
3
5


For this data file, the first operation should initialize the list, the next 3 operations should add the 3 students to the list, the 4th operation (3) should print the list logical order:

1000 Smith
2000 Green
3000 Hill

The last operation (5) should print the physical contents of the list.

list=1 avail= 3

0 3000 Hill -1
1 1000 Smith 2
2 2000 Green 0
3 4
4 5
5 6
6 7
7 -1








This is the code i have.

#include <iostream>
#include <string>

struct studenttype{
int stnum;
string lname;
int link;
}
studenttype stud[8];
int list;
int avail;
int prev;

void initialize (int&, int&, int&);
void addstud (int&, studenttype[], int&, int&);
void delstud (int&, studenttype[], int&, int&);
void printlo (int, studenttype[]);
void findstud (int, studenttype[]);
void printfo (int, studenttype[], int);

main();
{
int operation;
while (cin>>operation)
{ switch (operation)
{
case 0 :
initialize (list, avail, prev);
break;
case 1 :
addstud (list, stud, avail, prev);
break;
case 2 :
delstud (list, stud, avail, prev);
break;
case 3 :
printlo (list, stud);
break;
case 4 :
findstud (list, stud)
break;
case 5 :
printfo (list, stud, avail);
break;
default :
cout<<”Entered wrong operation code”<<endl;
break;
}
}
}
void initialize (int& begin, int& open, int& before)
{
begin = -1;
open = 0;
before = -1
cout<<”The list is now empty”<<endl;
}
void addstud (int& begin, studenttype s[], int& open, int& before)
{ cin>>s[open].idnum>>s[open].lname;
if (before = -1)
{ s[open].link = before;
before = before + 1;
begin = open;
open = open + 1;
cout<<”The next available position is “<<open<<endl;
} else
{ s[before].link = open;
before = before + 1;
s[open].link = -1;
open = open + 1;
cout<<”The next available position is “<<open<<endl;
}
}

void delstud (int& begin, studenttype s[], int& open, int& before)
{ int ditem;
while (s[
User is offlineProfile CardPM

Go to the top of the page


fyrestorm
post 6 Dec, 2005 - 07:38 PM
Post #2


D.I.C Lover

Group Icon
Joined: 4 Apr, 2002
Posts: 3,103



Thanked 1 times

Dream Kudos: 228
My Contributions


nothing like procrastination, eh?

what's your hang up?
User is offlineProfile CardPM

Go to the top of the page

otiseoj
post 7 Dec, 2005 - 10:32 AM
Post #3


New D.I.C Head

*
Joined: 28 Feb, 2005
Posts: 18

What Im having problems with is deleting items"data" from this linked list without using pointers. I dont know how to get my delete function running. Everythingelse seems to be okay.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 08:07AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month