But I am having the same result, what I want is with the same node I can move forth and back, is this possible?
Printable View
But I am having the same result, what I want is with the same node I can move forth and back, is this possible?
I don't understand, do you want to move the node, or do you want to move a pointer back and forth on the list once the list is created? The example code fragment I posted is being used to create the list (in addition to the first allocation that went to head). If you're just trying to back up head to the start of the list after you create the list with the code in your previous post (without my suggested change):
Code:/* ... */
while(head->prev != NULL)
head = head->prev;
And I just noticed that last number is not being printed
Try this:
Code:#include <stdio.h>
#include <stdlib.h>
typedef struct _node
{
int value;
struct _node *next;
struct _node *prev;
}node;
int main(void)
{
node *head = NULL;
int i;
head = malloc(sizeof(node));
head->value = 1;
head->prev = NULL;
for (i = 2; i < 11; i++)
{
head->next = malloc(sizeof(node));
head->next->prev = head;
head = head->next;
head->value = i;
}
head->next = NULL;
while(1)
{
printf("%d\n",head->value);
if(head->prev == NULL)
break;
head = head->prev;
}
printf("\n");
while(1)
{
printf("%d\n",head->value);
if(head->next == NULL)
break;
head = head->next;
}
printf("\n");
system("pause");
return 0;
}
Thanks man.
whats the problem in display function and delete_rec_no???????Code:/*
*Database for storing music cd.
*/
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<cs50.h>
#define MAX 200 //max characters a user can enter
/*prototypes*/
void write_record(void);
int file_exists(void);
void file_new(void);
void file_open(void);
void delete_rec_no(void);
void display(void);
/*structure for music cd*/
typedef struct cd
{
char artist[MAX];
char title[MAX];
int tracks;
int album;
float price;
struct cd *next;
}cd;
/* Global Variables: */
cd *head = NULL; // a pointer that points to start of the cd node.
cd *curr; // pointer that point to current cd node
int changes = 0; // boolean: if changes made or not
char fname[MAX]; // stores a name of a file.
int count; //stores numbers of cds
/*function that shows up on exit*/
void quit(void)
{
printf("\nThank you for using database. Press enter to exit\n");
while(getchar() != '\n');
}
/*Function that asks a question and demands y\n answer*/
int yesno(char prompt[])
{
char answer;
/*Loop will only break if a user enters 'y' or 'n'*/
for(;;)
{
printf("%s (y/n) ",prompt);
scanf (" %c",&answer);
answer = toupper(answer);
if(answer == 'Y' || answer == 'N')
break;
}
if(answer == 'Y') //if answer is 'y' then return 1 else return 0
return 1;
else
return 0;
}
/*Function to see if a file exists*/
int file_exists(void)
{
FILE *fptr = fopen(fname,"rb"); //open a file for reading.
if(fptr == NULL) //if file does not exists return 0.
return 0;
else //if file exists:
{
fclose(fptr); //close file
return 1; //return 1.
}
}
/*Function to ask the user for cd details*/
void input(void)
{
printf("Title: ");
scanf (" %s",curr->title);
printf("Artist: ");
scanf (" %s",curr->artist);
printf("Tracks: ");
scanf (" %d",&curr->tracks);
curr->album = yesno ("Artist?");
printf("Price: ");
scanf (" %f",&curr->price);
}
/*Function to reset everything back*/
void reset_all(void)
{
changes = 0;
fname[0] = '\0';
head = NULL;
curr = NULL;
count = 0;
}
/*Function to show unsaved records*/
void display(void)
{
cd *current;
current = head;
while(current != NULL)
{
printf("Title: %s\n",current->title);
printf("Artist: %s\n",current->artist);
printf("Tracks: %d\n",current->tracks);
printf("%s\n", current->album ? "album" : "single");
printf("Price: %f\n",current->price);
current = current->next;
}
return;
}
int main(void)
{
int choice; //variable that will store choice which the user enters
atexit(quit); //calls 'quit' function b4 program exits
/*Loop that will only break on exit*/
for(;;)
{
printf("\nCurrent file open: %s\n",fname); // print current file opened
/*print menu*/
puts("\nMenu: \n");
puts(" 1. File new");
puts(" 2. File open");
puts(" 3. Write a record");
puts(" 11. Show records");
puts(" 4. Delete a record by number");
puts(" 5. Delete a record by title");
puts(" 6. Save records");
puts(" 7. Save records with a new file name");
puts(" 8. Edit a record");
puts(" 9. Delete a file");
puts(" 10. Exit");
scanf(" %d",&choice); //ask user for choice
switch(choice)
{
case 1:
file_new();
break;
case 2:
file_open();
break;
case 3:
write_record();
break;
case 4:
delete_rec_no();
break;
case 10:
exit(0);
break;
case 11:
display();
break;
}
}
}
/*function that resets everything*/
void file_new(void)
{
/*check if there are changes and then see if a the user wants to discard the records*/
if(changes && yesno("Are you sure you want to discard the records?"))
{
return;
}
reset_all();
return;
}
void file_open(void)
{
/*check if there are changes and then see if a the user wants to discard the records*/
if(changes && yesno("Are you sure you want to discard the records?"))
{
return;
}
reset_all();
/*ask the user the file name they would like to open*/
printf("Enter the file name you would like to open: ");
scanf (" %s",fname);
/*check if the file exists*/
if(!file_exists())
{
printf("File does not exists!\n");
fname[0] = '\0';
return;
}
}
/*function to write a record in dynamic memory*/
void write_record(void)
{
if(head == NULL) // check to see if head has not yet been assigned:
{
head = malloc(sizeof(cd)); // allocate memory for head
curr = head; // set curr to head
input(); //call input function
++count; //count increases by 1 everytime user writes a record
}
else //if head has already been assigned:
{
curr->next = malloc(sizeof(cd)); //malloc next node
curr = curr->next; //set curr to next node
input(); //call 'input' function
++count; //count increases by 1 everytime user writes a record
}
return;
}
/*Function to delete a record*/
void delete_rec_no(void)
{
int number; //stores the number which user wants to be deleted
cd *prev;
cd *current;
int i;
if(count == 0)
{
printf("There are no cds input!\n");
return;
}
do
{
printf("Enter the number of cd which you would like to remove: ");
scanf(" %d",&number);
if(number > count || number < 0)
printf("The number you entered is invalid. There are currently %d cds.\n",count);
else
break;
}while(number > count || number < 0);
current = head;
prev = head;
for(i = 2; i <= number; i++)
{
if(number == 1)
{
head = current->next;
free(current);
}
if(i == number)
{
prev->next = current->next;
free(current);
}
prev = current;
current = current->next;
i++;
}
// display();
return;
}
What's wrong is you haven't paid a blind bit of attention so far (re: global variables in your other recent thread).
Start making your functions accept parameters and return results.
All these void foo(void) functions are a waste of time and effort.
> Okay, give me a hint which variables should I make local?
All of them.
Start making use of parameters and return results, and the whole sorry mess of globals goes away.
You could create a list structure that contains a pointer to the first node (head) and the last node (tail) (for quick append to end of list), and pass a pointer to this structure in your functions. The "curr" pointer should be local to the functions. If you need a global "curr" pointer, put it in the list structure.