Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] Need Help Add Some Conditions - Data Structure In C Language

DATA STRUCTURE fflush

  • This topic is locked This topic is locked
7 replies to this topic

#1 LadyYeon

LadyYeon

    CC Lurker

  • New Member
  • Pip
  • 8 posts
  • Programming Language:C
  • Learning:C

Posted 27 May 2012 - 04:07 AM

#include <stdio.h>
#include <stdlib.h>
#define FUNC_SIZE 4  // size of pointer function array
// structure declare
typedef struct node_record {
int nData;
struct node_record *next;
} _node;

typedef _node* list_node;  // structure pointer's data type declare
list_node head = NULL;  // structure pointer variable declare
int list_menu(int*);	// menu function
int input_num(int*, int);   // input function
int insert_node(void);   // input in node function
int delete_node(void);   // delete node's content function
int print_node(void);   // print function
int end_proc(void);	// quit function
// main function
int main(void)
{
int nSelect=0, nRetry=1;  // select menu,  repeat variable
int (*func_node[FUNC_SIZE])(void)={insert_node, delete_node, print_node, end_proc};  // pointer function array declare and initialize to functions
while(nRetry)  // repeat
{
  list_menu(&nSelect);  // call main function
  nRetry = func_node[nSelect-1]();   //call correspond function
}
return 0;
}

// menu function
int list_menu(int* pSelect)
{
puts("********MENU********");
puts("* 1. Insert            *");
puts("* 2. Remove        *");
puts("* 3. Print             *");
puts("* 4. End              *");
puts("********************");
do
{
  fflush(stdin);
  printf("*Choice : ");
  scanf_s("%d", pSelect);   // input
} while(*pSelect > 4 || *pSelect < 1);   // choose range
return 0;
}

//  input function
int input_node(int* pNum, int nFlag)
{
printf("*%s : ", (nFlag == 1)?"Input Value":"Want to delete Value");
scanf_s("%d", pNum);  // input
return 0;
}

// input function of list
int insert_node(void)
{
list_node temp, ptr, preptr;  // structure pointer variable declare
int nData=0;   // variable for input
preptr = head;  // structure pointer variable "head" is stored in "preptr"
temp = (list_node)malloc(sizeof(_node));   // allocate memory in variable "temp"
input_node(&nData, 1);
if(temp == NULL)  // if memory doesn't be allocate very well
{
  printf("*Memory wasn't enough.");
  exit(1);   // quit  unusually
}
temp->nData = nData;  // store input data in "temp"'s parameter "nData"
if(head == NULL || head->nData > nData)  // the case "head" is "NULL" OR the case "head"'s parameter nData is larger than input value
{
  temp->next = head;  // "head"'s address(at this time) is stored in "next" parameter of "temp" structure pointer
  head = temp;	// "temp" is stored in "head" so "head" is changed
}
else  // exception
{
  ptr = preptr->next;  // address value that "next" of "preptr(head)" has is copied in "ptr"
  while(ptr != NULL)  // the case that "ptr" isn't NULL
  {
   if.(ptr->nData > nData)  //compare with "nData"value of "ptr" and Input value - the case that ptr->nData is larger than Input value
	break;   // quit repeat sentence
   preptr = ptr;   // copy "ptr"(in this time) to "preptr" (increase  one by one)
   ptr = ptr->next;  // "ptr->next"(in this time) copy address value to "ptr"(increase  one by one)
  }
  temp->next = ptr;   // store address of "ptr"(in this time) in "temp->next"(temp --> ptr)
  preptr->next = temp;  // store address of "temp" in "preptr->next"(preptr --> temp)
}
return 1;
}
// delete function of list
int delete_node(void)
{
list_node preptr, ptr;  // structure pointer variable declare
int nData=0;	 // data to delete
input_node(&nData, 2); // input to delete value
if(head == NULL)   // if "head" is "NULL", there are no data
{
  puts("*Data does not exist.");
}
else if(head->nData == nData)  // if "head->nData" is same with data that want to delete
{
  preptr = head;	// store address of "head" in "preptr"
  head = preptr->next;  // address value of "preptr(head)->next" is replaced to "head"("head" is change to address value that "head" points)
  free(preptr);	 // clear "preptr" memory
  puts("*Data was erased.");  // print message
  return 1;
}
else
{
  preptr = head;	// store address of "head" in "preptr"
  ptr = preptr->next;   // store next address that "preptr(head)" (that stored above is point) in "ptr"
  while(ptr != NULL && ptr->nData != nData)
   preptr = ptr;  
   ptr = ptr->next;  
  }

  if(ptr == NULL)
   puts("*Corresponding data does not exist.");
  else
  {
   preptr->next = ptr->next;  // give address of "ptr->next" to "preptr->next" so "pretr" points next address that "ptr" is pointing
   free(ptr);	   // clear memory about "ptr"
   puts("*Data was erased.");  // print message
  }
}
return 1;
}

// list print function
int print_node(void)
{
int nIdx=1;   // print counter
list_node preptr;  // structure pointer variable
preptr = head;  // copy "head" to "preptr"

if(preptr == NULL)  // there is no data when "preptr(head)" is "NULL"
  puts("*Data does not exist.");
else  // case that isn't "NULL"
{
  printf("*Display Data : ");
  while(preptr != NULL)  // repeat when "preptr" isn't "NULL"
  {
   printf("%d ",  preptr->nData); //print data
   preptr = preptr->next;  // substitute "preptr->next"'s address value(in this time) to "preptr"
  }
  printf("\n");
}
return 1;
}

