-
I need help
I just need someone to check my code.. I've already writin it so I'm not asking for someone to help me cheat or anything, I'm just havin a tuff time with this. I'll post the code that I've writtin but I'm not sayin its all right.... thats why I need help, thanks
I'm not sure how to delete the node that is requested
and how to print the node for the given location.
I have to write a program in C that implements a singly linked list using a pointer implementation. The data comes from a command-line parameter.
the file will follow this format:
<action-char><equiptment-name><location><property-tag-number>
<action-char> is a single letter code:
'a' - adds equiptment
'd' - deletes equiptment
'p' - prints the name and location for the given tag#
'r' - prints the name and tag# for all equiptment in the given
location
'P' - Prints the entire database.
And I have to organize the list by tag number....
#include <stdio.h>
typedef struct LLnode
{
char equiptment[40];
char location[40];
int tag;
int *temp;
int *hold;
struct LLnode *next;
}LListnode;
int main(int argc, char *argv[])
{
LListnode *head = 0;
int *temp;
int *hold;
FILE *f;
char action;
if(argc!=2)
{
printf("command-line parameter failed");
return(1);
}
if((f=fopen(argv[1],"r")==NULL))
{
printf("Unable to open file");
return(1);
}
while(fscanf(f,"%c %s %s %d\n", &action, equiptment, location, tag)!=EOF)
{
switch(action)
{
case a:
insert_list(LListnode &&head, LListnode data);
break;
case d:
if(head==NULL)
printf("Queue empty. Deletion failed.\n");
else
removeitem(head);
break;
case p:
printlist(LListnode &&head, LListnode data);
break;
case r:
break;
case P:
break;
default:
printf("Invalid Entry\n");
break;
}
}
}
void printlist(LListnode, *head)
{
if(head == NULL)
printf("List is empty!\n");
else
printf("***** Printing All Equiptment *****\n");
while(head!=NULL)
{
printf("%s %s %d\n", head->equiptment, head->location, head->tag);
head=head->next;
}
}
int insert_list(LListnode **head, LListnode data)
{
LListnode *temp, *hold;
if(*head!=NULL) //Insertion before the head
{
temp =*head;
hold = NULL;
while(strcmp(temp-> tag, LListnode.tag) < 0 && temp-> next != NULL)
{
if(temp==*head)
{
hold = (LListnode*) malloc(sizeof(LListnode));
if(hold == NULL) return(1); //check malloc
*head = hold;
}
hold->tag = LListnode.tag;
strcpy(hold -> equiptment, LListnode.equiptment);
strcpy(hold -> location, LListnode.location);
hold -> next = temp;
}
else //Insertion into empty list
{
hold =(LListnode*)malloc(sizeof(LListnode));
*head = hold;
temp = NULL;
}
hold->tag = LListnode.tag;
strcpy(hold -> equiptment, LListnode.equiptment);
strcpy(hold -> location, LListnode.location);
hold -> next = temp;
} return(0);
Popular pages
Recent additions