priority queue of string in c - c

This program is of priority queue where i am storing string as data and queue is created using linked list the element having least number (as priority no) has higher priority that is it will be inserted towards head node and so while the time of removing (pop or dequeue) that element will be removed first.(for eg 1 has higher Priority than 2)
#include<stdio.h>
#include<stdlib.h>
struct node {
char *string;
int priority;
struct node* next;
};
struct node *head;
struct node* getnewnode(char *s,int p){
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->string=s;
newnode->priority=p;
newnode->next=NULL;
return newnode;
}
void push(char* str,int p){
struct node* node1=getnewnode(str,p);
if(head==NULL){ //if the element is inserted in empty list
head=node1;
}
if(head->priority > p )
{
node1->next=head;
head=node1;
}
else
{
struct node* temp=head;
while(temp->next!=NULL&&temp->priority <= p){
temp=temp->next;
}
while(temp->next!=NULL&&temp->priority <= p) is correct or not because if the pushed element priority is matching than this new element will be placed after the present one(having same priority)
node1->next=temp->next;
temp->next=node1;
}
}
void pop(){
struct node* temp=head;
head=head->next;
free(temp);
}
char* peek(){
return head->string;
}
int main(){
head=NULL; //head is null initially
char a[10]="akash";
push(a,1);
char b[20]="rahul";
push(b,3);
printf("%s",peek());
}
It is not showing desired output but it is crashing
int main(){
head=NULL;
char* a=(char *)malloc(sizeof(char)*10);
a="akash";
push(a,1);
char* b=(char *)malloc(sizeof(char)*10);
b="rahul";
push(b,3);
char* c=(char *)malloc(sizeof(char)*10);
c="neymar";
push(c,1);
printf("%s",peek());
pop();
printf("%s",peek());
}
I give akash as priority 1 rahul as 2 and neymar again as 1 it should print akash and neymar for last two printf statements but it is printing akash rahul #dbush

In your push function, in the case where head is NULL, you set head to the new node, and then later try to put the node into the list again. You end up with the next field pointing to itself, so future inserts fall into an infinite loop.
When inserting into an empty list, you only need to set head to the new node and nothing else, so just return right away.
if(head==NULL){ //if the element is inserted in empty list
head=node1;
return;
}
Also, you should make a copy of the string that's passed in when you make a new node. Otherwise, you could end up with a pointer to memory that is no longer valid or multiple pointers pointing to the same buffer, with only the most recent update being viewable:
struct node* getnewnode(char *s,int p){
struct node* newnode = malloc(sizeof(struct node)); // don't cast return value of malloc
newnode->string = strdup(s); // create copy of given string
newnode->priority=p;
newnode->next=NULL;
return newnode;
}

Related

I am trying to insert value in linked list but in output it is showing only first insertion

In this I am trying to insert values in empty linked list initially and then adding element after that .
function insert is inserting element in linked list .
display function is displaying linked list .
so i am getting output as first insertion only .
#include<stdio.h>
#include<stdlib.h>
struct node
{
int value;
struct node *next;
}*first = NULL;
void insert(struct node *ptr,int n ){
struct node* t;
t=(struct node* )malloc(sizeof(struct node ));
t->value=n;
if(first==NULL){
t->next=first;
first=t;
return;
}
else{
ptr=first;
while(ptr!=NULL){
ptr=ptr->next;
}
t->next=ptr;
t->value=n;
ptr=t;
}
}
void display(struct node *f){
while(f!=NULL){
printf("%d",f->value);
f=f->next;
}
}
int main(){
insert(first,5);
insert(first,20);
insert(first,32);
insert(first,66);
insert(first,689);
display(first);
return 0;
}
First off, you're reassigning ptr before making use of its original value, and you're accessing start in the function directly anyway, so passing it as a function parameter is not needed.
Look more carefully at your appending code:
ptr=first;
while(ptr!=NULL){
ptr=ptr->next;
}
// you only reach here when ptr is NULL!
t->next=ptr; // sets next to NULL
t->value=n; // you've already set the value after you malloc'd the node
ptr=t; // this line has no overall affect. You're setting the local pointer "ptr" to point to something then it goes out of scope.
In short, you've got your pointers the wrong way round. What you instead want to be doing here is getting up to the last element and then setting its next to the new node:
struct node* ptr=first;
while(ptr->next!=NULL){
ptr=ptr->next;
}
// Now when we reach here, "ptr" is the last element in the list
ptr->next = t;
However, looping the entire list for every iteration is fairly inefficient, you could instead store a node* last as well as the first to avoid the loop:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int value;
struct node *next;
} *first = NULL, *last = NULL;
void insert(int n){
struct node* t;
t = (struct node*)malloc(sizeof(struct node));
t->value = n;
t->next = NULL
if (first == NULL) { // last == NULL too
first = last = t;
return;
}
last->next = t;
last = t;
}

how to delete head in a linked list in c?

This program should delete the N-node in a singly linked list. If i put N = 1 or N = 2 it's ok, the program works. But with N = 0 the output prints infinite nodes with random values (after deleting the node 0). I think the program can't see the new head. Thx for the help!
#include <stdio.h>
#include <stdlib.h>
#define N 0
struct node {
int data;
struct node *next;
};
void printlist(struct node *head){
struct node *current=head;
int i=0;
while (current!=NULL){
printf("node number %d \t : %d\n", i, current->data);
current=current->next;
i++;
}
}
int deletenode(struct node *head,int n){
struct node *current=head;
struct node *previous=head;
int i=0;
while(current!= NULL){
if (n==i && i!=0){
previous->next=current->next;
free(current);
return 1;
}
else if (n==i && i==0){
head=head->next;
free(current);
return 1;
}
i++;
previous=current;
current=current->next;
return 0;
}
printf("error\n");
exit(EXIT_FAILURE);
}
void main(){
struct node *n1=malloc(sizeof(struct node));
struct node *n2=malloc(sizeof(struct node));
struct node *n3=malloc(sizeof(struct node));
struct node *head=n1;
n1->data=5;
n1->next=n2;
n2->data=10;
n2->next=n3;
n3->data=15;
n3->next=NULL;
printf("\n\nbefore\n");
printlist(head);
deletenode(head,N);
printf("\n\nafter\n");
printlist(head);
}
I'm using currentas a temp pointer , because after the head shift on the second node i need a pointer to the old head and use free.
C always passes by value, so changing a parameter has no effect on the caller.
void foo(int i) {
i = 1234; // No effect on caller.
}
void foo(int *p) {
p = NULL; // No effect on caller.
}
If you want to modify a variable (such as the caller's head), you need to pass a pointer to it. (You can still change that to which a pointer references.)
int deletenode(struct node **head, int n) {
...
}
deletenode(&head, N);
Now, you could simply replace every instance of head in your code with (*head) to account for the new calling convention, but that would waste an opportunity for simplification. By having a pointer to a struct node *, we don't need to handle head (a struct node *) and prev_node->next (a struct node *) differently.
int delete_node_n(struct node **ptr, unsigned n) {
// Make `ptr` point to the pointer we want to modify.
// This will either be the `head` variable
// or the `next` field of some node.
while (1) {
if (!*ptr)
return 0;
if (!n)
break;
ptr = &( (*ptr)->next );
--n;
}
struct node *to_free = *ptr;
*ptr = (*ptr)->next;
free(to_free);
return 1;
}

linked list: how to return the whole linked list after insterting

I am trying to return the head of a linked list in the function Insert of the following program. However, it is failing with compilation error.
Can anyone please tell me what wrong I have done:
#include<stdio.h>
#include<stdlib.h>
struct ListNode
{
int data;
struct ListNode *next;
};
int ListLength(struct ListNode *head)
{
int count = 0;
struct ListNode *temp=head;
while(temp!=NULL)
{
count++;
temp=temp->next;
}
return count;
}
struct ListNode *Insert(struct ListNode *head, int value, int pos)
{
struct ListNode *temp,*curr;
curr=head;
int k=1;
temp=(struct ListNode *)malloc(sizeof(struct ListNode));
if(pos==1)
{
temp->data=value;
if(head==NULL)
{
temp->next=NULL;
head=temp;
}
else
{
temp->next=head;
head=temp;
}
}
else
{
while((curr!=NULL) && (k<pos))
{
k++;
curr=curr->next;
}
temp->data=value;
temp->next=curr->next;
curr->next=temp;
}
return head;
}
void printList(struct ListNode *head)
{
struct ListNode *temp;
temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
printf(" ");
temp=temp->next;
}
}
int main
{
struct ListNode *head=NULL;
//head = NULL;
head=Insert(head,10,1);
//Insert(head,11,2);
printList(head);
return 0;
}
I am trying to return the head of the new linked list after the insertion. I don't know where I am going wrong. Thanks in advance for the help.
(i) Firstly, include int main(void) as mentioned in the comments.
(ii) Next, with your current code, when you try printing the list, you are going to be in an infinite loop and get a stack overflow.
To avoid this, increment the temp to point to the next node after each print.
So your print function should look like:
void printList(struct ListNode *head)
{
struct ListNode *temp;
temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
printf(" ");
temp=temp->next; // this line is required
}
}
(iii) And in your main function, call the printList with an argument, that is the head of the node like this:
printList(head);
(iv) And don't forget to return the count in your finding the length of the list function. Add the return statement at the end of your ListLength function:
return count;
(v) Your current code does not handle a case when head is NULL, and user wants to insert at a position greater than 1. Or more generally, when a user wants to insert at a position that is greater than the current list's length.
While you trust such an input would not be given, always handle such exceptions (you would probably get a SEGMENTATION FAULT here when trying to access memory of null nodes).
To handle this, you can add a check at the start of the Insert function like,
int lenList = ListLength(head);
if (lenList < pos)
{
printf("Please provide a position less than %d to insert", lenList);
return 0; // don't proceed with inserting node with NULL pointers
}
If head is declared global you don't have to return it. (Sorry, my answer is short)