// quit function
int end_proc(void)
{
list_node temp;  // structure pointer variable

while(head != NULL)   // case that "preptr(head)" isn't "NULL"
{
  temp = head;	// copy "head" to "preptr"
  head = temp->next;  // copy "temp(head)->next"'s address to "head"
  free(temp);	 // clear "temp" variable memory of this time
}
puts("Quit the program.");	// print quit message
return 0;






When user choose 1. Add Data, program have to receive only INTEGER data.
If user input UN-INTEGER data(like 5.69, 8.3), it should print "Error : Input value is invalid".

And, when user choose 3. Display Data, program have to print only 5 data in each line.
(like this


Display Data :
1 6 16 8 30
9 65 25 7 9
15 36 95 4 2
.
.
.
)

I can't make it. I hold it almost 5 hours, but I can not find how I make it.
plzz help.. it make me mad...:Q
  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 27 May 2012 - 04:15 AM

LadyYeon, Welcome to CodeCall!

Does the code you posted compile without errors?
  • 0

#3 LadyYeon

LadyYeon

    CC Lurker

  • New Member
  • Pip
  • 8 posts
  • Programming Language:C
  • Learning:C

Posted 27 May 2012 - 04:21 AM

Glad to meet youkernelcoder :D
This code doesn't have errors BUT I have to add some conditions(input only integer and print 5 data in each line) and I don't know how should I do in this code....
  • 0

#4 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 27 May 2012 - 04:36 AM

Well, while I tried to compile your code, I got few errors. Following are the errors in your code.
  • The first error code is in the insert_node method.
    while(ptr != NULL)  // the case that "ptr" isn't NULL	
    {	
    if.(ptr->nData > nData)  //compare with "nData"value of "ptr" and Input value - the case that ptr->nData is larger than Input value	
    break;   // quit repeat sentence	
    preptr = ptr;   // copy "ptr"(in this time) to "preptr" (increase  one by one)	
    ptr = ptr->next;  // "ptr->next"(in this time) copy address value to "ptr"(increase  one by one)	
    }	
    temp->next = ptr;   // store address of "ptr"(in this time) in "temp->next"(temp --> ptr)	preptr->next = temp;  // store address of "temp" in "preptr->next"(preptr --> temp)
    The error is in the if construct. There is an extra dot (.) after the if keyword.
  • Look at the end of delete_node method and there is error.
    if(ptr == NULL) 	
    puts("*Corresponding data does not exist.");	
    else	
    {	
    preptr->next = ptr->next;  // give address of "ptr->next" to "preptr->next" so "pretr" points next address that "ptr" is pointing	
    free(ptr);      // clear memory about "ptr"	
    puts("*Data was erased.");  // print message	
    }	
    }	
    return 1;	}
    The return statement is outside the scope of the method and there is also an extra } at the end.
  • There is another error : the method end_proc is not ended with the }. .
Well, We do want to help you but I think it is your responsibility to supply us errors free code (if the errors are not your problem you want answer for).
Here is the print_node method that will print as your expectation...

int print_node(void)
{
int nIdx=1;   // print counter
int count = 0;
list_node preptr;  // structure pointer variable
preptr = head;  // copy "head" to "preptr"

if(preptr == NULL)  // there is no data when "preptr(head)" is "NULL"
puts("*Data does not exist.");
else  // case that isn't "NULL"
{
printf("*Display Data : \n");
while(preptr != NULL)  // repeat when "preptr" isn't "NULL"
{
printf("%d ",  preptr->nData); //print data
preptr = preptr->next;  // substitute "preptr->next"'s address value(in this time) to "preptr"
count++;
if (count % 5 == 0) {
printf("\n");
}
}
printf("\n");
}
return 1;
}

I bet there is error (unintentional) in your delete_node method. The first while construct should be scoped with curly braces.

preptr = head;    // store address of "head" in "preptr"
ptr = preptr->next;   // store next address that "preptr(head)" (that stored above is point) in "ptr"
while(ptr != NULL && ptr->nData != nData) { // you missed this brace
     preptr = ptr;   
     ptr = ptr->next;  
} // you missed this brace.

  • 0

#5 LadyYeon

LadyYeon

    CC Lurker

  • New Member
  • Pip
  • 8 posts
  • Programming Language:C
  • Learning:C

Posted 27 May 2012 - 04:52 AM

Oh, that errors are made when I translate my comments to English. All comment are another language at first time.
And I'm very thankful about your coding. I couldn't think to declare variable count that counting the number of data:D
  • 0

#6 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 27 May 2012 - 04:59 AM

Change your input_node method to follows which will take integer input only. However, be careful the floating point inequality comparison.
[code=auto:0]
// input function
int input_node(int* pNum, int nFlag)
{
float input;
printf("*%s : ", (nFlag == 1)?"Input Value":"Want to delete Value");
scanf_s("%f", &input); // input
*pNum = (int)input;
if ( (input - *pNum) != 0.0f) {
printf("Error : Input value is invalid. Please input integers only.\n");
return input_node(pNum, nFlag);
}
return 0;
}
  • 0

#7 LadyYeon

LadyYeon

    CC Lurker

  • New Member
  • Pip
  • 8 posts
  • Programming Language:C
  • Learning:C

Posted 27 May 2012 - 05:07 AM

Wow... I couldn't think of it... amazing!! You are awesome kernelcoder!! :thumbup1:
  • 0

#8 Roger

Roger

    Skadoosh!

  • Administrator
  • 1222 posts
  • Programming Language:C, PHP
  • Learning:Others

Posted 27 May 2012 - 09:00 AM

This topic has been marked as SOLVED. If you have a similar question or topic, go back to the subforum and start a new topic to continue discussions.
  • 0

New around here? Click here to register and start participating in under a minute?

Or do a quick search and you may find the answer you're looking for.






Also tagged with one or more of these keywords: DATA STRUCTURE, fflush

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download