linked list - C LinkedList printing -


i writing program reads txt file , adds contents linked list. then, user interface in command prompt, should able view list, add/delete list, , search item in list using part number see full detail.

currently encountering 2 problems:

1.in switch case 1 in main function, insert node, tempnode->item.dataitem = getinfo(); line works correctly outside of while loop, when put inside while loop, skips taking name of new item , goes directly part number. cannot figure out why this.

2.in switch case 3 in main function, cannot figure out how print contents of searched node, displaynode function won't work on because of union inside structure of node (i want figure out how print without changing struct of node).

this current code:

#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <stddef.h>  typedef struct inventory {     char invname[36];     int  invpartno;     int  invqoh;     float invunitcost;     float invprice; }stock;  struct  node {     union     {         int  nodecounter;         void  *dataitem;     }item;     struct node *link; };  struct node *initlist(); void displaynode(struct inventory *); struct inventory * readdata(file *); void displaylist(struct node *); struct node* getnode(file *); void  add2list(struct node *, struct node *); struct node* searchlist(struct node *, int ); void  deletenode(struct node *, int ); void readfromtext(file *, struct node *); struct inventory *getinfo();  int main(int argc, char* argv[]) {     //create new linked list     struct node *header;     header = initlist();      //open text file , read inputs linked list     file *fp = fopen("input.txt", "r");     readfromtext(fp, header);      //divider     printf("\n--------------------------------------------------\n\n");      int input = 0;     int = 0;     struct node *tempnode = (struct node*)malloc(sizeof node);     //tempnode->item.dataitem = getinfo(); //lets user input info cmd     //add2list(header, tempnode); //inserts new node list      while(a == 0)     {         printf("\n\nenter number select option: \n");         printf("1 insert item \n");         printf("2 delete item \n");         printf("3 item \n");         printf("4 print current list of items \n");         printf("5 exit \n");          scanf("%d", &input);         switch(input)         {         case 1:             //insert node (add2list function)             tempnode->item.dataitem = getinfo(); //lets user input info cmd             add2list(header, tempnode); //inserts new node list          break;          case 2:             printf("enter item number delete \n");             int inumber;             scanf("%d", &inumber);             deletenode(header, inumber);             printf("item %d deleted", inumber);         break;          case 3:             printf("enter item number search \n");             int searchnumber;             scanf("%d", &searchnumber);             //create temp node take value search             //struct node *tempnode = (struct node*)malloc(sizeof node);             tempnode = searchlist(header, searchnumber); //returns node             //display node contents             //displaynode(tempnode->item);         break;          case 4:             displaylist(header);         break;          case 5:             = 1; //ends while loop         break;          default:             = 1; //ends while loop         }      }      //divider     printf("\n--------------------------------------------------\n\n");      return 0; }  struct node *initlist() {     struct node *temp = (struct node*)malloc(sizeof node);      temp->item.nodecounter = 0;     temp->link = null;     return temp; }   void  add2list(struct node *start, struct node *newnode) {     struct node *current = start;      while (current->link != null)         current = current->link;      current->link = newnode;     newnode->link = null;      start->item.nodecounter++; }   struct node* getnode(file *fptr) {     struct node *temp = (struct node*)malloc(sizeof node);      temp->item.dataitem = readdata(fptr);     temp->link = null;      return temp; }   void displaylist(struct node *start) {     struct node *current = start->link;      while (current != null)     {         displaynode((struct inventory *)current->item.dataitem);         current = current->link;      } }   void displaynode(struct inventory *stuff) {     printf("name: %s\n", stuff->invname);     printf("part number: %.5d\n", stuff->invpartno);//printf(“ %.9d”, x)     printf("quantity on hand: %d\n", stuff->invqoh);     printf("unit cost: %0.2f\n", stuff->invunitcost);     printf("price %0.2f\n\n", stuff->invprice); }   struct inventory * readdata(file *fptr) {     struct inventory *temp = (struct inventory *)malloc(sizeof inventory);      if(fptr==stdin)         printf("enter item name: ");     fscanf_s(fptr, "%s", temp->invname);     if(fptr==stdin)         printf("enter item part number: ");     fscanf_s(fptr, "%d", &temp->invpartno);     if(fptr==stdin)         printf("enter item quantity on hand: ");     fscanf_s(fptr, "%d", &temp->invqoh);     if(fptr==stdin)         printf("enter item unit cost: ");     fscanf_s(fptr, "%f", &temp->invunitcost);     if(fptr==stdin)         printf("enter item price: ");     fscanf_s(fptr, "%f", &temp->invprice);      return temp; }  struct node* searchlist(struct node *start, int olddata) {     struct node* current = start;     struct inventory * st = (struct inventory *)current->link->item.dataitem;      while (st->invpartno != olddata && current != null)     {         current = current->link;         if(current->link)             st = (struct inventory *)current->link->item.dataitem;     }     return current; }  void  deletenode(struct node *start, int olddata) {     struct node *current, *oldnode;      current = searchlist( start, olddata);     oldnode = current->link;     current->link = oldnode->link;     free(oldnode);     start->item.nodecounter -= 1; }  void readfromtext(file *fp, struct node *header) {     if( fp != null )     {         while(!feof(fp))         {             struct node *nnode =  (struct node*)malloc(sizeof node);             struct inventory *newnode =  (struct inventory*)malloc(sizeof inventory);              fgets(newnode->invname, 100, fp);             fscanf(fp, " %d %d %f %f ", &newnode->invpartno,&newnode->invqoh,&newnode->invunitcost,&newnode->invprice);             nnode->item.dataitem = newnode;             header->item.nodecounter++;             add2list(header, nnode);         }      } }  struct inventory *getinfo() {     struct inventory *temp =  (struct inventory*)malloc(sizeof inventory);      printf("enter item name: ");     scanf("%99[^\n]", temp->invname); //scans whole line 99 characters or until \n      printf("enter item part number: ");     scanf("%d", &temp->invpartno);      printf("enter item quantity on hand: ");     scanf("%d", &temp->invqoh);      printf("enter item unit cost: ");     scanf("%f", &temp->invunitcost);      printf("enter item price: ");     scanf("%f", &temp->invprice);      return temp; } 