Node pointer to current Node during mutile function call

I have declared a global pointer ptr and want that it should point to current node during different function call.
This is a sample code where I am creating a new node in fun1 and inserting in link list. In func2 I want to update the other members of newNode in linklist with a different value.
Currently I am traversing the link list to get the current Node or last Node which I dont want since during insertion of new Records already we have to traverse to reach to last Node thus storing the address of last Node.
But by doing the below I am not getting the proper values. Kindly someone suggest where I went wrong.
I am doing like this :
#include<stdio.h>
#include <stdlib.h>
struct Node
{
int data1;
int data2;
struct Node* next;
};
struct Node* head=NULL;
struct Node* ptr =NULL; /* Global pointer */
void insertNode(struct Node ** , struct Node* );
void fun1();
void fun2();
void fun1()
{
struct Node* ptr1 =NULL;
ptr1 = (struct Node*)malloc(sizeof(struct Node*));
ptr1->data1=1; /* intilaizing with some values */
insertNode(&head,ptr1);
}
void fun2()
{
/* Updating the current Node in the linklist with new value . */
ptr->data2=2;
}
void insertNode(struct Node ** head, struct Node* NewRec)
{
if(*head ==NULL )
{
NewRec->next = *head;
*head = NewRec;
ptr=*head;
}
else
{
/* Locate the node before the point of insertion */
struct Node* current=NULL;
current = *head;
while (current->next!=NULL )
{
current = current->next;
}
NewRec->next = current->next;
current->next = NewRec;
ptr=current->next;
}
}
int main ()
{
fun1();
fun2();
while(head!=NULL)
{
printf("%d", head->data1);
printf("%d",head->data2);
head=head->next;
}
return 0;
}
You made a classic mistake.
This is wrong:
ptr1 = (struct Node*)malloc(sizeof(struct Node*));
The allocated space here is sizeof(struct Node*) which is the size of a pointer (usually 4 or 8 bytes depending on the platform). But you need to allocate space for the whole struct Node structure, whose size is sizeof(struct Node).
So you simply need this:
ptr1 = (struct Node*)malloc(sizeof(struct Node));
BTW: in C you don't cast the return value of malloc so you actually should write this:
ptr1 = malloc(sizeof(struct Node));

