I am trying to implement singly linked list. Is this correct? Getting this warning "non portable pointer conversion". How do i check if the SLL is working? Is it connected to each other? By the way, I am using Turbo C. I am still in this creating and inserting the nodes part.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*start=NULL;
void creat(int *num)
{
struct node *new_node,*current;
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=num;
new_node->next=NULL;
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
}
main()
{
int binrange,max=100,n,i,divi;
clrscr();
printf("enter range: ");
scanf("%d",&binrange);
n=max/binrange;
divi=max/n;
for(i=0;i<=max;i++)
{
if(i%divi==0 && i>0)
{
//create nodes here
//store i into nodes
creat(&i);
}
}
getch();
}
new_node->data=num;
should be
new_node->data=*num;
num is a pointer and *num gives the value to be stored in the new_node->data
Assigning pointer to a variable of type int is giving a valid warning.
Related
#include<stdio.h>
#include<stdlib.h>
struct node{
float coeff;
int exp;
struct node *next;
};
struct node*inserts(struct node *start,float co,int ex){
struct node*newnode;
struct node*ptr;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->exp=ex;
newnode->coeff=co;
if(start==NULL||start->exp<ex){
newnode->next=start;
start=newnode;
}
else{
ptr=start;
while(ptr->next!=NULL&&ptr->exp>ex){
ptr=ptr->next;
}
newnode->next=ptr->next;
ptr->next=newnode;
}
return start;
}
struct node*create(struct node *start){
int ex,n;
float co;
printf("enter the number of terms\n");
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("enter your coefficient\n");
scanf("%f",&co);
printf("enter your exponent\n");
scanf("%d",&ex);
start=inserts(start,co,ex);
}
return start;
}
struct node* polymulti(struct node *start1,struct node *start2){
struct node*p2;
struct node *p1;
struct node *start3;
p1=start1;
p2=start2;
while(p1!=NULL){
while(p2!=NULL){
start3=inserts(start3,p2->coeff*p1->coeff,p2->exp*p1->exp);
p2=p2->next;
}
p1=p1->next;
}
return start3;
}
void display(struct node*st){
struct node *temp;
temp=st;
while(temp->next!=NULL){
printf("%.1fx^%d",temp->coeff,temp->exp);
printf("+");
temp=temp->next;
}
printf("%.1fx^%d",temp->coeff,temp->exp);
}
int main(){
struct node*start1;
struct node*start2;
struct node*start3;
start1=create(start1);
start2=create(start2);
start3=polymulti(start1,start2);
display(start3);
return 0;
}
i am working on a program that accepts two polynomials and then displays the product of the two polynomials .i have created three linked list ,two of which are used to input the two polynoimals and third is used to display the product. However when i was inserting values into the 2nd linked list ,the program abruptly ended without giving any errors or warning. can anybody tell whats the problem here?
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;
}`
I wrote a program to create and print a single linked list. I have used structure pointer to structure pointer for modifying the list. When I print the list it only prints last two nodes added.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node * createnode(int num){
struct node * n;
n=(struct node*)malloc(sizeof(struct node));
n->data=num;
n->next=NULL;
return n;
}
void createlist(struct node **head,int num){
struct node *temp=createnode(num);
*head=temp;
return;
}
void options(){
printf("Enter your choice\n");
printf("1.Add at end\n");
printf("2.Add at beginning\n");
printf("3.Add at location\n");
printf("4.Print list\n");
}
void add_end(struct node **head){
int info;
printf("Value: ");
scanf("%d",&info);
if(*head==NULL){
createlist(head,info);
return;
}
struct node *temp=createnode(info);
struct node **end=head;
while((*end)->next!=NULL){
(*end)=(*end)->next;
}
(*end)->next=temp;
}
void print2(struct node *head){
struct node * temp=head;
printf("\nList :");
while(temp!=NULL){
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
int main(){
struct node *head=NULL;
createlist(&head,5);
int choice=0;
options();
scanf("%d",&choice);
while(1){
switch(choice){
case 1:add_end(&head);
break;
case 4:print2(head);
break;
default:exit(0);
}
options();
scanf("%d",&choice);
}
return 0;
}
Each time, I append 5 to 6 nodes at the end of list and when I print the list, it only prints last to nodes added.
I don't know it is wrong with add_end function or print function.
Your add_line routine incorrectly searches for the last node. The end variable is a pointer to pointer, so it is in a sense equivalent to the head parameter and it is not a temporary value, as it should be. Change the last lines to something like this:
void add_end(struct node **head){
...
struct node *end = *head;
while (end->next) {
end = end->next;
// Original line overwrites the 'head': (*end)=(*end)->next;
}
end->next=temp;
}
My doubly Linked list in C is displaying garbage value instead of value that I have entered, I have run this code on Turbo C++.
the code compiled correctly with 0 errors and 0 warnings, but still it is displaying some garbage values. I have included the libraries (stdio.h,conio.h,stdlib.h,malloc.h)
here is the code :
struct dlist
{
int data;
struct dlist *next;
struct dlist *prev;
};
struct dlist *head, *end;
void create_dlist()
{
char k='y';
struct dlist *new_node;
while(k=='y'||k=='Y') {
if(head==NULL) {
new_node=(struct dlist *)malloc(sizeof(struct dlist));
printf("Enter the integer value -> ");
new_node->data=0;
new_node->next=NULL;
new_node->prev=NULL;
scanf("%d",&new_node->data);
head=new_node;
end=new_node;
} else {
new_node=(struct dlist *)malloc(sizeof(struct dlist));
printf("Enter the integer value -> ");
new_node->data=0;
new_node->next=NULL;
new_node->prev=end;
scanf("%d",&new_node->data);
end->next=new_node;
end=new_node;
}
printf("Do you want to continue (y/n) ; ");
scanf(" %c",&k);
}
}
void display()
{
struct dlist *pointer;
pointer=head;
if(pointer!=NULL) {
while(pointer->next!=NULL) {
printf("%d\t",&pointer->data);
pointer=pointer->next;
}
} else {
printf("list is empty");
}
}
int main()
{
clrscr();
head=NULL;
end=NULL;
create_dlist();
display();
getch();
return 0;
}
A coded solution will be a great help
Whole problem is in display(). First you print address and not value of data. Also, you are not printing last element. I made some changes. Ask me if you need any explanations.
void display()
{
struct dlist *pointer;
pointer=head;
if(pointer!=NULL)
{
while(pointer!=NULL)
{
printf("%d\t",pointer->data);
pointer=pointer->next;
}
}
else
{
printf("list is empty");
}
}
I want to create a singly linked list using C. Why is this piece of code not working? The code is given below. I am using CodeBlocks for running this which is an opensource compiler.
#include<stdio.h>
#include<malloc.h>
struct node
{
int info;
struct node *next;
}*first=NULL;
void create()
{
struct node *ptr;
int i,n;
printf("Enter the number of nodes");
scanf("%d", &n);
for(i=0;i<n;i++)
{
ptr=(struct node *)malloc(sizeof(struct node));
printf("Enter the data.");
scanf("%d",&ptr->info);
ptr=ptr->next;
if(first==NULL)
{first=ptr;}
}
ptr->next=NULL;
}
void main()
{
create();
}
When you do ptr=ptr->next; in your loop, you loose ptr and then you point to garbage because next is not yet initialized. So first link ptr into the list and then go to next.
Doing that I leave for you as excercise.