Linked list always show empty - c

REQUIREMENT / QUESTION 2
In computing, the process identifier (normally referred to as the process ID or PID), is a number used by most operating system kernels, to uniquely identify an active process. The PIDs are usually allocated on a sequential basis, beginning with 0 and rising to a maximum value that varies from system to system. Create a link list to store the PIDs. To create a new PID, createPID() function is used.
Each PID is inserted into the list at the beginning of the list, using insertPID() function. Once a process is completed, that particular PID is deleted, using the deletePID() function.
The assessment will be done based on the following criteria:
A proper writing of C codes and its structure
The ability of program to be compiled and executed
Implementation of correct programming techniques
Complete documentation and correct submission
Note: You must write C programming codes for this assignment.
I already create a code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
void createPID ();
void insertPID (struct node *start,int x);
struct node * deletePID (struct node * start,int x);
void displayPID(struct node *start);
struct node * start;
int main()
{
createPID();
insertPID(start,0);
insertPID(start,1);
insertPID(start,2);
displayPID(start);
start=deletePID(start, 3);
displayPID(start);
}
void createPID(){
struct node *start = NULL;
}
void displayPID(struct node *start)
{
struct node *p;
if(start == NULL)
{
printf("List is empty\n");
return;
}
printf("List is :");
p=start;
while(p!=NULL)
{
printf("%d\t",p->info);
p=p->link;
}
printf("\n");
}
/*End of displayList()*/
void insertPID(struct node * start, int data)
{
struct node *temp,*p;
p=start;
while (p!=NULL)
{
if(p->link==NULL)
break;
p=p->link;
}
temp=(struct node *)malloc (sizeof (struct node));
temp->info=data;
if(p==NULL)
start=temp;
else
{
temp->link= p->link;
p->link= temp;
}
}
struct node* deletePID(struct node * start,int x){
struct node *temp, *p;
if(start == NULL)
{
printf("List is empty\n");
return start;
}
/*Deletion of first node */
if(start->info == x)
{
temp=start;
start= start->link;
free(temp);
return start;
}
/*Deletion in between or at the end */
p=start;
while (p->link != NULL)
{
if (p-> link -> info== x)
break;
p=p-> link;
}
if(p->link==NULL)
printf("Element %d not in list \n\n",x);
else
{
temp=p->link;
p->link=temp->link;
free (temp);
}
return start;
}
The result always empty list. Need help to figure out what is the problem ?

Problem is in insertPID function. start as argument of function is local variable, it hides global start and the line below
start = temp;
only modifies local start, global start is not affected and it is always NULL.
If you want to update start pointer you need to pass it by pointer-to-pointer:
void insertPID (struct node **start,int x);
//...
insertPID(&start,0);
insertPID(&start,1);
insertPID(&start,2);
//...
void insertPID(struct node ** start, int data)
{
//...
p=*start;
//...
if(p==NULL)
*start=temp;
//...
}
void createPID(){
struct node *start = NULL;
}
I assume this function wants to set start (global) to 0 ? So it should be:
void createPID(){
start = NULL;
}
Your version introduces local start variable, so global is not affected.
But the call of createPID is superfluous here because start as global variable is initialized to 0 before main starts executing.

Related

how to remove code duplications in this code?

I am a new C99 programmer and want the help of the community on this one.
I wrote the following function which receives two pointers for a node (Or simply Node) and a pointer to a pointer to node (Or *Node) an merges them together into one sorted Node.
This is Given:
typedef struct node_t {
int x;
struct node_t *next;
} *Node;
typedef enum {
SUCCESS = 0,MEMORY_ERROR, EMPTY_LIST, UNSORTED_LIST, NULL_ARGUMENT,
} ErrorCode;
int getListLength(Node list);
bool isListSorted(Node list);
This is the code I wrote:
ErrorCode mergeSortedLists(Node list1, Node list2, Node *merged_out)
{
if (merged_out==NULL)
return NULL_ARGUMENT;
if (list1==NULL || list2==NULL)
return EMPTY_LIST;
if (!isListSorted(list1) || !isListSorted(list2))
return UNSORTED_LIST;
Node ptr=*merged_out;
int list1_len=getListLength(list1),list2_len=getListLength(list2);
for (int i=0;i<list1_len+list2_len;i++)
{
int min=0;
if (list1!=NULL && (list2==NULL || (list2!=NULL && list1->x<=list2->x))){
min = list1->x;
list1=list1->next;
}
else{
min=list2->x;
list2=list2->next;
}
ptr->x=min;
if (i==list1_len+list2_len-1){
ptr->next=NULL;//The next for the last Node should be Null
continue;
}
ptr->next=malloc(sizeof(*ptr));
if (ptr->next==NULL){
//We should Free all previosly allocated memory
//except for the first node since it was not allocated via malloc
return MEMORY_ERROR;
}
ptr=ptr->next;
}
ptr=NULL;
return SUCCESS;
}
But after reviewing my code I was told it has a lot of code duplications (more precisely inside he for loop) which should be corrected by using external functions, do u notice any code duplications? and how to fix that, any ideas?