and text file read from:

#1 flat blade screwdriver 12489 36 .65 1.75 #2 flat blade screwdriver 12488 24 .70 1.85 #1 phillips screwdriver 12456 27 0.67 1.80 #2 phillips screwdriver 12455 17 0.81 2.00 claw hammer 03448 14 3.27 4.89 tack hammer 03442 9 3.55 5.27 cross cut saw 07224 6 6.97 8.25 rip saw 07228 5 6.48 7.99 6" adjustable wrench 06526 11 3.21 4.50 

1.in switch case 1 in main function, insert node, tempnode->item.dataitem = getinfo(); line works correctly outside of while loop, when put inside while loop, skips taking name of new item , goes directly part number. cannot figure out why this.

it's because format string passed scanf() on line 241 didn't specify read anything. is, have this:

scanf("%99[^\n]", temp->invname); //scans whole line 99 characters or until \n 

when should this:

scanf("%99s[^\n]", temp->invname); //scans whole line 99 characters or until \n 

2.in switch case 3 in main function, cannot figure out how print contents of searched node, displaynode function won't work on because of union inside structure of node (i want figure out how print without changing struct of node).

well, can print out union this:

printf("tempnode->item.nodecounter=%i\n", tempnode->item.nodecounter); printf("tempnode->item.dataitem=%p\n", tempnode->item.dataitem); 

note of course 1 of these 2 print lines display incorrect/irrelevant information particular node, since union can 1 of constituent members @ time -- example, if particular node has been set hold nodecounter (integer) value, shouldn't accessing dataitem member value in second line, or if node has been set hold data item (pointer) value, shouldn't accessing nodecounter member variable. should have way know of 2 union-members valid 1 , not. typical way add separate member struct (outside of union) set indicate union set -- that's known tagged union. if don't way, you'll have come other mechanism instead.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -