I was practicing a bit of linked list and I was trying to add elements into sorted doubly linked list. However when I call the the function to add the element in the list, instead of calling the function, program goes into an infinte loop. I have checked the the program does not enter the function adding print statement at start of function. Here's the whole program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node* next;
struct node* prev;
};
struct node* sortedInsert(struct node* head, int data)
{
printf("x" );
struct node* res=head;
struct node* ptr=(struct node*)malloc(sizeof(struct node));
ptr->info=data;
ptr->next=NULL;
ptr->prev=NULL;
printf("X");
if(head==NULL)
return ptr;
else if(ptr->info<=head->info)
{
ptr->next=head;
head->prev=NULL;
res=ptr;
return res;
}
else
{
while(head!=NULL)
{
if(head->info>=ptr->info)
{
ptr->prev=head->prev;
ptr->next=head;
head->prev=ptr;
return res;
}
}
}
}
struct node* push(struct node* head)
{
struct node* ptr=(struct node*)malloc(sizeof(struct node));
int n;
printf("Enter size: ");
scanf("%d",&n);
printf("Enter elements: ");
for(int i=0;i<n;i++)
{
if(head==NULL)
{
scanf("%d",&ptr->info);
ptr->next=NULL;
ptr->prev=NULL;
head=ptr;
}
else
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
scanf("%d",&temp->info);
temp->next=NULL;
temp->prev=ptr;
ptr->next=temp;
ptr=temp;
}
}
return head;
}
void display(struct node* head)
{
struct node *res;
for(res=head;res!=NULL;res=res->next)
printf("%d\t",res->info);
printf("\n");
}
int main()
{
struct node* head1=NULL;
head1=push(head1);
display(head1);
int num;
printf("Enter number: ");
scanf("%d",&num);
printf("%d\n",num);
head1=sortedInsert(head1,num);
display(head1);
return 0;
}
Output is:
Enter size: 4
Enter elements: 1 2 4 5
1 2 4 5
Enter number: 3
3
It is because you are not incrementing the head to point next node in the while loop.
Also once you found the position in the list where new node to be inserted you need to make prev node point to new node
head->prev->next = ptr; other wise your list will break;
Your code should look like below.
struct node* sortedInsert(struct node* head, int data)
{
.......
.......
while(head!=NULL)
{
if(head->info>=ptr->info)
{
ptr->prev=head->prev;
ptr->next=head;
head->prev->next = ptr; // to make prev node to point new node
head->prev=ptr;
return res;
}
head=head->next; // Increment the head to point next node.
}
.......
}
Related
I have tried to create and display singly linked linked list using separate functions. the elements would be entered by the user. my program is taking input but not displaying those elements. any help would be greatly appreciated
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
void create(int n, struct node *newnode, struct node *prev, struct node *head){
for(int i=0; i<n; i++){
printf("Enter the data for node %d: ", i+1);
scanf("%d", &newnode-> data);
if(head==NULL){
head = newnode;
head-> next = NULL;
prev = head;
}
else{
prev->next= newnode;
newnode->next= NULL;
prev=newnode;
}
}
}
void display(int n, struct node *temp){
printf("%d ", temp->data);
temp=temp->next;
}
int main()
{
struct node *newnode, *prev, *head, *temp;
newnode = (struct node*)malloc(sizeof(struct node *));
head=NULL;
int n, choice;
printf("Number of elements in your linked list: ");
scanf("%d", &n);
create(n, newnode, prev, head);
printf("\n1.Display");
printf("Enter the operation you would like to perform: ");
scanf("%d",&choice);
if(choice==1){
temp=head;
display(n, temp);
}
return 0;}
Recently I learnt about doubly linked lists, and I have tried to write some code in C to implement them. The program below is supposed to receive integer inputs form the user and put them into a list with a maximum of ten integers. However, when I input the values and then print them, only the first and last values are output. How would one solve this issue?
Here is the code:
`#include <stdio.h>
#include <stdlib.h>
struct node {
struct node* prev;
int num;
struct node* next;
};
struct node *head,*p;
struct node* create_first_node(int x)
{
struct node *new = (struct node*)malloc(sizeof(struct node));
new->num=x;
new->prev=NULL;
new->next=NULL;
head=new;
p=head;
return new;
}
void add_node(int x)
{
struct node*new=(struct node*)malloc(sizeof(struct node));
new->num=x;
new->prev=p;
p->next=new;
(new->next)=NULL;
new=p;
}
void print_list(){
struct node *temp= head;
printf("\nThe list is:\n");
while (temp!=NULL)
{
printf("%d\t",temp->num);
temp=temp->next;
}
}
int main(){
int in[10];
int len,i;
head=NULL;
printf("How many nodes would you like?(Max=10) \n");
scanf("%d",&len);
for (i = 0; i < len; i++)
{
if (head == NULL && i==0)
{
printf("Enter Value for node %d\n",i+1);
scanf("%d",(in+i));
create_first_node(in[0]);
}
else
{
printf("Enter Value for node %d\n",i+1);
scanf("%d",(in+i));
add_node(in[i]);
}
}
print_list(head);
return 0;
}`
struct node{
char name[50];
double grade;
struct node* next;
};
void append(struct node* root){
int n;
printf("Enter the number of students: ");
scanf("%d",&n);
while(n !=0){
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the name of the student: ");
scanf("%s",&temp->name);
printf("\nEnter the grade for the student named %s: ",temp->name);
scanf("%f",&temp->grade);
temp->next=NULL;
if(root==NULL){
root=temp;
}else{
struct node* iterate=root;
while(iterate->next != NULL){
iterate=iterate->next;
}
iterate->next=temp;
}
n--;
}
}
int listLength(struct node* root){
struct node* temp = root;
int counter=0;
while(temp !=NULL){
counter++;
temp=temp->next;
}
return counter;
}
int main()
{
struct node* root = NULL;
append(&root);
//printList(&root);
printf("Node length: %d",listLength(&root));
return 0;
}
This is what I have so far as I'm starting out with linked lists. I tried to make it so I can append to multiple linked lists with the function. So I'd just create a different root pointer in main and call the append function with it as a parameter to add nodes.
This seems to work, however, it adds an extra empty node at the beginning of the list. This node doesn't contain any data. So for example, if I add 4 students to the list the nodeLength function would return 5.
Change this:
void append(struct node* root)
to this:
void append(struct node** root)
so that you make the changes last even when append() terminates.
Of course then you will have to use *root instead of root inside the body of that function.
PS: Do I cast the result of malloc? No.
struct node{
char name[50];
double grade;
struct node* next;
};
struct node * append(struct node* root){
int n;
printf("Enter the number of students: ");
scanf("%d",&n);
while(n !=0){
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the name of the student: ");
scanf("%s",&temp->name);
printf("\nEnter the grade for the student named %s: ",temp->name);
scanf("%f",&temp->grade);
temp->next=NULL;
if(root==NULL){
root=temp;
}else{
struct node* iterate=root;
while(iterate->next != NULL){
iterate=iterate->next;
}
iterate->next=temp;
root=iterate;
}
n--;
}
return root; }
int nodeLength(struct node* root){
struct node* temp = root;
int counter=0;
while(temp !=NULL){
counter++;
temp=temp->next;
}
return counter;
}
int main()
{
struct node* root = NULL;
root= append(root);
//printList(&root);
printf("Node length: %d",nodeLength(root));
return 0;
}
Check this code to understand a bit. Will explain more.
And don't cast the return type of malloc.
Here what you are doing will work fine for if you correctly changed the parameter to struct node** but yes it can be done more easily like this(example shown above). It's natural and more intuitive rather than using double pointer.
Please help values wont save once added into the list and nothing is printed as a result
#include <stdio.h>
#include <stdlib.h>
struct node{
char first[30];
char last[30];
char address[100];
char postal[6];
char number[10];
struct node* next;
};
typedef struct node node;
void print(node* root);
node* add(node* root);
void addinfo(node* root);
int main(){
node* root=NULL;
root=add(root);
print(root);
return 0;
}
Its supposed to add a new node at the end of the list then add values
node* add(node* root){
if(root==NULL){
root=(node*) malloc(sizeof(node));
addinfo(root);
root->next=NULL;
return root;
}
node* temp=root;
while(temp!=NULL){
temp=temp->next;
}
temp=(node*)malloc(sizeof(node));
addinfo(temp);
temp->next=NULL;
return root;
}
void print(node* root){
node* temp=root;
while(temp!=NULL){
printf("First Name: %s\n",temp->first);
printf("Last Name:%s\n",temp->last);
printf("Address:%s\n",temp->address);
printf("Postal%s\n",temp->postal);
printf("Phone Number:%s\n",temp->number);
temp=temp->next;
}
}
If I print in the function, the values print normally but once this functions ends all the values are gone.
void addinfo(node* root){
node* temp=root;
while(temp!=NULL){
temp=temp->next;
}
temp=malloc(sizeof(node));
printf("Please enter a name:");
gets(temp->first);
printf("Please enter a last name:");
gets(temp->last);
printf("Please enter an address:");
gets(temp->address);
printf("Please enter a postal code:");
gets(temp->postal);
printf("Please enter a phone number:");
gets(temp->number);
}
The simplest way of implementing what you want is keeping a pointer to the last item of a list:
typedef struct list {
node *first;
node *last;
} list;
Then you initialize a list with
list alist = { NULL, NULL };
and add items as follows:
void addNewNode(list *plist, node *pnode)
{
pnode->next = NULL;
if( plist->first == NULL)
plist->first = pnode;
else
plist->last->next = pnode;
plist->last = pnode;
}
fill the list:
while(there_is_something_to_read())
{
node *newnode = malloc(sizeof(node));
addInfo(newnode);
addNewNode(&alist, newnode);
}
note however, addInfo() should do just what its name says: add info to the node; no list manipulation:
void addinfo(node* pnode){
printf("Please enter a name:");
gets(pnode->first);
// .........
printf("Please enter a phone number:");
gets(pnode->number);
}
print it:
node *pnode;
for(pnode = alist.first; pnode != NULL; pnode = pnode->next)
printNode(pnode);
with
void printNode(node *pnode)
{
printf( "first name: %s\n", pnode->first);
//...
printf( "phone: %s\n", pnode->number);
printf( "\n");
}
The problem is in this code:
void addinfo(node* root){
node* temp=root;
while(temp!=NULL){
temp=temp->next;
}
temp=malloc(sizeof(node));
You carefully traverse the entire list until temp is NULL (which could be achieved more swiftly by writing temp = NULL; and then you overwrite the NULL with newly allocated memory, but don't hook the new node into the list.
This function cannot be used on an empty list. It can be revised to work on a non-empty list:
void addinfo(node *root)
{
if (root == NULL)
return; // Report error?
node *temp = root;
while (temp->next != NULL)
temp = temp->next;
temp->next = malloc(sizeof(node));
if (temp->next == NULL)
return; // Report error?
temp = temp->next;
temp->next = NULL;
…other code as before…
}
I want to swap the linkedlist terms pairwise.
Here is my code. It is giving me Segmentation Fault-core dumped
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void insert(struct node *n)
{
int num;
printf("Enter a number : ");
scanf("%d",&num);
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if(n==NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
int main()
{
struct node *n;
head=NULL;
n=head;
int i;
for(i=0;i<6;i++)
{
insert(n);
n=head;
}
display(n);
pairswap(n);
display(n);
}
void display(struct node *n)
{
struct node *temp;
temp=n;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
void pairswap(struct node *n)
{
struct node *temp,*temp1,*temp2;
temp=n;
temp1=temp->next;
while(temp!=NULL)
{
int tempnum;
tempnum=temp->data;
temp->data=temp1->data;
temp1->data=tempnum;
if(temp==n)
{
head=temp;
head->next=temp1;
}
else
{
temp2->next=temp;
temp2->next->next=temp1;
}
temp2=temp1;
temp=(temp->next)->next;
temp1=temp->next;
}
n=head;
}
Please learn about the debuggers.
Somewhere at the end of the list the value of
(temp->next)->next
is NULL, which you are putting it in variable temp.
Before making this assignment temp1=temp->next, you need to check if temp is NULL and take proper action.
The problem is pairswap function. In the while loop you are using the condition:
while(temp!=NULL)
and inside the loop you are assigning:
temp=(temp->next)->next
which will get crashed on the second last node since you are trying to dereference NULL pointer.
Use the following condition:
while((temp->next)->next!=NULL)
or
while (temp1->next != NULL)