Connect preorder predecessor with its successor in a binary search tree

Actually in an interview i was asked to write a code through which every node in a binary search tree is having a extra pointer namely "next" we have to connect this pointer of every node to its pre order successor ,can any one suggest me the code as i was not able to do so. the tree nodes has above structure :-
struct node {
int data ;
struct node *left,*right;
struct node *next; //this pointer should point to pre order successor
};
thank you .
Cracked the the solution thanks to you guys ,below is the whole code written in c :-
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left,*right,*next;
};
struct node* getNode(int data)
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->left=temp->right=NULL;
temp->data=data;
return temp;
}
void insert(struct node**root,int data)
{
if(!*root)
{
*root=(struct node*)malloc(sizeof(struct node));
(*root)->left=(*root)->right=NULL;
(*root)->data=data;
}
else if(data<(*root)->data)
insert(&((*root)->left),data);
else if(data>(*root)->data)
insert(&((*root)->right),data);
}
struct node* preorderSuccessor(struct node* root,struct node* p)
{
int top,i;
struct node *arr[20];
if(!root || !p)
return NULL;
if(p->left)
return p->left;
if(p->right)
return p->right;
top=-1;
while(root->data!=p->data)
{
arr[++top]=root;
if(p->data<root->data)
root=root->left;
else
root=root->right;
}
for(i=top;i>=0;i--)
{
if(arr[i]->right)
{
if(p!=arr[i]->right)
return arr[i]->right;
}
p=arr[i];
}
return NULL;
}
void connect(struct node* parent,struct node *r)
{
if(r)
{ connect(parent ,r->left);
r->next = preorderSuccessor(parent,r);
connect(parent,r->right);
}
}
int main()
{
struct node* root=NULL,*temp=NULL;
int arr[]={10,11,2,3,9,8,4,5},size,i;
size=sizeof(arr)/sizeof(arr[0]);
for(i=0;i<size;i++)
insert(&root,arr[i]);
connect(root,root);
struct node *ptr = root;
while(ptr){
// -1 is printed if there is no successor
printf("Next of %d is %d \n", ptr->data, ptr->next? ptr->next->data: -1);
ptr = ptr->next;
}
return 0;
}
As Eugene said: So traverse the tree with preorder traversal and connect. To do that, you need to know which node, if any, you visited last.
You can do that with the usual recursive approach by passing a reference to the previous node. This must be the address of a variable that is valid throughout the recursion, because the previous node is not necessarily closer to the root. You could use a global variable, but a variable created in a wrapper function may be better:
void connect_r(struct node *node, struct node **whence)
{
if (node) {
if (*whence) (*whence)->next = node;
*whence = node;
connect_r(node->left, whence);
connect_r(node->right, whence);
}
}
void connect(struct node *head)
{
struct node *p = NULL;
connect_r(head, &p);
if (p) p->next = NULL;
}
The pointer p in connect, whose address is passed to the recursive function connect_r holds the node whose next pointer should be updated next. The update doesn't happen on the first visited node. and the next member of the last visited node must explicitly be set to NULL after the recursion.
Alternatively, you can connect the nodes iteratively by using a stack:
void connect(struct node *head)
{
struct node *p = NULL;
struct node **prev = &p;
struct node *stack[32]; // To do: Prevent overflow
size_t nstack = 0;
if (head) stack[nstack++] = head;
while (nstack) {
struct node *node = stack[--nstack];
*prev = node;
prev = &node->next;
if (node->right) stack[nstack++] = node->right;
if (node->left) stack[nstack++] = node->left;
}
*prev = NULL;
}
The connected next pointers are a snapshot of the current tree. Insertions, deletions and rearrangements of nodes will render the next chain invalid. (But it can be made valid again by calling connect provided that the tree's left and right links are consistent.)

Resources