I'm working on a FIFO page replacement algorithm and have it almost working. My code uses scanf() to read in a page number then it adds that item to a linked list, up to 16 pages. However, if the page already exists in the lined list, it does not add the page to this list again. There are three page frames (slots). Everything works properly, but, it does not add that item to the list until scanf() reads another integer to add to the list. I am utterly confused.
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int num;
struct node * next;
} node_t;
void print_list(node_t * head) {
node_t * current = head;
while (current != NULL) {
printf("%d\n", current->num);
current = current->next;
}
}
int fault(node_t * head, int check) {
node_t * current = head;
while(current->next != NULL){
if(current->num == check)
return 0;
current = current->next;
}
return 1;
}
void addNode(node_t * head, int num) {
node_t * current = head;
while (current->next != NULL) {
current = current->next;
}
/* now we can add a new variable */
current->next = malloc(sizeof(node_t));
current->next->num = num;
current->next->next = NULL;
}
int pop(node_t ** head) {
int retnum = -1;
node_t * nextNode = NULL;
if (*head == NULL) {
return -1;
}
nextNode = (*head)->next;
retnum = (*head)->num;
free(*head);
*head = nextNode;
return retnum;
}
///////////////////////////////////////////////////////
int main(){
int num;
int faults = 0;
int n = 0;
int j = 0;
node_t * head = malloc(sizeof(node_t));
if (head == NULL) {
return 1;
}
head->num = -1;
printf("Input page number up to 16 pages. Enter 'q' to quit.\n");
for(j=0;j<16;j++){
if(scanf("%d\n",&num) == 1) {
if (fault(head, num) == 1 && n < 3) {
n++;
faults++;
addNode(head, num);
}
else if (fault(head, num) == 1 && n >= 3) {
pop(&head);
addNode(head,num);
faults++;
}
}
else{
int c = getchar();
if( c == 'q' ){
break;
}
}
if (n == 1 && faults == 1)
pop(&head);
printf("\nPage Table:\n");
print_list(head);
printf("\nInput page number: \n");
}
printf("Number of page faults: %d\n", faults);
}
I ran it through gdb and it doesn't even call the addNode function until the second integer has been scanned.
(And yes I know scanf is garbage, I just didn't want to bother learning how to do something else)
Thanks for the help!
Your code:
if(scanf("%d\n",&num) == 1) {
should be :
if(scanf("%d",&num) == 1) {
And head needs to be initialized.
node_t * head = malloc(sizeof(node_t));
if (head == NULL) {
return 1;
}
head->next = NULL;
Related
#include <stdio.h>
#include <stdlib.h>
/*EVERYTHING WORKS EXCEPT FOR INITIAL -1*/
// Implementing a Node Structure - This represents a node of data
typedef struct _listnode
{
int item; // Data
struct _listnode *next; // Linkage
} ListNode;
// Core Functions of a Linked List
void printList(ListNode *head);
ListNode* findNode(ListNode *head, int index);
int insertNode(ListNode **ptrHead, int index, int value);
void removeNode(ListNode **ptrHead, int index);
int main()
{
// Instantiate a Linked List
ListNode *head = NULL, *temp;
/*
head is a pointer variable that will eventually point to the firstNode.
temp is a pointer variable that points to the lastNode. This is used in from Linked List functions.
*/
int num_to_store;
printf("Enter an Integer to Store (-1 to end):\n");
scanf("%d", &num_to_store);
while (num_to_store != -1)
{
// Initialize a Linked List
if (head == NULL)
{
head = malloc(sizeof(ListNode));
temp = head;
}
else
{
temp->next = malloc(sizeof(ListNode));
temp = temp->next;
}
temp->item = num_to_store;
printf("Enter an Integer to Store (-1 to end):\n");
scanf("%d", &num_to_store);
}
temp->next = NULL;
// Menu-Driven Application
int user_choice, index, value, *p, option_3;
puts("");
printf("----------------\n 1: printList()\n 2: findNode()\n 3: insertNode()\n 4: removeNode()\n-1: End\n----------------\n");
scanf("%d", &user_choice);
while (user_choice != -1)
{
switch(user_choice)
{
case 1:
printList(head);
break;
case 2:
printf("Enter index to search:\n");
scanf("%d", &index);
p = findNode(head, index);
if (p != NULL) printf("Node Item: %d\n", *p);
break;
case 3:
printf("Enter index to insert:\n");
scanf("%d", &index);
printf("Enter value to insert:\n");
scanf("%d", &value);
option_3 = insertNode(&head, index, value);
if (option_3 == 0) printf("NODE INSERTED!\n");
break;
case 4:
printf("Enter index to remove:\n");
scanf("%d", &index);
removeNode(&head, index);
break;
}
// Prompt User to Make Another Selection
puts("");
printf("----------------\n 1: printList()\n 2: findNode()\n 3: insertNode()\n 4: removeNode()\n-1: End\n----------------\n");
scanf("%d", &user_choice);
}
return 0;
}
void printList(ListNode *head)
{
// Linked List is Empty
if (head == NULL)
{
printf("LINKED LIST IS EMPTY!\n");
}
// Linked List is Not Empty
else
{
printf("LINKED LIST: ");
while (head != NULL)
{
printf("%d ", head->item);
head = head->next;
}
puts("");
}
}
ListNode* findNode(ListNode *head, int index)
{
// Linked List is Empty or Index is Invalid
if (head == NULL || index < 0)
{
printf("INVALID!\n");
return NULL;
}
// Linked List is not Empty, Check if Index > len(Linked List)
else
{
while (index > 0)
{
head = head->next;
if (head == NULL)
{
printf("INVALID!\n");
return NULL;
}
index--;
}
printf("INDEX FOUND!\n");
return head;
}
}
int insertNode(ListNode **ptrHead, int index, int value)
{
ListNode *cur, *pre;
// Linked List is Empty || Insert at Index 0
if ((*ptrHead) == NULL || index == 0)
{
cur = *ptrHead;
*ptrHead = malloc(sizeof(ListNode));
(*ptrHead)->item = value;
(*ptrHead)->next = cur;
return 0;
}
// Insert in the Middle
else
{
pre = findNode(*ptrHead, index-1);
if (pre != NULL)
{
cur = pre->next; // This is temporary
pre->next = malloc(sizeof(ListNode)); // L connects to nN
pre->next->item = value;
pre->next->next = cur; // nN connects to R
}
return 0;
}
return -1;
}
void removeNode(ListNode **ptrHead, int index)
{
ListNode *cur, *pre;
// Linked List is Empty
if ((*ptrHead == 0) || index < 0)
{
printf("INVALID!\n");
}
// Remove firstNode
else if (index == 0)
{
cur = findNode(*ptrHead, index);
*ptrHead = cur->next;
free(cur); // Unallocate the memory
}
// Remove Middle
else
{
pre = findNode(*ptrHead, index-1);
cur = findNode(*ptrHead, index);
pre->next = cur->next;
cur->next = NULL;
}
}
Hi all, I am writing a Linked List program in C with the 4 core functions. The functions and everything else works except for when I compile the code and type -1. The program exits. I wonder what is wrong. I tried debugging on code::blocks but nothing showed up.
What am I trying to achieve?
Create a Linked List with no nodes.
Code the 4 core functions of a Linked List (insert/remove/print/search).
What have I tried?
Scattering printf() statements to debug.
Checked on stackoverflow for similar problems.
Viewed the code on geeksforgeeks.
Any idea the cause of the problem might be? And any possible solutions for this type of problem?
When you type -1, the whole while (num_to_store != -1) loop will be skipped, and temp will remain uninitialized, that will make temp->next = NULL; invoke undefined behavior.
Below I have made a simple Linked List in C. The code is currently producing a segmentation fault which I find odd because I was copying an example from our current book. The only thing I did to the code was put the code into the method "addToList". I'm aware the segmentation fault is coming from the method addToList but I do not know where I made a mistake.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node *next;
} Node;
void addToList(Node *, int);
void printList(Node *);
void main() {
int x;
Node *head = malloc(sizeof(Node));
for (x = 1; x < 4); x++) {
printf("Enter an integer: ");
x = scanf("%d");
addToList(head, x);
}
printList(head);
}
void addToList(Node *head, int val) {
Node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = malloc(sizeof(Node));
current->next->val = val;
current->next->next = NULL;
}
void printList(Node *head) {
Node *current = head;
while (current != NULL) {
printf("%d->", current->val);
current = current->next;
}
printf("\n");
}
Any help with telling me what is wrong or where I'm making the mistake would be greatly appreciated.
Look carefully at your code:
int main(void) {
int x;
Node *head = malloc(sizeof(Node));
for (x = 1; x < 4); x++) {
...
addToList(head, x);
}
...
}
You are not initializing the memory, so head->val and head->next are not
initialized. Because of that
while (current->next != NULL) {
current = current->next;
}
will loop an undefined amount of times. The first current->next is most
probably not NULL, so current = current->next get executed. At that point current is pointing to nowhere, hence the undefined behaviour which in your case leads to a segfault.
You have to initialized the memory like this:
Node *head = malloc(sizeof *head);
if(head == NULL)
// error handling
head->next = NULL;
But you could also use calloc, which also sets the memory to 0, thus you don't have to initialize the values (in this case):
Node *head = calloc(1, sizeof *head);
if(head == NULL)
// error handling
You should always check for the return value of malloc/calloc/realloc.
Also note that the signature of the main function can be one of these:
int main(void);
int main(int argc, char **argv);
int main(int argc, char *argv[]);
edit
Another error I've noticed right now:
x = scanf("%d");
That's not how scanf works. You have to pass a pointer, scanf saves the
scanned value through the passed pointer. scanf returns the number of matched
values on success, in this case, success would be 1:
int num;
int ret = scanf("%d", &num);
if(ret != 1)
{
fprintf(stderr, "Could not read value from the user\n");
continue; // to contiune looping
// you could also do a break; and stop the looping, or
// exit(1), etc.
}
// error with scanf
Also don't use the same variable x for the loop iteration and user input,
otherwise you are messing with the loop.
edit
User user3629249 wrote in the comment
good information, however the result will be the first entry in the linked list will contain garbage.
Better to declare head via: Node *head = NULL; and the function addToList() check for NULL and proceed accordingly.
That's right, the head element doesn't save any number in this way.
Option 1: double pointer
Here addToList receives a double pointer. The initialization of head occurs
when *head points to NULL. The function allocates memory for it, initializes
the memory, saves the value and returns. In the concurrent calls of addToList
*head won't be NULL, so addToList looks for the end of the list.
I've made small changes in the way you do malloc and realloc. Also I added
an implementation of freeList which should be used to free the memory:
void addToList(Node **head, int val) {
if(head == NULL)
{
fprintf(stderr, "head cannot be NULL\n");
return;
}
if(*head == NULL)
{
*head = calloc(1, sizeof **head);
head[0]->val = val;
head[0]->next = NULL;
return;
}
Node *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = malloc(sizeof *current->next);
if(current->next == NULL)
return;
current->next->val = val;
current->next->next = NULL;
}
int main(void)
{
int x;
Node *head = NULL;
for (x = 1; x < 4; x++)
{
int val;
printf("Enter an integer: ");
if(scanf("%d", &val) != 1)
{
fprintf(stderr, "Could not read from user. Skipping entry\n");
continue;
}
addToList(&head, val);
}
printList(head);
freeList(head);
return 0;
}
void freeList(Node *head)
{
if(head == NULL)
return;
Node *current = head;
Node *next;
while(next = current->next)
{
free(current);
current = next;
}
free(current); // the last one
free(head);
}
Option 2: addToList returns a pointer to the head
Here addToList takes a pointer to the head. If it's NULL, it allocates
memory and initializes like in the shown above. If head is not NULL, the
functions looks for the last element and the returns the head. On error the
function returns NULL.
Node *addToList(Node *head, int val) {
if(head == NULL)
{
head = calloc(1, sizeof **head);
head->val = val;
head->next = NULL;
return head;
}
Node *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = malloc(sizeof *current->next);
if(current->next == NULL)
return NULL;
current->next->val = val;
current->next->next = NULL;
return head;
}
int main(void)
{
int x;
Node *head = NULL, *tmp;
for (x = 1; x < 4; x++)
{
int val;
printf("Enter an integer: ");
if(scanf("%d", &val) != 1)
{
fprintf(stderr, "Could not read from user. Skipping entry\n");
continue;
}
tmp = addToList(head, val);
if(tmp == NULL)
{
fprintf(stderr, "Not enough memory\n");
freeList(head);
return 1;
}
head = tmp;
}
printList(head);
freeList(head);
return 0;
}
This is a toy version of the code I really want to write:
#include <stdio.h>
#include <stdlib.h>
typedef struct node_s {
char val;
struct node_s *next;
} node;
int n;
char obtain_next_char(){
if (n<100) {
return 'a';
} else {
return 0;
}
}
int main() {
char c;
node *start, *curr;
n = 0;
start = malloc(sizeof(node));
curr = start;
while ((c=obtain_next_char()) != 0) {
curr->val = c;
curr->next = malloc(sizeof(node));
curr = curr->next;
}
curr->next = NULL;
printf("chain made, %d links\n", n);
curr = start;
while (curr != NULL) {
printf(">>>%c<<<\n",curr->val);
curr=curr->next;
}
}
First, regarding obtain_next_char, the function is highly simplified here. With the real function, there is no way to know ahead of time what the return value will be.
Now, the code works, but it results in an empty node at the end of the list, which I'm going to have to get rid of. Can this code be re-written to remedy this deficiency? I am trying to avoid duplicate code at the head of the loop, i.e. I don't want to set up the first node with code outside the loop and then set up the following nodes with the same code inside the loop.
node dummy, *start, *curr;
curr = &dummy;
while ((c=obtain_next_char()) != 0) {
curr->next = malloc(sizeof(node));
curr = curr->next;
curr->val = c;
}
curr->next = NULL;
start = dummy.next;
How about this:
start = NULL;
while ((c=obtain_next_char()) != 0) {
if (!start) {
start = malloc(sizeof(node));
curr = start;
} else {
curr->next = malloc(sizeof(node));
curr = curr->next;
}
curr->val = c;
curr->next = NULL;
}
I'm not sure what you want to achieve with this function because, here you are not scanning in any input :
char obtain_next_char(){
if (n<100) {
return 'a';
} else {
return 0;
}
}
maybe you need this :
char obtain_next_char()
{
char c;
scanf(" %c",&c); //scanning input
if (n<100) //if number of links < 100 then, return character
{
return c;
}
else //else return '0' (ascii character)
{
return '0';
}
}
but it results in an empty node at the end of the list
reason :
The problem is that a new node is being created at the end of while loop
while ((c=obtain_next_char()) != 0)
{
curr->val = c;
curr->next = malloc(sizeof(node)); //new node at end of each iteration
curr = curr->next;
}
So, when you end input, the last empty node remains
solution : (without first node with code outside the loop)
To avoid this, try creating node at the start of each iteration
while ((c=obtain_next_char()) != '0')
{
if(n != 0) //to avoid again creating for head node
{
curr->next = malloc(sizeof(node));
curr = curr->next;
}
curr->val = c;
n++; //increasing number of links
}
This way, you can avoid creating extra empty node at the end of each iteration.
I don't want to set up the first node with code outside the loop
while ((c=obtain_next_char()) != '0')
{
if(n != 0) //any other node
{
curr->next = malloc(sizeof(node));
curr = curr->next;
}
else //for head node
{
start = malloc(sizeof(node));
curr = start;
}
curr->val = c;
n++; //increasing number of links made
}
putting altogether your code would be :
#include <stdio.h>
#include <stdlib.h>
typedef struct node_s {
char val;
struct node_s *next;
} node;
int n;
char obtain_next_char()
{
char c;
scanf(" %c",&c);
if (n<100)
{
return c;
}
else
{
return '0';
}
}
int main() {
char c;
node *start=NULL, *curr=NULL;
n = 0;
while ((c=obtain_next_char()) != '0')
{
if(n != 0) //any other node
{
curr->next = malloc(sizeof(node));
curr = curr->next;
}
else //for head node
{
start = malloc(sizeof(node));
curr = start;
}
curr->val = c;
n++;
}
curr->next = NULL;
printf("chain made, %d links\n", n);
curr = start;
while (curr != NULL) {
printf(">>>%c<<<\n",curr->val);
curr=curr->next;
}
}
input :
1 2 3 4 5 0
output :
chain made, 5 links
>>>1<<<
>>>2<<<
>>>3<<<
>>>4<<<
>>>5<<<
I'm having trouble deleting a node from a linked list by inputting the telephone number of the record ... This is the code that is supposed to do this:
typedef struct record
{
char name[20];
char surname[20];
char telephone[20];
}Record;
typedef struct node
{
Record data;
struct node *next;
}Node;
Node *head = NULL;
void delete() {
Node *n = head;
Node* previous = NULL;
Node *next = n;
int length;
int valid;
char telNumber[20];
char confirm = 1;
do {
valid = 0;
printf(" TELEPHONE NO. (8 digits) : ");
gets();
gets(telNumber);
length = strlen(telNumber);
for (int i = 0; i < length; i++)
{
if (!isdigit(telNumber[i]) || (length != 8))
{
printf("You enterred an invalid number\n");
valid = 1; break;
}
}
} while (valid == 1);
while (n != NULL) {
if (strcmp(&n->data.telephone, telNumber) == 0) {
if (previous == NULL) {
n = n->next;
free(head);
}
else {
previous->next = n->next;
free(n);
n = previous->next;
}
}
else {
previous = n;
n = n->next;
}
}
printf("You have successfully deleted the telephone record");
.
.
.
The record still remains there. Also, when I have two records, if i try to delete the first record, the program doesn't find it
You are not moving the head forward when first node is deleted.
while (n != NULL) {
if (strcmp(&n->data.telephone, telNumber) == 0) {
if (previous == NULL) {
n = n->next;
free(head);
head = n; /* missing*/
}
...
Well there is no error in deletion part. Maybe the head is not initialized and passes the null value to 'n'. So, since 'n' has null value, deletion is not possible. Or updated value of n is not returned or passed properly to the main function.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct _listnode
{
int item;
struct _listnode *next;
} ListNode;
int removeNode(ListNode **ptrHead, int index);
void printList(ListNode *head);
ListNode * findNode(ListNode *head, int index);
int main()
{
ListNode *head = NULL, *temp=NULL;
int i = 0;
int index = 0;
while (1)
{
printf("Enter a integer: ");
scanf("%d", &i);
if (i == -1)
break;
if (head == NULL)
{
head = malloc(sizeof(ListNode));
temp = head;
}
else{
temp->next = malloc(sizeof(ListNode));
temp = temp->next;
}
temp->item = i;
}
removeNode(&head, index);
return 0;
}
void printList(ListNode *head)
{
int i = 0;
if (head == NULL)
return;
while (head != NULL)
{
printf("%d ", head->item);
head = head->next;
}
printf("\n");
}
ListNode * findNode(ListNode *head, int index)
{
if (head == NULL || index < 0)
return NULL;
while (index > 0){
head = head->next;
if (head == NULL)
return NULL;
index--;
}
return head;
}
int removeNode(ListNode **ptrHead, int index)
{
ListNode *pre, *cur,*temp;
if (index >= 0)
{
printf("Enter index to remove: ");
scanf("%d", &index);
if ((pre = findNode(*ptrHead, index - 1)) != NULL)
{
cur = pre->next;
temp = cur;
pre->next = cur->next;
free(temp);
printList(*ptrHead);
}
}
return -1;
}
I successfully revamp my code and now I am able to remove the node and display out, but the whole program just crash after my printList function. It does not go back to remove node and I cant continue removing other indexes.
Output:
Enter a value: 2
Enter a value: 4
Enter a value: 6
Enter a value: 8
Enter a value: -1
Enter index to remove: 2
Current list: 2 4 8
Enter index to remove: 0
Current list: 4 8
Enter index to remove: -1
when you enter index = 0, the inner if block of removenode function will not be executed as the findnode functin returns NULL. But in your output for index = 0 you got node 2 removed. How did u get that?
removeNode(head, index); should be removeNode(&head, index);
And printList(ptrHead); should be printList(*ptrHead); (in removeNode) I have the feeling this piece of code goes crazy that's why your app is not responding anymore.
What compiler do you use? It should have warned you.
In your updated code:
You still do not set next to NULL.
You never increment index when you fill list.
You do not check if scanf() succeeds.
There is no need to use pointer to pointer in removeNode(), you can use:
int removeNode(ListNode *ptrHead, int index);
You have added removeNode() in printList() which is really out of place.
You do not have any free function for the list.
...
Original answer to original code:
Missing header file.
Miss match between function signatures in declaration and definition.
You never set next to NULL.
You do not free list before exit.
...
Code with some comments:
/* MISSING: <stdio.h> */
#include <stdlib.h>
typedef struct _listnode
{
int item;
struct _listnode *next;
} ListNode;
/* Signature miss-match */
int removeNode(ListNode **ptrHead, int index);
void printList(ListNode *head);
ListNode *findNode(ListNode *head, int index);
int main()
{
ListNode *head = NULL, *temp=NULL;
int i = 0;
int index = 0;
while (i != -1)
{
printf("Enter a integer: ");
scanf("%d", &i);
/* If -1 entered, -1 will be added to list. */
if (head == NULL)
{
head = malloc(sizeof(ListNode));
temp = head;
}
else{
temp->next = malloc(sizeof(ListNode));
temp = temp->next;
}
temp->item = i;
/* temp->next never set to NULL */
}
/* Miss match between function signature and call. */
removeNode(head, index);
/* No freeing of list before exit. */
return 0;
}
void printList(ListNode *head)
{
/* Redundant check of head != NULL */
if (head == NULL)
return;
while (head != NULL)
{
printf("%d", head->item);
head = head->next;
}
}
ListNode *findNode(ListNode *head, int index)
{
if (head == NULL || index < 0)
return NULL;
while (index > 0){
head = head->next;
if (head == NULL)
return NULL;
index--;
}
return head;
}
/* Why pass index as argument when it is not used? */
int removeNode(ListNode **ptrHead, int index)
{
ListNode *pre, *cur,*temp;
printf("Enter index to remove: ");
scanf("%d", &index);
/* Here you should check < 0, not != -1.
What if user enters -9999 ? */
if (index != -1)
{
if ((pre = findNode(*ptrHead, index - 1)) != NULL)
{
cur = pre->next;
temp = cur;
pre->next = cur->next;
free(temp);
/* Bad return statement, should be int */
return;
}
/* You only print list if none was removed. */
/* Miss match between function signature and call. */
printList(ptrHead);
}
return -1;
}