Assignment #4 – Linked Lists using Pointers (50 pts) Solution

$30.00

Description

This assignment is similar to #3 except

  • The Team information should be represented as a ordered singly linked list using pointers. (Keep the nodes in order by szTeamId as they are inserted.)
  • Your search for a Team Id is linear (following pointers) since linked lists don’t support binary search.
  • The command file will have two new subcommands for TEAM. We will be able to insert NEW teams. Also, we can change the team’s contact information.
  • You will be provided with a driver program (cs1713p4Driver.c) (see below)
  • Your code must be created in a separate C file (p4abc123.c). (see below)
  • There is a new include file, cs1713p4.h

Input:

Team same as Programming Assignment #2 although there may be different data.

Command Same as assignment 3 plus this new subcommand for TEAM:

TEAM NEW szTeamId iWins iLosses dFeeAmount dPaidAmount

6s d d lf lf

Add this new team to the linked list. Note that you should check whether it already exists and print a warning message if it does, but do not exit.

TEAM CONTACT szTeamId zTeamNm szEmail szPhone szContactName

6s 12s 30s 13s 20s

The values are separated by commas.

This command should change the contact information for a team to the specified values. If the team doesn’t exist, it should show a warning message, but not exit.

Driver program:

You will be provided with a driver program, cs1713p4Driver.c which

  1. invokes the driver’s processCommandSwitches
  2. invokes the driver’s getTeams to read the original team information into an ordered linked list. You must keep the list in order (by szTeamId) for each insertion. getTeams calls your insertLL to insert it into the ordered linked list.
  3. invokes your printTeams to print the original team information.
  4. invokes a driver-provided processCommands which
    • reads input lines from the command file until eof:
      • prints the input line
      • determines command and subcommand
      • invokes either
        • your processTeamCommand to process a TEAM subcommand
        • your processGameCommand to process a GAME subcommand
  1. invokes your printTeams to print the resulting team information
  2. Larry also provided these functions:
  • allocateNode
  • getTeams

Note: do not change the cs1713p4Driver.c

Your p4abc123.c code:

  • You should probably copy your p3abc123.c into a file named p4abc123.c.
  • It does the following includes:

#include <stdio.h>

#include <string.h>

#include “cs1713p4.h”

  • It must not include cs1713p4Driver.c within your p4abc123.c file. Look at the notes below on compiling. The “link” step is where the functions you call in the driver and the functions the driver calls in your code get resolved.
  • Remove the code for getTeams from your p4abc123.c file since it is in the driver.
  • Remove the code for sortTeams from your p4abc123.c file.
  • Remove the code for searchTeams from your p4abc123.c file.
  • Change printTeams:
    • Receives Node *pHead instead of the teamM array and count
    • The for loop varies a pointer instead of a subscript
    • References pointers to nodes instead of using teamM array subscript references (e.g., p->team.szTeamId instead of teamM[i].szTeamId)
  • Change processTeamCommand:
    • Receives Node **ppHead instead of the teamM array and count
    • Declares local Node * pointers for the result of searchLL and pPrecedes:

Node *p;

Node *pPrecedes;

    • Uses searchLL to find a Team in the ordered linked list:

p = searchLL(*ppHead, team.szTeamId, &pPrecedes);

    • Uses pointer notation to reference a team instead of referencing an element of the teamM array.
    • Add code for the new TEAM NEW subcommand. This should sscanf the data into elements of a Team structure (declared using Team Team). Show a warning if the team already exists. If it doesn’t already exist, your code should then pass that team to insertLL.
    • Add code for the new TEAM CONTACT subcommand. This should sscanf the data into elements of a Team structure (declared using Team Team). Show a warning if the team doesn’t already exist. Copy individual attributes from the Team structure to the appropriate node’s team attributes.
  • Change processGameCommand:
    • Receives Node **ppHead instead of the teamM array and count
    • Invokes processGame passing ppHead instead of the teamM array and count (check the parameter order)
    • Invokes processGameFix passing ppHead instead of the teamM array and count (check the parameter order)
  • Change processGame:
    • Receives Node **ppHead instead of the teamM array and count
    • Declares local variables for pPrecedes, p1, and p2.
    • Uses searchLL to find a Team in the ordered linked list.

p1 = searchLL(*ppHead, game.szTeamId1, &pPrecedes);

p2 = searchLL(*ppHead, game.szTeamId2, &pPrecedes);

    • Uses pointer notation to update the contents of a node instead of the contents of an array element (e.g., p->team.szTeamId instead of teamM[i]. szTeamId)
  • Change processGameFix:
    • Receives Node **ppHead instead of the teamM array and count
    • Declares local variables for pPrecedes, p1, and p2.
    • Uses searchLL to find a Team in the ordered linked list.

p1 = searchLL(*ppHead, game.szTeamId1, &pPrecedes);