trouble in with reverse viewing/search function in double linked list C programming

hey guys this is my first time doing double linked list so I'm not very sure what I'm doing here ,need some help to check the code,thanks, here is what I have done with comments included.
The functions I have done here are print,print reverse,count elements in the linked list,and search function to determine whether this node exist
void printListfow() //print the list in forward manner
{
CLR;
struct node *tmpval; //declare a temp storage
if(start==NULL) //if 1st node = null,then nth is inside,nth to print
{
printf("List is empty\n");
return;
}
tmpval=start; //assign the head/start to temp storage to retrieve data in 1st node
printf("List of customer details: \n");
while(tmpval!=NULL) //keep doing till it is NULL/the end
{
printf("%d ", tmpval->detail); //print the 'detail' which is in the node temp is pointing at
tmpval=tmpval->next; //assign next node to the temp storage so that it can be printed again
}
}
void printListrev() //print in reverse manner
{
CLR;
struct node *tmpval; //temp storage
if(start==NULL) //
{
printf("List is empty\n");
return;
}
tmpval=start; //assign start to tmpval to retrieve value
printf("List of customer details: \n");
tmpval=tmpval->prev //move backward and assign the data to tmpval
printf("%d",tmpval->detail) //print it
}
void count() //count total number of records
{ struct node *x;
x=start; //assign value of start to temp storage
int ctr=0; //initialize counter
while(x!=NULL)
{
x=x->next; //keep going to next node and then increase the counter
ctr++;
}
printf("Number of customer records are %d\n",ctr);
}
int getNode(node *tmp ,int cust) //when user wants to delete a customer ID and its details, this will search through the list,then if found,pass the value to another function for deletion
{
tmp=tmp->cust;
while(tmp!=NULL)
{
if(tmp->detail == cust) //check if detail[ID stored] is same as requested[cust]
{
return 1;
}tmp=tmp->next; //if not same,then move to next one
}return 0;
}
thanks!
In context to printListrev():
Unless this is a circular doubly linked list, in which case last element is preceded by the first, start would have previous element NULL. So, there is no point in accessing the previous field of start, as you do here:
tmpval=start;
...
tmpval=tmpval->prev;
You can keep another pointer to end of the list for this purpose.
Other alternatives include:
recursive function:
void printrev(struct node *s)
{
if (s == NULL)
return;
printrev(s->next);
printf("%d ", s->detail);
}
iterative function:
void printrev()
{
struct node *end;
for (end = start; end->next != NULL; end = end->next)
;
for (; end != NULL; end = end->prev)
printf("%d ", end->detail);
}
Your getNode is of limited use. Suppose, if you want to delete element, your getnode would only return whether, element is present or not. Say it is present, your deleteNode function would still have to iterate to the appropriate element in the list before deleting it.
This could be solved by getNode returning pointer to the node:
node *getNode(int x)
{
node *t;
for (t = start; t != NULL; t = t->next)
if (t->detail == x)
return t;
return t;
}
Now, you can code delete as follows:
void delNode(node *n)
{
n->prev->next = n->next;
n->next->prev = n->prev;
free(n);
}
And call as follows:
node *n;
if ((n = getNode(x)) != NULL)
delNode(n);
I've assumed that you struct is:
struct node {
int detail;
struct node *next;
struct node *right;
};
typedef struct node * node;
In printListrev() you are printing only one node not going reverse.
One major problem you are doing is in getNode() ,you are changing the address of a local pointer but the original pointer still points where it previously.
If so then how you are going to delete that node ,as you can't know the node address after this function returns.
Are you going to call the getNode() for all the node ,if so that is not a good if you have many nodes.

Using Pointer to Pointer of Struct while adding nodes in the binary tree

I want to know the reason why do we use, pointer to pointer while inserting nodes in the binary tree.
But, While traversing the binary tree, we just refer the tree by simple pointer to the root node. But why while inserting node?
Can anyone help me in providing the reason or reference link to understand why it is pointer to pointer .
/*This program clears out all the three methods of traversal */
#include<stdio.h>
#include<stdlib.h>
/* Let us basically describe how a particular node looks in the binary tree .... Every node in the tree has three major elements , left child, right child, and and the data. */
struct TreeNode {
int data;
struct TreeNode *leftChild;
struct TreeNode *rightChild;
};
void inorder(struct TreeNode *bt);
void preorder(struct TreeNode *bt);
void postorder(struct TreeNode *bt);
int insert(struct TreeNode **bt,int num);
main()
{
int num,elements;
struct TreeNode *bt;
int i;
printf("Enter number of elements to be inserted in the tree");
scanf("%d",&num);
printf("Enter the elements to be inserted inside the tree");
for(i=0;i<num;i++)
{
scanf("%d",&elements);
insert(&bt,elements);
printf("\n");
}
printf("In Order Traversal \n");
inorder(bt);
printf("Pre Order Traversal \n");
preorder(bt);
printf("Post Order Traversal \n");
postorder(bt);
return 0;
}
int insert(struct TreeNode **bt,int num)
{
if(*bt==NULL)
{
*bt= malloc(sizeof(struct TreeNode));
(*bt)->leftChild=NULL;
(*bt)->data=num;
(*bt)->rightChild=NULL;
return;
}
else{
/* */
if(num < (*bt)->data)
{
insert(&((*bt)->leftChild),num);
}
else
{
insert(&((*bt)->rightChild),num);
}
}
return;
}
void inorder(struct TreeNode *bt){
if(bt!=NULL){
//Process the left node
inorder(bt->leftChild);
/*print the data of the parent node */
//printf(" %d ", bt->data);
/*process the right node */
inorder(bt->rightChild);
}
}
void preorder(struct TreeNode *bt){
if(bt)
{
//Process the parent node first
printf("%d",bt->data);
//Process the left node.
preorder(bt->leftChild);
//Process the right node.
preorder(bt->rightChild);
}
}
void postorder(struct TreeNode *bt){
if(bt)
{
//process the left child
postorder(bt->leftChild);
//process the right child
postorder(bt->rightChild);
//process the parent node
printf("%d",bt->data);
}
}
"I want to know the reason why do we use, pointer to pointer while inserting nodes in the binary tree. But, While traversing the binary tree, we just refer the tree by simple pointer to the root node. But why while inserting node?"
We actually don't even need the code to answer this. If you want to modify (write to) data in an external function in C, you need to have the address of the data. Just like:
main() {
int x = 2;
change_me(x);
printf("%d\n", x); // prints 2
}
void change_me(int x){
x++;
}
has no meaning. You're (in this example) getting a local copy of the vairable, any changes made to the value are only within the local scope. If you want those changes to propagate back to the calling function you need the address:
main() {
int x = 2;
change_me(&x);
printf("%d\n", x); // prints 3
}
void change_me(int* x){
(*x)++;
}
The same applies to pointers. In the example of a linked list, if I want to print the values, I need to traverse the tree and read data. I don't need to change anything so just the pointer will do. However if I want to modify the tree:
struct node{
int val;
sturct node* next;
};
main() {
struct node* head = malloc(sizeof(struct node));
head->val = 3;
insert_a_node_in_front(head);
}
insert_a_node_in_front(node * ptr) {
struct node* temp = ptr;
ptr = malloc(sizeof(struct node));
ptr->val = 5;
ptr->next = temp;
}
Well, guess what? We didn't actually just insert that node because head's value never changed. It's still pointing to the original node with a val==3. The reason is the same as before, we tried to change the value of the local copy of the parameter. If we want those changes to stick it needs the address of the original copy:
insert_a_node_in_front(&head);
}
insert_a_node_in_front(node ** ptr) {
struct node* temp = (*ptr);
(*ptr) = malloc(sizeof(struct node));
(*ptr)->val = 5;
(*ptr)->next = temp;
}
It's because of the first part of insert where it mallocs a new struct treenode. If you only passed in a struct treenode * this would look something like this:
int insert(struct TreeNode *bt,int num)
{
if(bt==NULL)
{
bt= malloc(sizeof(struct TreeNode));
(bt)->leftChild=NULL;
(bt)->data=num;
(bt)->rightChild=NULL;
return;
}
...
}
The problem with that would be that bt is local to insert so the bt in main would be unchanged. So you pass in a pointer to main's bt, which allows insert to change it.
a very good tips on using pointer is given here :
Try this basics : -
http://www.geeksforgeeks.org/how-to-write-functions-that-modify-the-head-pointer-of-a-linked-list/

Linked list in C - first node data incorrect and can't figure it out

I have a multithreaded C program, where one thread creates random times and numbers, and stores them in a sorted linked list, and the second thread compares the the time in the first node to the current system time and deletes the node whenever the times are equal. However, 0 0 gets added to the linked list first, and I can't find where. It's not being added in the insert function as far as I can tell. Any help would be greatly appreciated. Here is the relevant code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
pthread_mutex_t count_mutex;
/* Link list node */
struct node
{
int roomNo;
time_t time;
struct node* next;
};
void printList(struct node *node)
{
while(node!=NULL)
{
printf("%d ", node->roomNo);
printf("%d\n ", node->time);
node = node->next;
}
}
/* Function to insert a node at the beginging of the linked list */
void insert(struct node** head_ref, int new_room, time_t new_time)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->roomNo = new_room;
new_node->time = new_time;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
MergeSort(&(*head_ref));
}
/* sorts the linked list by changing next pointers (not data) */
void MergeSort(struct node** headRef)
{
struct node* head = *headRef;
struct node* a;
struct node* b;
/* Base case -- length 0 or 1 */
if ((head == NULL) || (head->next == NULL))
{
return;
}
/* Split head into 'a' and 'b' sublists */
FrontBackSplit(head, &a, &b);
/* Recursively sort the sublists */
MergeSort(&a);
MergeSort(&b);
/* answer = merge the two sorted lists together */
*headRef = SortedMerge(a, b);
}
struct node* SortedMerge(struct node* a, struct node* b)
{
struct node* result = NULL;
/* Base cases */
if (a == NULL)
return(b);
else if (b==NULL)
return(a);
/* Pick either a or b, and recur */
if (a->time <= b->time)
{
result = a;
result->next = SortedMerge(a->next, b);
}
else
{
result = b;
result->next = SortedMerge(a, b->next);
}
return(result);
}
void FrontBackSplit(struct node* source,
struct node** frontRef, struct node** backRef)
{
struct node* fast;
struct node* slow;
if (source==NULL || source->next==NULL)
{
/* length < 2 cases */
*frontRef = source;
*backRef = NULL;
}
else
{
slow = source;
fast = source->next;
/* Advance 'fast' two nodes, and advance 'slow' one node */
while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}
/* 'slow' is before the midpoint in the list, so split it in two
at that point. */
*frontRef = source;
*backRef = slow->next;
slow->next = NULL;
}
}
void * addThread(void *n)
{
struct node *llnode = n;
int i;
for(i = 0; i <4; i++)
{
pthread_mutex_lock(&count_mutex);
printf("Adding node.\n");
insert(&llnode, getRandRoom(), getRandTime());
sleep(1);
printf("the list is...\n");
printList(llnode);
pthread_mutex_unlock(&count_mutex);
}
}
struct node* head;
int main()
{
signal(SIGINT, ctrlc_catch);
pthread_t addWakeup, makeWakeup;
pthread_create(&addWakeup, NULL, addThread, (void*)&head);
pthread_create(&makeWakeup, NULL, wakeThread, (void*)&head);
pthread_join(addWakeup, NULL);
pthread_join(makeWakeup, NULL);
return 0;
}
Example output is:
Adding node.
the list is...
0 0
4000 1323882918
Adding node.
the list is...
0 0
809 1323882890
4000 1323882918
Adding node.
the list is...
0 0
809 1323882890
7617 1323882908
4000 1323882918
Adding node.
the list is...
0 0
809 1323882890
7617 1323882908
4000 1323882918
4426 1323882926
You problem is most likely due to passing address of head (i.e. ~struct node**) instead of head(i.e. struct node*) to the thread function where void * is typecast to struct node*. Change this call pthread_create(&addWakeup, NULL, addThread, (void*)&head); to pthread_create(&addWakeup, NULL, addThread, (void*)head); & next call also on similar lines. Unfortunately you were not seeing a crash in the current case. On my system I observed that initializing head to NULL was crashing the program. Similar suggestion has already been provided by #wildplasser. Try this change out and check the list printed.
Hope this helps!
Your function addthread creates a local copy llnode of its argument n, and uses a pointer to llnode to call insert(). Insert can alter llnode, but that change will only affect the local copy.
You should change addThread to take an argument of type struct node**, instead of struct node*.
While it may exist in code not shown, the code may need a call to pthread_mutex_init to initialize the mutex. It is possible that the contents of the variable count_mutex are initialized "by chance" to values that will actually work, but it would not be good to rely on that.

