I'm new to C so I'm seeking help because I'm stuck.
After creating a linked-list library, from which I can add, delete and print all the nodes the user wants, I should add to the program an additional function of integer type and return a -1 if the value doesn't exist inside of the linked-list. If the value does exist inside the linked list, it should return the position of the element.
For example in this linked-list (a -> b -> c -> d -> NULL) if I want to know the position of c it should return me a 3, if I want to know the position of G it should return me -1
This is the program I was able to make until now:
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
char data;
struct ListNode *nextNode;
};
typedef struct ListNode node;
int main()
{
node *startNodePtr= NULL;
int choice;
char value;
printf("\t\t\tLIST OF CHARACTERS\n");
do
{
printf("\n1.Add New Node \t2.Delete Node \t3.Print Current List \t4.QUIT\n\n");//user friendly interface
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter Character: ");
scanf("\n%c",&value);
insertNode(&startNodePtr,value);//calling the function to add a node
break;
case 2: printf("Delete Character: ");
scanf("\n%c",&value);
deleteNode(&startNodePtr,value);//calling the function to remove a node
break;
case 3: printList(startNodePtr);//calling the function to list the nodes
break;
case 4: continue;//if we type 4 it won't show the default answer
default:printf("\t\tINVALID ANSWER! Please type 1. 2. 3. or 4.\n");// in case we type any other character that is not 1, 2, 3 or 4. In this way the program will not crash
break;
}
} while(choice!=4);//keep adding or deleting nodes until we enter 4 which refers to "QUIT"
return 0;
}
void insertNode(node **sPtr, char add)//function to add a node
{
node *newPtr;
node *curPtr;
node *prevPtr;
newPtr=malloc(sizeof(node));
newPtr->data=add;
newPtr->nextNode=NULL;
prevPtr=NULL;
curPtr=*sPtr;
while(curPtr!=NULL)
{
prevPtr=curPtr;
curPtr=curPtr->nextNode;
}
if (prevPtr==NULL)
{
*sPtr=newPtr;
}
else
{
prevPtr->nextNode=newPtr;
}
}
void deleteNode(node **sPtr, char remove)//function to remove a node
{
node *curPtr;
node *prevPtr;
curPtr=*sPtr;
prevPtr=NULL;
if(curPtr->data==remove)
{
*sPtr=curPtr->nextNode;
free(curPtr);
return;
}
while (curPtr!=NULL)
{
if (curPtr->data==remove)
{prevPtr->nextNode=curPtr->nextNode;
free(curPtr);
return;}
else
{prevPtr=curPtr;
curPtr=curPtr->nextNode;}
}
}
void printList(node *sPtr)//function to list the nodes
{
if (sPtr==NULL)
{
printf("The list is empty!\n");
}
else
{
while (sPtr!=NULL)
{
printf("\n%c-->", sPtr->data);
sPtr=sPtr->nextNode;
}
} printf("NULL\n\n");
}
You have basically solved it already, you just have to count the number of iterations until you find the correct element.
int search(struct ListNode *node, char data) {
int position = 1;
// List is empty
if (node == NULL) {
return -1;
}
while (node != NULL) {
if (node->data == data) {
return position;
}
position++;
node = node->nextNode;
}
// Element was not in the list
return -1;
}
Related
#include <stdio.h>
#include <stdlib.h>
#define N 10
typedef struct music_student *StudentPtr;
typedef struct music_student
{
int student_ID;
double hw_average;
int exam_grade;
int final_grade;
StudentPtr next;
} Music_student;
StudentPtr creatnew(int id,double hw,int exam);
StudentPtr Del(int id,StudentPtr list);
StudentPtr insert(int id,double hw,int exam,StudentPtr list);
void Proportion(float prop,StudentPtr list);
int main()
{
int req,id,exam;
double avg;
float prop;
StudentPtr list=NULL;
printf("Course Management Menu\nPlease choose among the following:\n");
printf("***\n");
printf("0: Quit.\n1: Insert student at first place on the list.\n2: Delete by ID.\n3: Find lucky student.\n4: Change homework exam ratio.\n5: Print List.\n");
while (N<15)
{
if(scanf("%d",&req)!=1)
{
printf("Input Error!");
return 1;
}
while(req>6||req<0)
{
printf("Invalid Selection\n");
if(scanf("%d",&req)!=1)
{
printf("Input Error!");
return 1;
}
}
switch (req)
{
case 1:
printf("insert student ID\n");
scanf("%d",&id);
while(id<10000||id>99999)
{
printf("Invalid student id try_again: value between 10,000 and 99,999\n");
scanf("%d",&id);
}
printf("insert hw grade\n");
scanf("%lf",&avg);
while(avg>100||avg<0)
{
printf("Invalid hw grade: value between 0 and 100\n");
scanf("%lf",&avg);
}
printf("insert exam grade\n");
scanf("%d",&exam);
while(exam>100||exam<0)
{
printf("Invalid exam grade: value between 0 and 100\n");
scanf("%d",&exam);
}
list=insert(id,avg,exam,list);
break;
case 2:
printf("Enter student to expunge from records:\n");
scanf("%d",&id);
Del(id,list);
break;
case 3:
Lucky(list);
break;
case 5:
lilPrint(list);
break;
case 0:
printf ("bye bye!");
return 0;
}
}
}
void lilPrint(StudentPtr list)
{
StudentPtr temp = list;
if(list==NULL)
return;
while (temp != NULL)
{
printf("Student Id: %d\nFinal Grade: %d\n",temp->student_ID,temp->final_grade);
temp = temp->next;
}
return;
}
StudentPtr Del(int id, StudentPtr list)
{
StudentPtr curr = list, prev = NULL;
if(curr->student_ID==id)
{
list=list->next;
free(curr);
curr=list;
}
while (curr != NULL)
{
if (curr->student_ID == id)
{
if (prev == NULL)
{
list = curr->next;
}
else
{
prev->next = curr->next;
}
free(curr);
return list;
}
prev = curr;
curr = curr->next;
}
return list;
}
StudentPtr insert(int id,double hw,int exam,StudentPtr list)
{
StudentPtr cell=creatnew(id,hw,exam);
if(cell==NULL)
{
free(list);
exit(1);
}
cell->next=list;
list=cell;
return list;
}
StudentPtr creatnew(int id,double hw,int exam)
{
StudentPtr cell=(StudentPtr)malloc(sizeof(Music_student));
if(cell == NULL)
{
printf("Allocation failed!");
return NULL;
}
cell->exam_grade=exam;
cell->student_ID=id;
cell->hw_average=hw;
cell->final_grade=(my_round(0.7*cell->exam_grade)+my_round(cell->hw_average*0.3));
cell->next=NULL;
return cell;
}
im trying to enter 4 or 5 nodes in it and the use lilprint to print some info about the linked list.
when i enter some nodes and then delete the firstnode(only delting the first one does this problem)
the program doesnt delete it fully so it only delets the student_id in that node and returns the rest. so when i print it it prints normally but the student_id is trash value .
This definition and using of N with the magic number 10 along with another magic number 15
#define N 10
//...
while (N<15)
//...
makes your code unclear.
Also this typedef declaration for a pointer
typedef struct music_student *StudentPtr;
is a bad idea.
For example if you will write const StudentPtr then it will be equivalent to struct music_student * const instead of const struct music_student * and the last type specification should be used in the function lilPrint because within the function the list is not changed.
void lilPrint( const struct music_student *list);
And by the way you forgot to place the function declaration before main.
In the function insert this statement
free(list);
does not make sense because it does not free all the allocated memory.
The function Del can invoke undefined when the list is empty due to using a null pointer accessing memory as for example in this statement
if(curr->student_ID==id)
Also it seems the list should store nodes with unique student_ID but the function Del is designed such a way that it deletes at most two nodes with the same student_ID due to the following while loop after the first if statement
if(curr->student_ID==id)
{
list=list->next;
free(curr);
curr=list;
}
while (curr != NULL)
{
if (curr->student_ID == id)
{
if (prev == NULL)
{
list = curr->next;
}
else
{
prev->next = curr->next;
}
free(curr);
return list;
}
//...
That is the function should delete either all nodes with the same student_ID or only one node with the given id.
And in main you forgot to assign the returned pointer to the pointer list
Del(id,list);
Instead you need to write
list = Del(id,list);
I've written a linked list program and want to take input with spaces but it's not working.It works fine when I simply use "scanf" with %s but since I want to take input with multiple spaces I tried using "gets" and "puts" I've also tried using scanf("%[^\n]*c"); but on the console it gives me random garbage value for scanf("%[^\n]*c"); and for "gets" it reads blank space,
now let me tell you guys some info about the code and how it works
createNode(); function basically just creates a new node to store in the list and returns the address of this newly created node to the insertend(); function where it aligns the new node at the end of the list and in start=t=newnode start is the head pointer which points to the very first node and t is used to traverse the list until t's value becomes NULL,As you could see in the else part of the insertend(); function we're using another pointer t and storing the value of start in it so that we can traverse the list without losing the the address of the first node which is originally kept in the start pointer.
here's the code ->
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct Node
{
char first[20];
struct Node* next;
};
struct Node* start=NULL;
struct Node* t,*u;
int i=1;
struct Node* createNode() //this function creates a newnode everytime it's called
{
struct Node* create=(struct Node*)malloc(sizeof(struct Node));
return create;
}
int length() //to measure the length of the list.
{
int count = 0;
struct Node* temp;
temp=start;
while(temp!=NULL)
{
count++;
temp = temp->next;
}
return count;
}
void insertend() //to insert a node at the end of the list.
{
int l;
struct Node* newnode = createNode();
printf("Enter Name : ");
fgets(newnode->first,sizeof(newnode->first),stdin);
if(start==NULL)
{
start=t=newnode;
start->next=NULL;
}
else
{
t=start;
while(t->next!=NULL)
t=t->next;
t->next=newnode;
t=newnode;
t->next=NULL;
printf("%s successfully added to the list!",newnode->first);
}
l=length();
printf("The length of the list is %d",l);
}
void display() //to display the list
{
struct Node* dis;
dis=start;
if(start==NULL)
{
system("cls");
printf("No elements to display in the list");
}
else
{
system("cls");
for(int j=1;dis!=NULL;j++)
{
printf("%d.) %s\n",j,dis->first);
dis=dis->next;
}
}
}
int menu() //this is just a menu it returns the user input to the main function
{
int men;
printf("Please select a choice from the options below :-\n\n");
printf("1.) Add at the end of the list\n");
printf("2.) Display list\n");
printf("3.) exit\n");
printf(" Enter your choice : ");
scanf("%d",&men);
return men;
}
int main()
{
while(1)
{
system("cls");
switch(menu())
{
case 1 : insertend();
break;
case 2 : display();
break;
case 3: exit(0);
default : system("cls"); printf("Ivalid choice!Please select an appropriate option!");
fflush(stdin);
break;
}
getch();
}
return 0;
}
gets is not to be used, it has been removed from C standard due to it's lack of security.
If you want to know more read Why is the gets function so dangerous that it should not be used?
If you use [^\n] it should work, though it's also problematic since this specifier does not limit the lenght of the stream to be read only that it must stop when finding a newline character.
I suspect the problem might be in the container rather than in the reading, maybe uninitialized memory, If you provide the struct code it'll be easier to diagnose.
You can try:
fgets(newnode->first, sizeof(newnode->first), stdin)
There is a caveat:
If the inputed stream is larger than the container, the extra characters will remain in the input buffer, you might need to discard them.
EDIT:
So the main problem was the fact that through your code you have lingering characters in the buffer, in the particular case of your fgets input it would catch a '\n' left in the buffer, so it would read it before the inputed stream, leaving it, again, in the buffer.
I added a function to clean up buffer, note that fflush(stdin) leads to undefined behaviour so it's a bad option.
I also added a few small tweaks.
- Note that conio.h is windows specific as is system("cls") and getch()(ncurses.h in Linux systems) so I commented it for this sample.
Live sample here
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct Node
{
char first[20];
struct Node *next;
};
struct Node *start = NULL;
struct Node *t, *u;
void clear_buf(){ //clear stdin buffer
int c;
while((c = fgetc(stdin)) != '\n' && c != EOF){}
}
struct Node *createNode() //this function creates a newnode everytime it's called
{
struct Node *create = malloc(sizeof(struct Node));
return create;
}
int length() //to measure the length of the list.
{
int count = 0;
struct Node *temp;
temp = start;
while (temp != NULL)
{
count++;
temp = temp->next;
}
return count;
}
void insertend() //to insert a node at the end of the list.
{
int l;
struct Node *newnode = createNode();
printf("Enter Name : ");
clear_buf(); //clear buffer before input
fgets(newnode->first, sizeof(newnode->first), stdin);
newnode->first[strcspn(newnode->first, "\n")] = '\0'; //remove '\n' from char array
if (start == NULL)
{
start = t = newnode;
start->next = NULL;
printf("%s successfully added to the list!", newnode->first);
}
else
{
t = start;
while (t->next != NULL)
t = t->next;
t->next = newnode;
t = newnode;
t->next = NULL;
printf("%s successfully added to the list!", newnode->first);
}
l = length();
printf("The length of the list is %d", l);
}
void display() //to display the list
{
const struct Node *dis;
dis = start;
if (start == NULL)
{
system("cls");
printf("No elements to display in the list");
}
else
{
system("cls");
for (int j = 1; dis != NULL; j++)
{
printf("%d.) %s\n", j, dis->first);
dis = dis->next;
}
}
}
int menu() //this is just a menu it returns the user input to the main function
{
int men;
printf("\nPlease select a choice from the options below :-\n\n");
printf("1.) Add at the end of the list\n");
printf("2.) Display list\n");
printf("3.) exit\n");
printf(" Enter your choice : ");
scanf("%d", &men);
return men;
}
int main()
{
while (1)
{
system("cls");
switch (menu())
{
case 1:
insertend();
break;
case 2:
display();
break;
case 3:
exit(0);
default:
system("cls");
printf("Ivalid choice!Please select an appropriate option!");
clear_buf();
break;
}
getch();
}
return 0;
}
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
I am trying to create a linked list with names, for example:
Tom -> David -> John...
In my main function, I have a switch menu where the program asks if you want to create a new list or exit.
When the user choose 1, the program calls insertIntoLinkedList(name, &head) function where the user can add name(s) or type "end" to exit.
Everything works fine, however if the user enter end and choose option 1 again, the program creates a new linked list whereas I want to add names to an existing list.
Can someone please help me to figure out my problem? Thank you for your time.
EDIT
Here is my source code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NAME_SIZE 30
#define EXIT "end"
// Node structure
struct node {
char name[NAME_SIZE];
struct node *next;
};
typedef struct node Node;
typedef struct node* NodePointer;
int userChoice(void);
void insertIntoLinkedList(char [], NodePointer *);
void displayNames(NodePointer);
int nodeCounter = 0;
int main(void) {
int choice = 99;
do {
printf("\n--- MENU ---\n\n");
printf("1.\tCreate a new friend list\n");
printf("2.\tExit o_O");
printf("\n\n------------\n");
printf("Go to:\t");
choice = userChoice();
switch (choice) {
case 1: {
char name[NAME_SIZE] = "";
NodePointer head = NULL;
while(0 != strcmp(name, EXIT)){
printf("Enter new friend name or \"%s\" to return back to the main menu: ", EXIT);
scanf("%s", name);
if(0 != strcmp(name, EXIT)){
insertIntoLinkedList(name, &head);
displayNames(head);
}
}
displayNames(head);
break;
}
case 2: {
printf("\n\nYou have %d node(s) in your linked list. Have a great day.\n\n", nodeCounter);
break;
}
default:
printf("There is no such option. Please choose one of the option from 1 to 2.\n");
}
} while(choice != 2);
return 0;
}
int userChoice() {
int num;
scanf("%d", &num);
return num;
}
void insertIntoLinkedList(char word[], NodePointer *head){
NodePointer newNode = NULL;
NodePointer previous = NULL;
NodePointer current = *head;
newNode = malloc(sizeof(Node));
if(NULL != newNode){
strcpy(newNode -> name, word);
while(NULL != current && strcmp(word, current -> name) > 0){
previous = current;
current = current -> next;
}
if(NULL == previous){
newNode -> next = current;
*head = newNode;
} else {
previous -> next = newNode;
newNode -> next = current;
}
}
}
void displayNames(NodePointer current) {
nodeCounter = 0;
if(NULL == current){
printf("Friend list is empty... I am sorry :(\n\n");
return;
} else {
printf("\nCurrent friend list: ");
while(NULL != current){
nodeCounter++;
printf("%s → ", current -> name);
current = current -> next;
}
printf("\nNumber of friends in your current list:\t%d\n\n", nodeCounter);
}
}
Well U Can Just Declare A New Function For That. Because Every Time You Call This Function Head Is Re-declared .
E.g case 3:printf("\nEnter A New Friend Name:\n");
scanf("%s",name);
insertIntoLinkedList(name, &head);
displayNames(head);
break;
Everything works fine, however if the user enter end and choose option 1 again, the program creates a new linked list whereas I want to add names to an existing list.
The issue is that you have to declare the pointer which sores the head of the list outside the while loop.
NodePointer head = NULL;
do {
....
switch (choice) {
case 1: {
char name[NAME_SIZE] = "";
while(0 != strcmp(name, EXIT)){
....
}
....
}
} while(choice != 2);
Note you have declared the variable in the block scope inside the case. See Scope rules in C.
At the end of the scope the variable is not longer accessible and its content is lost. When you "reach" the code the next time, the you get a completely new initialized variable.
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)