When I run these methods My list never grows and I don't know why. The head and tail are always equal. Is there something wrong with my pointers or struct setup? I spent 4 hours on this switching things around and I just can't seem to find why things aren't working unless it's a pointer thing (which I am learning and not good with)..
typedef struct EmployeeStruct
{
char LastName[10];
char FirstName[10];
struct EmployeeStruct *next;
} Employee;
struct EmployeeStruct *head,*tail, *temp;
Employee* hireEmployee(Employee* head, char LastName[10], char FirstName[10])
{
// exit if max number of employees is reached
if(eNum > MAX_EMPLOYEES)
{
printf("Sorry, but you can't hire any more employees right now\n");
return 0;
}
// insert at tail
newHire = head;
while(newHire->next != NULL)
{
newHire = newHire->next;
printf("nope\n");
}
// allocate memory
newHire->next = malloc(sizeof(Employee));
if(newHire == NULL)
{
printf("Memory allocation failed");
return 0;
}
newHire->next = NULL;
// insert values into this node
strcpy(newHire->LastName, LastName );
strcpy(newHire->FirstName, FirstName );
newHire->EmployeeNumber = eNum;
tail = newHire;
//printf("%d\n",newHire->EmployeeNumber);
eNum+=1;
//printf("%d\n",newHire->EmployeeNumber);
return newHire;
}
int main()
{
char choice;
char first[20];
char last[20];
int i = 0;
// allocate memory
head = malloc(sizeof(Employee));
if(head == NULL)
{
printf("Memory allocation failed");
return 0;
}
head->next = tail;
while(TRUE)
{
// prompt user for choice
printf("Please choose from the following options:\n");
printf("a: Hire new employee\nb: Promote employee\nc: Delete employee\nd: Display roster\ne: Exit\n");
scanf("\n%c", &choice);
switch(choice)
{
case 'a':
printf("New employees first name:\n");
scanf("%s", first);
printf("New employees last name:\n");
scanf("%s", last);
tail = hireEmployee(head,last,first);
temp = head;
while(temp->next != NULL)
{
temp = temp->next;
printf("nope\n");
}
temp->next = tail;
tail = temp->next;
printf("A%d: %s %s\n", tail->EmployeeNumber,tail->FirstName,tail->LastName);
tail = tail->next;
printf("A%d: %s %s\n", tail->EmployeeNumber,tail->FirstName,tail->LastName);
tail->next = NULL;
//printEmployees(head);
break;
case 'b':
//TBD
break;
}
}
}
** Works now, it was these four errors:
(newHire == NULL)
(head == NULL)
newHire =newHire->next;
temp->next = tail;
BTW, I always do my homework and attend class, and I was simply stuck on a small piece of a MUCH larger program. So thank you to those who didn't insult me and actually gave useful advice. I truly appreciate it.
Here are your mistakes
newHire->next = malloc(sizeof(Employee));
the return type of malloc is void* so change it to
newHire->next = (EmployeeStruct *)malloc(sizeof(Employee));
if(newHire == NULL)
it will never be NULL change it to
if(newHire->next == NULL)
also change
newHire->next = NULL;
you have to traverse one more time link
newHire=newHire->next;
newHire->next = NULL;
I think the problem is in here
temp->next = tail;
tail = temp->next;
inside your case 'b'
because it making temp->next to its head
Related
My goal is to create a linked list and store elements inside that list.
struct node
{
int a;
struct node *b;
} p,*temp,*head;
void create ( struct node *temp)
{
char c;
temp = malloc (sizeof(struct node));
printf("enter data\n");
scanf(" %d",&temp->a);
printf("do you want to insert another node y/n\n");
scanf("%s", &c);
if (c=='y')
{
create(temp->b);
}
else if ( c=='n')
{
temp->b= NULL;
temp=&p;
return;
}
}
void traverse ( struct node *head)
{
while(head != NULL)
{
printf("%d ",head->a);
head=head->b;
}
}
int main ()
{
int i,j,k,l,m,n;
do{
if(i==1)
{
printf("enter data\n");
scanf("%d",&p.a);
create (p.b);
}
else if ( i==2)
traverse(temp);
}
while(i!=3);
printf("%d",temp->a);
}
I can't recover the elements once i've stored them. When I try to traverse the list, it only gives me the first element of the list and nothing else.
In main
do {
if(i==1)
{
...
}
else if ( i==2)
traverse(temp);
}
while(i!=3);
must be something like
do {
if (scanf("%d", &i) != 1)
break;
if(i==1)
{
...
}
else if ( i==2)
traverse(temp);
}
while(i!=3);
to know what the user want (i not initialized in your code)
in create
scanf("%s", &c);
is wrong because c is a char rather than a string
Do not mix read of int and char because you will read newline and space when reading a character, so read a string for c, for instance
char c[2];
...
scanf("%1s", &c);
if (*c == 'y')
...
else if (c == 'n')
...
the return in else branch is useless, and in case the answer is not 'y' or 'n' you do nothing so you do not set temps, probably you have to just check if 'y' and all other answers must be considered to be 'n', or you need to ask again for the choice
in create you assign the local variable temps, that has no effect on p.b in main, you need to get a node** for instance
in main temp is used but never set elsewhere, and the variables j,k,l,m,n are useless. You also ask for the data in main while you also do in create, must not be done in main. The way you manage your variables do not allow you to modify/print the list
I encourage you to not use global variables the must you can, and to not use the same name for a global and local variable like you do for temp and head because that do not help the reader of your code
A proposal solving the problems :
#include <stdlib.h>
#include <stdio.h>
struct node
{
int a;
struct node * b;
};
/* flush input up to the end of the line */
void flush()
{
int c;
while ((c = getchar()) != '\n') {
if (c == EOF)
exit(-1);
}
}
void create (struct node ** l)
{
/* go to the end of the list */
while (*l != NULL)
l = &(*l)->b;
for (;;) {
char c[2];
int v;
printf("enter data\n");
if (scanf("%d", &v) != 1) {
puts("invalid value");
flush();
}
else {
*l = malloc (sizeof(struct node));
(*l)->a = v;
(*l)->b = NULL;
l = &(*l)->b;
for (;;) {
printf("do you want to insert another node y/n\n");
scanf("%1s", c);
if (*c == 'y')
break;
else if (*c == 'n')
return;
}
}
}
}
void traverse ( struct node *head)
{
while(head != NULL)
{
printf("%d ",head->a);
head = head->b;
}
putchar('\n');
}
int main ()
{
int i;
struct node *head = NULL;
for (;;) {
puts("enter choice : 1 to create new node, 2 to print list, 3 to exit");
if (scanf("%d", &i) != 1)
flush();
switch(i) {
case 1:
create(&head);
break;
case 2:
traverse(head);
break;
case 3:
return 0;
default:
break;
}
}
}
Compilation and execution :
/tmp % gcc -pedantic -Wextra -Wall t.c
/tmp % ./a.out
enter choice : 1 to create new node, 2 to print list, 3 to exit
2
enter choice : 1 to create new node, 2 to print list, 3 to exit
1
enter data
11
do you want to insert another node y/n
y
enter data
22
do you want to insert another node y/n
n
enter choice : 1 to create new node, 2 to print list, 3 to exit
2
11 22
enter choice : 1 to create new node, 2 to print list, 3 to exit
1
enter data
3
do you want to insert another node y/n
n
enter choice : 1 to create new node, 2 to print list, 3 to exit
2
11 22 3
enter choice : 1 to create new node, 2 to print list, 3 to exit
4
enter choice : 1 to create new node, 2 to print list, 3 to exit
3
I encourage you to add the free of the list
There are several issues here:
struct node
{
int a;
struct node *b;
} p,*temp,*head;
Why do you declare global variables and use it as function parameters ? Global variables are available globally, there is no need to pass them into functions. On the other hand, global variables should be avoided and used with care, so it would be better to create local variables (e.g. in main function) and pass them as parameters into next functions.
void create ( struct node *temp)
{
char c;
temp = malloc (sizeof(struct node));
printf("enter data\n");
scanf(" %d",&temp->a);
printf("do you want to insert another node y/n\n");
scanf("%s", &c);
if (c=='y')
{
create(temp->b);
}
else if ( c=='n')
{
temp->b= NULL;
temp=&p;
return;
}
}
This function looks wrong. temp function parameter is actually an internal function's variable, that is not an in/out parameter. In that case you can assign to the temp variable, but it will not for a list. Also temp function parameter shadows temp global variable. It is also a good think to return status of operation, usually '0' means no error, any other error value.
Another thing is to keep things as simple as possible. This will allow for more reuseability and goes with single responsibility principle. If function actually performs two tasks it should be split into two functions.
One more thing, you allocate memory dynamically but never free the memory. This will lead into memory losses.
A possible implementation of your list could be:
#include <stdio.h>
#include <stdlib.h>
typedef struct _node_t
{
int a;
struct _node_t * next;
} node_t;
static node_t * head = NULL;
static node_t * tail = NULL;
node_t * create(void)
{
node_t * temp = malloc(sizeof(node_t));
if (NULL == temp)
{
return NULL;
}
printf("Enter data\n");
scanf("%d", & temp->a);
return temp;
}
void append(node_t * data)
{
if (NULL == head)
{
head = tail = data;
}
else
{
tail->next = data;
tail = tail->next;
}
tail->next = NULL;
return;
}
int add_data(void)
{
node_t * data = NULL;
char answer = 'y';
data = create();
if (NULL == data)
{
return 1;
}
append(data);
return 0;
}
void traverse(void)
{
node_t * current = NULL;
for (current = head; current != NULL; current = current->next)
{
printf("%d ", current->a);
}
printf("\n");
return;
}
void cleanup(void)
{
node_t * current = head;
while (NULL != current)
{
head = head->next;
free(current);
current = head;
}
return;
}
int main(int argc, char ** argv)
{
int option = 3;
do
{
printf("Enter option:\n 1 - add data\n 2 - traverse list\n 3 - exit\n\n");
scanf("%i", & option);
switch (option)
{
case 1:
if (0 != add_data())
{
printf("ERROR:: Cannot allocate memory.\n");
cleanup();
return 1;
}
break;
case 2:
traverse();
break;
default:
if (option > 3)
{
printf("ERROR:: Improper option, try again.\n");
}
break;
}
}
while (option != 3);
cleanup();
return 0;
}
I tried making it as simple as possible and keeping your logic there.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
// create a new node
struct node* create(int data)
{
struct node *n = malloc(sizeof(struct node));
n->data = data;
n->next = NULL; // by default its not NULL, so we must set it
return n;
}
void traverse(struct node *head)
{
struct node *tmp = head;
while(tmp != NULL)
{
printf("%d\t",tmp->data);
tmp = tmp->next;
}
}
void cleanup(struct node *head)
{
struct node *cur = head;
struct node *next;
while(cur != NULL)
{
next = cur->next;
free(cur);
cur = next;
}
}
int main ()
{
int data;
struct node *head, *tmp;
// head node is always created
printf("enter data\n");
scanf("%d",&data);
head = tmp = create(data);
if(head == NULL) return -1;
// now we loop until we don't want to create any more nodes
while(1)
{
char another;
printf("do you want to insert another node y/n\n");
scanf(" %c", &another); // ignore all the previous whitespace
if(another == 'y')
{
printf("enter data\n");
scanf("%d",&data);
tmp->next = create(data);
tmp = tmp->next;
}
else break;
/*
// anything but 'y' breaks the loop, but it can be set to continue if neither 'y' nor 'n' was read:
else if(another == 'n') break;
else continue;
*/
}
traverse(head);
cleanup(head);
}
EDIT: as #Diodacus stated, i added cleanup and checking if malloc returned NULL
It seems that my code isn't working because I cannot add any string to the list. It seems like the list remains empty. After every input of a string like "asd" or a single character, the message that appears every time is:
List is empty
Also, I don't know if the 'delete' function could work, because the 'insert' function isn't working.
Here's my code(It is a bit long):
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct listnode
{
char data[100];
listnode *nextPtr;
};
Here is my insert function. PS: I want to insert names so they can be alphabetically ordered.
void insert(struct listnode *strPtr, char value[])
{
struct listnode *previousPtr, *currentPtr, *newPtr;
newPtr=(listnode*)malloc(sizeof(listnode));
strcpy(newPtr->data,value);
newPtr->nextPtr=NULL;
previousPtr=NULL;
currentPtr=strPtr;
if(newPtr!=NULL)
{
while(currentPtr!=NULL && strcmp(currentPtr->data,value)>0)
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}
if(previousPtr==NULL)
{
newPtr->nextPtr=strPtr;
strPtr=newPtr;
}
else
{
previousPtr->nextPtr=newPtr;
newPtr->nextPtr=currentPtr;
}
}else printf("%s was not inserted. Insuffiecient memory!",value);
}
Here is my delete function
const char *del(struct listnode* strPtr, char value[])
{
struct listnode *previousPtr, *currentPtr, *tempPtr;
if(strcmp(value, strPtr->data)==0) /*if the first node shall be
deleted*/
{
/*delete node*/
tempPtr=strPtr;
strPtr=strPtr->nextPtr;
free(tempPtr);
return value;
}
else
{
previousPtr=strPtr;
currentPtr=strPtr->nextPtr;
while(currentPtr!=NULL && strcmp(value, currentPtr->data)!=0)
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}
if(currentPtr!=NULL)
{
tempPtr=currentPtr;
previousPtr->nextPtr=currentPtr->nextPtr;
free(tempPtr);
return value;
}
}
return '\0';//daca nu s-a gasit nodul
}
void instructions(void)
{
printf( "Options:\n"
" 1 to insert a name in the list.\n"
" 2 to delete a name from the list.\n"
" 3 to exit.\n" );
}
void printList(struct listnode *currentPtr)
{
if(currentPtr==NULL)
printf("List is empty\n");
else
{
printf("List is:\n");
while(currentPtr!=NULL)
{
printf("%s -->",currentPtr->data);
currentPtr=currentPtr->nextPtr;
}
}
}
int Empty(struct listnode *strPtr)
{
return strPtr == NULL;
}
int main()
{
struct listnode* startPtr;
startPtr=NULL;
int optiune;
char nume[100];
instructions();
printf("->> ");
scanf("%d",&optiune);
Here is the menu
while(optiune!=3)
{
switch(optiune)
{
case 1:
printf("Enter name:");
fflush(stdin);
gets(nume);
insert(startPtr, nume);
printList(startPtr);
break;
case 2:
printf("Search by name to delete from list:");
gets(nume);
if(!Empty(startPtr))
{
if(del(startPtr, nume))
{
printf("%s a fost sters din lista!\n");
printList(startPtr);
}else printf("%s nu a fost gasit!\n",nume);
}else
printf("List is empty!");
break;
case 3:
break;
default:
printf("No such option!\n\n");
instructions();
break;
}
printf("\n->> ");
scanf("%d",&optiune);
}
printf("Execution stopped.\n" );
return EXIT_SUCCESS;
}
I think I got it to work.
First thing I changed in your code was defining your struct as a typdef.
typedef struct{
char data[100];
struct listnode *nextPtr;
}listnode;
This is how I learned and it makes it easier to declare variables of this struct type, since we can just type "listnode" instead of "struct listnode" all the time.
Since you didn't give us the full code, I had to remove some things so I could compile it. As you are just asking why the list remains empty after the "insert" function I think this is fine for you.So here's the "main()" and "printlist()" functions that I used:
void printList(listnode *strPtr){
listnode *currentPtr = malloc(sizeof(listnode));
//Setting the ptr to run the list
currentPtr = strPtr;
if(strPtr == NULL) printf("Fail\n");
//Printing the list,while next is different than NULL keep going
printf("\nListing:\n");
while(currentPtr->data != NULL){
printf("%s\n",currentPtr->data);
currentPtr = currentPtr->nextPtr;
}
}
int main(){
char nume[128];
listnode *startPtr = NULL;//Initializing the fisrt ptr
//Reading the name
printf("Enter name:\n");
scanf("%s",nume);
//Inserting and printing the list
insert(&startPtr,nume);
insert(&startPtr,"Hello");
printList(startPtr);
return 0;
}
Some pretty basic stuff. Now to the main point:
As our friends told us in the comments, in C, the pointer is passed by value. It means that whatever changes you make in the function, it will not modify the startPtr variable in main().
To fix that all he have to do is to add one extra '*' on the function parameters. This means that we are now passing the strPtr argument by reference.
Here's the final function:
void insert(listnode **strPtr,char value[]){
listnode *previousPtr, *currentPtr,*newPtr;
//Allocating the nodes
newPtr = malloc(sizeof(listnode));
currentPtr = malloc(sizeof(listnode));
//Initializing the new node and setting the currentPtr
strcpy(newPtr->data,value);//Copying the name to the new node
newPtr->nextPtr = NULL;
currentPtr = strPtr;
//Previous is NULL
previousPtr = NULL;
if(newPtr != NULL){
//If there's already a node in the list let's find the right location
//For the new one(sorting)
while(currentPtr != NULL && strcmp(currentPtr->data,value) > 0){
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
//If previous is NULL than this is the first node,no while iterations
//Note:strPtr is passed by reference,extra '*' needed
if(previousPtr == NULL){
newPtr->nextPtr = *strPtr;
*strPtr = newPtr;
}else{
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
*strPtr = newPtr;
}
}else{
printf("%s was not inserted. Insuffiecient memory!",value);
}
}
Note that we are also using an extra '*' when setting the newPtr to strPtr.This will modify the actual variable startPtr from main().
From that I hope you can also fix the delete function!
Don't forget to free the memory!
Also, don't forget to comment your code. It's really important to help you and other people to understand it, no matter how easy it might be!
Cheers!
I'm trying to write a program that enqueue, dequeue, delete a chosen number and print the list. I have problems with the dequeue that i think is because of the menu part when you write a number, I've tried to fix it but the it removes the last number and not the first. The print shows the wrong number and when I tried to solve that problem I got the same problem as I had in dequeue. It's sometinhg wrong in delete but i cant figure it out.
I appreciate all the help i can get
edit:
I've changed it a lot and now everything else works except delete. I want delete to find the number i enter and delete it.
queue.c
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
struct node
{
int info;
struct node *ptr;
int next;
}*first, *last, *temp, *first1;
void enq(int data);
void deq();
void empty();
void display();
void create();
void delete_queue();
int count = 0;
void main()
{
int no, ch;
printf("\n 1 - Enqueue");
printf("\n 2 - Dequeue");
printf("\n 3 - Delete");
printf("\n 4 - Display");
printf("\n 5 - Exit");
create();
while (1)
{
printf("\n Enter choice : ");
scanf_s("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf_s("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
printf("Enter data : ");
scanf_s("%d", &no);
delete_queue(no);
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
void create()
{
first = last = NULL;
}
void enq(int data)
{
if (last == NULL)
{
last = (struct node *)malloc(1 * sizeof(struct node));
last->ptr = NULL;
last->info = data;
first = last;
}
else
{
temp = (struct node *)malloc(1 * sizeof(struct node));
last->ptr = temp;
temp->info = data;
temp->ptr = NULL;
last = temp;
}
count++;
}
void display()
{
first1 = first;
if ((first1 == NULL) && (last == NULL))
{
printf("Queue is empty");
return;
}
while (first1 != last)
{
printf("%d ", first1->info);
first1 = first1->ptr;
}
if (first1 == last)
printf("%d", first1->info);
}
void deq()
{
first1 = first;
if (first1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
}
else
if (first1->ptr != NULL)
{
first1 = first1->ptr;
printf("\n Dequed value : %d", first->info);
free(first);
first = first1;
}
else
{
printf("\n Dequed value : %d", first->info);
free(first);
first = NULL;
last = NULL;
}
count--;
}
void delete_queue()
{
int retval = -1;
if (first)
{
struct node *temp = first;
first = first->next;
if (!first) { last = first; }
retval = temp->next;
free(temp);
}
return retval;
}
void empty()
{
if ((first == NULL) && (last == NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
}
Let me start with a few points of advice about design and style:
I do not recommend this:
typedef struct node {
int data;
struct node *next;
} node;
you are typedefing struct node to node. while it is not illegal, it is confusing. I would recommend
typedef struct _node {
int data;
struct _node *next;
} node;
Additionally, I do not recommend use of global variable with static storage class to keep track of your queue, instead you should create a queue in your main. Use global variables only when you have compelling reasons to do so.
Do remember that when you get rid of your global variable, you will need to rewrite your enqueue dequeue delete etc... functions to take in a queue_c * as parameter (because it wont have access to queueref any more)
Now for the reason that your code is not working properly and #Weather Vane alluded to:
you have a big problem in your delete function.
int delete(int data)
{
int result = 0;
node *curr_ptr; //pointer just created and not initialized
node *prev_ptr; //not initialized
node *temp_ptr; //not initialized
while (curr_ptr != NULL)
//curr_ptr was just created, where is it pointing? fatal error here
{
//inside this block lets imagine curr_ptr is pointing to a valid
//node in the global queue
if (curr_ptr->data == data)
{
result = 1;
if (curr_ptr->next != NULL)
{
temp_ptr = curr_ptr;
//both pointers point to the same thing
destroy_node(temp_ptr);
//now you just destroyed both nodes
prev_ptr->next = curr_ptr->next;
//the first time this block runs prev_ptr is uninitialized
//so prev_ptr->next will most likely seg fault
//this happens for example if you call this function
//for the first time with a long queue
}
else
{
temp_ptr = curr_ptr;
queueref.last = prev_ptr;
prev_ptr->next = NULL;
destroy_node(temp_ptr);
//again you are destroying both curr_ptr and temp_ptr
}
}
curr_ptr = curr_ptr->next;
prev_ptr = prev_ptr->next;
return result;
}
}
Perhaps it would be better if you think edge cases very carefully and rethink some of the logic from scratch. (test edge cases as you go)
I can't figure out why my code breaks. Every thing works, except when I try to reverse the linked list. It "stops working." (This function; void reverseList(struct produceItem** inHead)) Any ideas? I've been stuck on this for some time now. I think the issue might be that it doesn't read a NULL some where, but I cannot figure it out.
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
struct produceItem{
char produce[20];
char type[20];
char soldBy[20];
float price;
int quantityInStock;
struct produceItem *next;
};
struct produceItem* append(struct produceItem *inHead, char *nextProduce, char *nextType, char *nextSoldBy, char *nextPrice, char *nextQuantityInStock){
struct produceItem *temp;
temp =(struct produceItem *)malloc(sizeof(struct produceItem));
strcpy(temp->produce, nextProduce);
strcpy(temp->type, nextType);
strcpy(temp->soldBy, nextSoldBy);
temp->price = atof(nextPrice);
temp->quantityInStock = atoi(nextQuantityInStock);
if(inHead == NULL){
inHead = temp;
temp->next = NULL;
}
else{
temp->next = inHead;
inHead = temp;
}
return inHead;
}
struct produceItem* readData(struct produceItem *inHead){
const char comma[2] = ",";
char *produceTemp;
char *typeTemp;
char *soldByTemp;
char *priceTemp;
char *quantityInStockTemp;
char dataLine[100];
char fileName[] = "AssignmentTwoInput.txt";
FILE *inputFile;
printf("\nFile %s has been read.\n\n", fileName);
inputFile = fopen(fileName, "r");
if( inputFile == NULL){
printf("%sList Does not exist\n", fileName);
return;
}
while((fgets(dataLine, 100, inputFile)) != NULL){
produceTemp = strtok(dataLine, comma);
typeTemp = strtok(NULL, comma);
soldByTemp = strtok(NULL, comma);
priceTemp = strtok(NULL, comma);
quantityInStockTemp = strtok(NULL, comma);
inHead = append(inHead, produceTemp,typeTemp,soldByTemp,priceTemp,quantityInStockTemp);
}
fclose(inputFile);
return inHead;
}
void display(struct produceItem *inHead){
int i = 1;
if(inHead == NULL){
printf("List does not exist.\n");
return;
}
printf("=========================================================================\n");
printf("Item # Produce Type Sold By Price In Stock\n");
printf("==========================================================================\n");
for(i=1; i<27; i++){
//while(inHead != NULL){
printf("\n%5d", i);
printf(" %-12s ", inHead->produce);
printf("%-16s ", inHead->type);
printf("%-16s ", inHead->soldBy);
printf("%3.2f ", inHead->price);
printf("%8d", inHead->quantityInStock);
inHead = inHead->next;
//i++;
}
printf("\n\n");
}
exportData(struct produceItem *n){
char fileName[] = "AssignmentTwoOutput.txt";
FILE *exportFile;
int i =1;
if(n == NULL){
printf("List does not exist.\n");
return;
}
exportFile = fopen(fileName, "w");
//printf("hi");
fprintf(exportFile,"================================================================\n");
//printf("hi");
fprintf(exportFile,"Item# Produce Type Sold By Price In Stock\n");
fprintf(exportFile,"================================================================\n");
for(i=1; i<27; i++){
//while(n->next != NULL){
//printf("hi");
fprintf(exportFile,"\n%3d", i);
fprintf(exportFile," %-12s ", n->produce);
fprintf(exportFile,"%-15s ", n->type);
fprintf(exportFile,"%-15s ", n->soldBy);
fprintf(exportFile,"%3.2f ", n->price);
fprintf(exportFile,"%8d", n->quantityInStock);
n = n->next;
}
printf("\nYour data has been written to AssignmentTwoOutput.txt, thank you.\n\n");
fclose(exportFile);
}
//void recursiveReverse(struct node** head_ref)
//{
//struct node* first;
// struct node* rest;
/* empty list */
// if (*head_ref == NULL)
//return;
/* suppose first = {1, 2, 3}, rest = {2, 3} */
//first = *head_ref;
//rest = first->next;
/* List has only one node */
//if (rest == NULL)
//return;
/* reverse the rest list and put the first element at the end */
//recursiveReverse(&rest);
//first->next->next = first;
/* tricky step -- see the diagram */
//first->next = NULL;
/* fix the head pointer */
//*head_ref = rest;
//}
void reverseList(struct produceItem** inHead){
//printf("1");
struct produceItem* first;
struct produceItem* follow;
//printf("2");
if (*inHead==NULL){
// printf("3");
printf("List does not exist.\n");
return;}
first = *inHead;
//printf("4");
follow = first->next;
if(follow==NULL)
//printf("5");
return;
reverseList(&follow);
first->next->next = first;
first->next = NULL;
*inHead = follow;
}
int main (void){
int choice;
struct produceItem *head;
while(1){
printf("List of Operations\n");
printf("===============\n");
printf("1. Stock Produce Department\n");
printf("2. Display Produce Inventory\n");
printf("3. Reverse Order of Produce Inventory\n");
printf("4. Export Produce Inventory\n");
printf("5. Exit\n");
printf("Enter you choice: ");
scanf("%d", &choice);
if(choice <= 0 || choice > 5){
printf("Enter a valid response please: \n");
exit(0);
}
switch(choice){
case 1:
//reading from thefile
head = readData(head);
break;
case 2:
//displays the list
display(head);
break;
case 3:
//reverse the list
reverseList(&head);
break;
case 4:
exportData(head);
break;
case 5:
//Exits the operation
printf("Exiting, Thank you.");
exit(0);
break;
}
}
}
in this recursive function:
void reverseList(struct produceItem** inHead){
//printf("1");
struct produceItem* first;
struct produceItem* follow;
//printf("2");
if (*inHead==NULL){
// printf("3");
printf("List does not exist.\n");
return;}
first = *inHead;
//printf("4");
follow = first->next;
if(follow==NULL)
//printf("5");
return;
reverseList(&follow);
first->next->next = first;
first->next = NULL;
*inHead = follow;
}
This function will run recursively through the list.
Ok lets look at your code:
2 local variables (does nothing really)
code to exit if input is null (mostly for safety)
You set your local variables to the current and next node.
code to exit if this is the last node in the list.
You call your function recursively.
Note that at this stage actually nothing has happened.
This means that your processing (the lines at the end of your function) will happen in reverse order of the list. From back to front.
This seems correct for what you want to do.
Next line:
first->next->next = first;
This seems correct as it would set the next of the "following" node to point to this one. As in change the direction of the next pointer within the list.
Now you have this line:
first->next = NULL;
Remember that we said your "processing" of the nodes will happen in the reverse order. So effectively one by one you will set all your next pointers to NULL. Which I think will completely disconnect your queue completely?
The last line I understand is simply to enable you to find the new head pointer for your queue and that seems fine.
btw doing this kind of algorithm with recursion is a bad idea normally.
As you can easily cause stack overflow problems if your list is getting long.
Also if for some reason your list has a problem and it is circular then it is difficult to detect that and not cause problems.
Also generally in terms of performance this type of recursive algorithm will be slower than just a proper loop implementation.
Pseudo code to "reverse" list:
reverseList(** head)
{
current = head
prev = null
next = null
int i = 0;
while (current)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
i++;
if (i > MAX) reportError();
}
head = prev;
}
I'm new to C and need your assistance please.
I have posted already 2 parts of the big Linked List issues I was having because i didn't want to bombard you all with a big code so i'm doing this in parts. This is a new question though so if you could explain me I would really appreciate it as always.
I have a function on my doubly linked list that is supposed to delete a string that's on my list but I seem to have a problem it's not deleted anything. In fact it get's me stuck and i can't input anything. I would like to paste my code for you to maybe understand better with what i'm dealing with. Love your help!
This is my struct node:
struct node
{
char data[100];
struct node *previous; // Points to the previous node
struct node *next; // Points out to the next node
}*head, *last;
This is my function called: delete_from_middle
char delete_from_middle(char words[99])
{
struct node *temp,*var,*temp1;
temp=head;
strcpy(temp->data, words);
while (temp!=NULL)
{
if (temp->data == words)
{
if (temp->previous==NULL)
{
free(temp);
head=NULL;
last=NULL;
return 0;
}
else
{
var->next=temp1;
temp1->previous=var;
free(temp);
return 0;
}
}
else
{
var=temp;
temp=temp->next;
temp1=temp->next;
}
}
printf(" Data deleted from list is %s \n", words);
return 0;
}
And this is where i assign it on my main
int main()
{
char loc[99];
char words[99];
int i, dat;
head=NULL;
printf("Select the choice of operation on link list");
printf("\n1.) Insert At Begning\n2.) Insert At End\n3.) Insert At Middle");
printf("\n4.) Delete From End\n5.) Reverse The Link List\n6.) Display List\n7.)Exit");
while(1)
{
printf("\n\n Enter the choice of operation you want to do ");
scanf("%d",&i);
switch(i)
{
case 1:
{
printf("Enter a word you want to insert in the 1st node ");
scanf(" %s",words);
insert_beginning(words);
display();
break;
}
case 2:
{
printf("Enter a word you want to insert in the last node ");
scanf(" %s",words);
insert_end(words);
display();
break;
}
case 3:
{
printf("After which data you want to insert your new data ");
scanf(" %s",words);
printf("Enter the data you want to insert in list ");
scanf(" %s",loc);
insert_after(words, loc);
display();
break;
}
case 4:
{
delete_from_end();
display();
break;
}
case 5:
{
printf("Enter the value you want to delete");
scanf(" %s",words);
delete_from_middle(words);
display();
break;
}
really sorry if the code seems long but i really tried to figure how to do it.
Any help?
please let me know if i'm missing something or if my question is not correctly asked.
Well, the line
if (temp->data == words) {
certainly does not do what you expect it to do: You are comparing pointers, not the strings behind the pointers! Use strcmp() for that.
To be precise: the == operator is written to compare two arrays, but these arrays decay into pointers to their first elements, the code is equivalent to
if (&temp->data[0] == &words[0]) {
But that is probably a lesson you should learn later on, it confuses enough seasoned C programmers...
You start the function by setting temp to point to head of the list. Then you replace head->data with your search string. Obviously now head->data == words and previous == null so head is deallocated and function returns zero
Your code is very complex and seems to contain more than one problem. You should probably cut the function into smaller parts to find errors easier.
ex:
struct node *find(char words[99])
{
struct node *temp;
temp = head;
while (temp != NULL)
{
if (strcmp(temp, words) == 0)
return temp;
}
return NULL;
}
void deleteNode(struct node *n)
{
if (n->previous != NULL)
n->previous->next = n->next;
else // n is head
head = n->next;
if (n->next != NULL)
n->next->previous = n->previous;
else
last = n->previous;
free(n);
}
char delete_from_middle(char words[99])
{
struct node *target = find(words);
if (target != NULL)
deleteNote(target);
}
There is a problem in your code.
Try:
Traverse the whole linked list
like
for(node temp=head; temp!=null; temp=temp->next)
{
if(temp->data==words)
{
//update links
temp->previous=temp->next;
temp->next->previous=temp->previous->next;
break;
}
}
free (temp); //delete/free node
First in while loop, if condition is always true because of this line -
strcpy(temp->data, words);
Now there are two parts in if -> first ( if(temp->previous == NULL)
) if there is only a single element in the list then list will set
to null by setting head = NULL and last = NULL;
Second part -> If list has more than one elements then your
operations are
var->next=temp1; //This one is wrong cause var is not assigned its only declared
temp1->previous = var; //this one is causing problem free(temp); you freed the memory that was allocated for temp but
//forget to delete temp .
return 0;
//At last your element is not actually deleted and you freed the
//memory that was allocated for that element.
For deleting a specific element simplest code is ->
char delete_from_middle(char words[99])
{
struct node *h;
h = head;
while ( h! = NULL ) {
if ( strcmp(h->data, words) == 0)
{
if ( h->previous == NULL )
{
if( head == last )
{
head = NULL;
last = NULL;
}
else
{
head = head->next;
head->previous = NULL;
}
}
else
{
if( h->next!=NULL ) h->next->previous = h->previous;
h->previous->next = h->next;
}
printf(" Data deleted from list is %s \n", words);
free(h);
return 0;
}
h = h->next;
}
printf(" Data not found");
return 0; }
There are so many problems in the code, while deleting the integrity of the list is not maintained. The following should be the way to delete a node from the list:
void delete_from_middle(char words[99])
{
struct node *temp;
temp=head;
while (temp!=NULL)
{
if (strcmp(temp->data,words)==0) //this is the data we are looking for, go and delete this
{
if (temp->previous==NULL) //this is the head
{
head=temp->next;
temp->next->previous=NULL;
free(temp);
printf(" Data deleted from list is %s \n", words);
return;
}
else if(temp->next==NULL) //this is last node
{
temp->previous->next=NULL;
free(temp);
printf(" Data deleted from list is %s \n", words);
return;
}
else
{
temp->previous->next=temp->next;
temp->next->previous=temp->previous;
free(temp);
printf(" Data deleted from list is %s \n", words);
return;
}
}
else //this node does not contain the data, go to the next node
{
temp=temp->next;
}
}
//search ended
printf(" Data not found\n");
return;
}