Error in trying to count the nodes in circular doubly linked list recursively

Here is my implementation of count:
int count(node *start)
{
static int l ;
node *current; /* Node for travelling the linked list*/
current=start;
if(current->next!=start)
{
l = 1 + count ( current->next ) ;
return ( l ) ;
}
else
{
return(1);
}
}
Here is a fragment of main function where I am calling it:
void main()
{
node *head;
printf ( "Length of linked list = %d", count ( head ) ) ;
}
Here is the structure:
struct cirdoublelinklist
{
struct cirdoublelinklist *prev; /** Stores address of previous node **/
int value; /** stores value **/
struct cirdoublelinklist *next; /** stores address of next node **/
};
/** Redefining list as node **/
typedef struct cirdoublelinklist node;
On running and trying to see the length of a list, it crashes with out of bound memory. Please help me with this, I have been working on this for a long time now.
Method to add the first node:
void initialize(node *start)
{
start->prev=start;
printf("\nEnter Value\n");
scanf("%d",&start->value);
start->next=start;
}
Method to add subsequent nodes after the location specified:
void insert_after(node *start)
{
int num; /* value for inserting a node */
int flag=0;
node *newnode; /* New inputed node*/
node *current; /* Node for travelling the linked list*/
newnode=(node*)malloc(sizeof(node));
printf("\nEnter the value after which you want to insert a node\n");
scanf("%d",&num);
init(newnode);
current=start;
while(current->next!=start)
{
if(current->value==num)
{
newnode->next=current->next;
current->next->prev=newnode;
current->next=newnode;
newnode->prev=current;
flag=1;
}
current=current->next;
}
if(flag==0 && current->next==start && current->value==num)
{
/*** Insertion checking for last node ***/
newnode->next=current->next; /* Start is being copied */
current->next->prev=newnode;
current->next=newnode;
newnode->prev=current;
flag=1;
}
if(flag==0 && current->next==NULL)
printf("\nNo match found\n");
}
Every time you call count, it has a new start, so current->next!=start is always comparing a node to its successor, which will only ever end if the list has length 1. What you most likely want to do is have two functions:
int count(node *start)
{
if(start == NULL)
return 0;
return count_helper(start, start);
}
int count_helper(node *start, node *current)
{
static int l;
if(current->next!=start)
{
l = 1 + count (start, current->next);
return ( l ) ;
}
else
{
return(1);
}
}
As others have mentioned, the static variable is not necessary. A better way of writing what I have called count_helper would be:
int count_helper(node *start, node *current)
{
if(current->next!=start)
{
return 1 + count (start, current->next);
}
else
{
return 1;
}
}
Finally, a more efficient implementation would be non-recursive:
int count(node *start)
{
if(start == NULL)
return 0;
node *current = start->next;
int c = 1;
while(current != start)
{
c++;
current = current->next;
}
return c;
}
Well, the problem is that you call the function in the main on a NULL pointer. Infact node *head; is declared but never assigned to something. So when you execute this line:
if(current->next!=start)
the program crashes because it will check for NULL->next that, obviously, doesn't exist.
You need to pass a pointer to start pointer in insert_after function
void insert_after(node **start)
instead of
void insert_after(node *start)
Otherwise you'll be just updating the local copy of *start.
Similarly for initialize
void initialize(node **start)
Put simply, the recursive calls don't know the original start node. You will need to add a second node* argument and pass the start node via it.
Here's a recursive solution that doesn't use static or helper variables:
int count(Node* head) {
// Base cases:
// 0 nodes
if (!head)
return 0;
// 1 node
if (head->next == next)
return 1;
// Keep a pointer to the node to be removed
Node* rest = head->next;
// Remove the node
head->next = head->next->next;
// Get the length of the new list
int result = 1 + count(head->next);
// Reconnect the node
head->next = rest;
return result;
}

Resources