p2 = searchLL(*ppHead, game.szTeamId2, &pPrecedes);

    • Uses pointer notation to update the contents of a node instead of the contents of an array element (e.g., p->team.iWins instead of teamM[i]. iWins)
  • You must create the following routines (see the include file):
    • insertLL – uses searchLL to search for a Team in the ordered linked list and then inserts it if it wasn’t found. Most of this code is in the course notes, but it is passed Team information.
    • searchLL – searches for a Team Id in the ordered linked list. If found, it returns a pointer to the node that contains it. If not found, it returns NULL. It also returns a pointer to the node that precedes it. Most of this code is in the course notes except it is passed a Team Id as its match value and it references
      p->Team.szTeamId instead of teamM[i].iInfo.

Please review the cs1713p4.h include file.

Compiling

  • (Before doing these steps, make a copy of your code called p4Saved.c in case you incorrectly type them and wipe out your .c file.)
  • Compile the driver using

gcc -g -c cs1713p4Driver.c

(automatically creates the cs1713p4Driver.o)

  • Compile your code using

gcc -g -c p4abc123.c

(automatically creates the p4abc123.o)

  • Link them together using:

gcc -g -o p4 cs1713p4Driver.o p4abc123.o

  • You only have to compile code that changes. Since the driver shouldn’t change, you only have to compile it once.

Executing the p4 executable:

./p4 -c p4Command.txt -t p4Team.txt

Turn in:

Your p4abc123.c file.

Your output based on the data provided.

Sample Output (partial):

Initial Teams

Id Team Name Wins Loss Fee Amt Paid Amt

Contact Name Phone Email

ALHGHT Cake Eaters 4 4 175.00 100.00

E Z Street (210)555-6666 sliverspoon@xyz.com

COM001 Comm Eagles 7 1 150.00 75.00

Mae King (210)555-2222 maeking@xyz.com

NEWB01 River Rats 0 8 120.00 75.00

Rock D Boat (210)555-4444 riverrat@xyz.com

SOUTH1 Slam Dunk 5 3 120.00 75.00

Jerry Tall (210)555-3333 slamdunk@gmail.com

UNKN01 Org New Blk 1 7 150.00 50.00

Bob Wire (210)555-1234 bobwire@xyz.com

UNKN02 Hackers 3 5 150.00 75.00

Tom E Gunn (210)555-5555 cyber@gmail.com

UTSA01 Armadillos 8 0 150.00 80.00

Jean E Us (210)555-1111 utsa@xyz.com

GAME RESULT UTSA01 NEWB01 55 12

GAME RESULT COMM01 SOUTH1 17 15

*** team (COMM01) not found

GAME RESULT SOUTH1 ALHGHT 66 3

GAME RESULT UTSA01 SOUTH1 44 45

GAME RESULT UNKN01 UNKN02 33 39

GAME RESULT COM001 UNKN02 43 37

GAME RESULT ALHGHT UNKN02 20 20

*** game was a tie

GAME RESULT NEWB01 NEWB01 30 20

*** same team

TEAM SHOW UTSA01

UTSA01 Armadillos 9 1 150.00 80.00

Jean E Us (210)555-1111 utsa@xyz.com

TEAM PAID UTSA01 50.00

TEAM SHOW UTSA01

UTSA01 Armadillos 9 1 150.00 130.00

Jean E Us (210)555-1111 utsa@xyz.com

TEAM SHOW UNKN01

UNKN01 Org New Blk 1 8 150.00 50.00

Bob Wire (210)555-1234 bobwire@xyz.com

TEAM PAID UNKN01 30.00

TEAM PAID UNKN01 30.00

TEAM SHOW UNKN01

UNKN01 Org New Blk 1 8 150.00 110.00

Bob Wire (210)555-1234 bobwire@xyz.com

TEAM PAID YYYY01 50.00

*** team (YYYY01) not found

TEAM SHOW YYYY01

*** team (YYYY01) not found

TEAM SHOW NEWB01

NEWB01 River Rats 0 9 120.00 75.00

Rock D Boat (210)555-4444 riverrat@xyz.com

GAME FIX UTSA01 NEWB01 55 12 55 2

TEAM SHOW UTSA01

UTSA01 Armadillos 9 1 150.00 130.00

Jean E Us (210)555-1111 utsa@xyz.com

TEAM SHOW NEWB01

NEWB01 River Rats 0 9 120.00 75.00

Rock D Boat (210)555-4444 riverrat@xyz.com

TEAM SHOW SOUTH1

SOUTH1 Slam Dunk 7 3 120.00 75.00

Jerry Tall (210)555-3333 slamdunk@gmail.com

GAME FIX UTSA01 SOUTH1 44 45 45 44

TEAM SHOW UTSA01

UTSA01 Armadillos 10 0 150.00 130.00

Jean E Us (210)555-1111 utsa@xyz.com

TEAM SHOW SOUTH1

SOUTH1 Slam Dunk 6 4 120.00 75.00

Jerry Tall (210)555-3333 slamdunk@gmail.com

TEAM NEW SSIDE1 1 0 120.00 70.00

TEAM CONTACT SSIDE1,SS 4 Ever,MannyFest@gmail.com,(210)555-8888,Manny Fest

TEAM SHOW SSIDE1

SSIDE1 SS 4 Ever 1 0 120.00 70.00

Manny Fest (210)555-8888 MannyFest@gmail.com

TEAM NEW SOUTH1 0 0 130.00 75.00

*** team (SOUTH1) already exists