#include<stdio.h>
#include<stdlib.h>
struct list
{
int a;
int b;
int c;
struct list *next;
};
struct list* addlistele(struct list*,int,int,int);
/* List c element */
void listc()
{
printf(" soon...\n");
}
void printlist(list)
{
struct list* temp;
temp=list;
while(temp!=NULL)
{
printf("a:%d,b;%d,c:%d\n",temp->a,temp->b,temp->c);
temp=temp->next;
}
}
/* List element */
struct list* addlistele(struct list* listadd,int b,int d,int m)
{
int i;
struct list* temp;
struct list* addelement=(struct list*)malloc(sizeof(struct list));
addelement->a=b;
addelement->b=d;
addelement->c=m;
addelement->next=NULL;
if(listadd==NULL)
{
printf("entering");
return addelement;
}
else
{
temp=listadd;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=addelement;
}
return listadd;
}
int main()
{
int ch,i,a,b,c;
struct list *element,*list;
element=(struct list*)malloc(sizeof(struct list));
printf("Choose any one of the option \n");
printf("1.List All \n 2.List c \n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter values \n");
for(i=0;i<2;i++)
{
scanf("%d %d %d \n",&a,&b,&c);
list=addlistele(element,a,b,c);
}
printlist(list);
break;
case 2:listc(); break;
default:break;
}
}
Hi all, i have written the code like the above one.In that when i gave inputs
> Choose any one of the option
> 1.List All
> 2.List c 1 Enter values 2 3 4 1 2 3
The output is
a:0,b;0,c:0
a:2,b;3,c:4
a:1,b;2,c:3
and also it is not adding the element first i mean it is not entering into this loop
if(listadd==NULL)
{
printf("entering");
return addelement;
}
how to make the head element to be NULL and also i don't know how 0 is coming first.Could anybody can tell me what will be the issue?
In main() you create head element and pass it to the addlistele() function.
element=(struct list*)malloc(sizeof(struct list));
...
list=addlistele(element,a,b,c);
You are seeing this first element which does not have valid values that you expected.
Solution would be you malloc() the element in the function rather than in main() and do not allocate element in main().
replace
struct list *element,*list;
element=(struct list*)malloc(sizeof(struct list));
with
struct list *list=NULL;
then
replace
list=addlistele(element,a,b,c);
with
list=addlistele(list,a,b,c);
Also
replace
void printlist(list)
with
void printlist(struct list *list)
and
replace scanf("%d %d %d \n",&a,&b,&c); with scanf("%d %d %d",&a,&b,&c);
Related
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
char data;
int p;
struct node *ptr;
};
struct node *start=NULL;
struct node *insert()
{
struct node *new_node,*z;
int num;
char s;
printf("\nEnter -1 to stop.");
printf("\nEnter the characters and their priorities: ");
scanf("\n%c %d",&s,&num);
while(num!=-1)
{
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=s;
new_node->p=num;
if(start==NULL)
{
start=new_node;
z=start;
new_node->ptr=NULL;
}
else
{
z->ptr=new_node;
new_node->ptr=NULL;
z=z->ptr;
}
scanf("%c %d",&s,&num);
}
return start;
}
struct node *display()
{
struct node *z;
z=start;
printf("\nDisplaying elements:\n");
while(z!=NULL)
{
printf("\t%c [Priority=%d]\n",z->data,z->p);
z=z->ptr;
}
return start;
}
struct node *sortit()
{
struct node *z,*q;
int t;
char x;
z=start;
while(z->ptr!=NULL)
{
q=z->ptr;
while(q!=NULL)
{
if(z->p>q->p)
{
t=z->p;
x=z->data;
z->p=q->p;
z->data=q->data;
q->p=t;
q->data=x;
}
q=q->ptr;
}
z=z->ptr;
}
return start;
}
struct node *new_insert()
{
struct node *y,*z;
int n;
char s;
y=(struct node *)malloc(sizeof(struct node));
printf("\nEnter character and priority for new node:");
scanf("%c %d",&s,&n);
printf("%c",s);
y->data=s;
y->p=n;
printf("%c",y->data);
printf("%d",y->p);
if(n<start->p)
{
y->ptr=start;
start=y;
printf("%d",y->p);
}
else
{printf("\nff");
z=start;
while(z->ptr->p<=n&&z->ptr!=NULL)
z=z->ptr;
y->ptr=z->ptr;
z->ptr=y;
}
return start;
}
int main()
{
insert();
display();
sortit();
display();
new_insert();
display();
return 0;
}
In this C code, I have tried to implement priority queues.
Everything works perfectly fine except the new_insert() function. The print statements after y->p=n; in new_insert() function print 0.
Hence, the function doesn't work properly. Also, in display() function the print statement prints the [Priority] twice.
Hence, I am not able to add an external node in my priority queue.
Well, you code is not far from working I think.
There are some problems that can be corrected:
the scanf usage is problematic
%c selector must be use with caution, use " %c %d" to prevent end of line problems
return value must be checked
in function insert, z may not be initialized (if insert() is called when start is not NULL)
You don't need to cast malloc returned value.
I tested the following code with this input:
a 2
b 3
c 1
d -1
e 0
And I got this result:
Enter -1 to stop.
Enter the characters and their priorities:
Displaying elements:
a [Priority=2]
b [Priority=3]
c [Priority=1]
Displaying elements:
c [Priority=1]
a [Priority=2]
b [Priority=3]
Enter character and priority for new node:
s: 'e' n: '0'
e00
Displaying elements:
e [Priority=0]
c [Priority=1]
a [Priority=2]
b [Priority=3]
Seems to work isn't? You can test it online
And here's the corrected code:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
char data;
int p;
struct node *ptr;
};
struct node *start=NULL;
struct node *insert()
{
/* initialize z in case of start is not null */
struct node *new_node,*z = start;
int num, ok;
char s;
printf("\nEnter -1 to stop.");
printf("\nEnter the characters and their priorities: ");
/* read the scanf return value */
ok = scanf(" %c %d",&s,&num);
/* and assert two elements have been read*/
while(ok == 2 && num!=-1)
{
new_node=malloc(sizeof(struct node));
new_node->data=s;
new_node->p=num;
if(start==NULL)
{
start=new_node;
z=start;
new_node->ptr=NULL;
}
else
{
z->ptr=new_node;
new_node->ptr=NULL;
z=z->ptr;
}
ok = scanf(" %c %d",&s,&num);
}
return start;
}
struct node *display()
{
struct node *z;
z=start;
printf("\nDisplaying elements:\n");
while(z!=NULL)
{
printf("\t%c [Priority=%d]\n",z->data,z->p);
z=z->ptr;
}
return start;
}
struct node *sortit()
{
struct node *z,*q;
int t;
char x;
z=start;
while(z->ptr!=NULL)
{
q=z->ptr;
while(q!=NULL)
{
if(z->p>q->p)
{
t=z->p;
x=z->data;
z->p=q->p;
z->data=q->data;
q->p=t;
q->data=x;
}
q=q->ptr;
}
z=z->ptr;
}
return start;
}
struct node *new_insert()
{
struct node *y,*z;
int n, ok;
char s;
printf("\nEnter character and priority for new node:");
ok = scanf(" %c %d",&s,&n);
if (2 == ok)
{
/* alloc y after having check that user gave usage stuff */
y = malloc(sizeof(struct node));
y->data=s;
y->p=n;
printf("%c",y->data);
printf("%d",y->p);
if(n<start->p)
{
y->ptr=start;
start=y;
printf("%d",y->p);
}
else
{
printf("\nff");
z=start;
while(z->ptr->p<=n&&z->ptr!=NULL)
z=z->ptr;
y->ptr=z->ptr;
z->ptr=y;
}
}
return start;
}
int main()
{
insert();
display();
sortit();
display();
new_insert();
display();
return 0;
}
I am trying to create an (ordered) linked list of (ordered) linked lists. The list-of-list links are carried by the first nodes of its member lists. I am trying to achieve this via the following code, but my program crashes right after I try to view the second node into the list of lists.
Here's a schematic of the data structure I am trying to construct:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node{
int number;
struct node*next;
struct node*lsnext;
};
typedef struct node Node;
Node* insertValue(Node * list, int value);
void display(Node*);
Node* insertArr(Node * list, int value);
int main()
{
Node *globalList = NULL, *lists,*start,*save;
int nbrOfLists, listNo, nbrOfVal, valNo, val;
printf("\n Enter the number of lists:");
scanf("%d", &nbrOfLists);
if(nbrOfLists < 0)
return -1;
for(listNo = 0; listNo < nbrOfLists; listNo++)
{
printf("\n\n Enter the number of inputs to the list %d: \n ",listNo+1);
scanf("%d", &nbrOfVal);
lists = NULL;
for(valNo = 0; valNo < nbrOfVal; valNo++)
{
printf("Enter node value %d:", valNo+1);
scanf("%d", &val);
// Here we insert the value in both lists
lists= insertValue(lists, val);
globalList = insertValue(globalList, val);
}
start=lists;
if(listNo==0){
save=start;
}
start=start->lsnext;
printf("\n The list %d is: ",listNo+1);
display(lists);
}
printf("\n\n The final list is: ");
display(globalList);
printf("The first list is");
display(save);
printf("The second list is");
display(save->lsnext); // CRASHES HERE
return 0;
}
Node* insertValue(Node * list, int value)
{
Node *newNode, *m;
newNode = malloc(sizeof(Node));
newNode->number=value;
if(list == NULL)
{
newNode->next=NULL;
return newNode;
}
if(value < list->number)
{
newNode->next = list;
return newNode;
}
m = list;
while(m->next)
{
if(value < m->next->number)
break;
m = m->next;
}
newNode->next = m->next;
m->next = newNode;
return list;
}
void display(Node*nodex){
while(nodex)
{
printf("%d ->",nodex->number);
nodex=nodex->next;
}
}
The problem is you never assign the lsnext node pointer. You will want to make sure you set this pointer equal to "lists" prior to resetting your start pointer. And you shouldn't need the following line of code:
start=start->lsnext;
That fixes your crashing issue that you mention, however the source code has other errors. Running your application through GDB will be invaluable in ensuring your pointer values are what you think they should be at each step, as well as a source code formatter to ensure readability, making it easier to tell which code belongs inside which blocks.
This is what I have tried so far.
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void create(struct node **q)
{
struct node *r,*t;
int cnt,i,dat;
printf("How many nodes?");
scanf("%d",&cnt);
for(i=0;i<cnt;i++)
{
printf("Enter the data %d ",i);
scanf("%d",&dat);
r=(struct node *) malloc (sizeof(struct node));
r->data=dat;
r->next=NULL;
if(*q==NULL)
{
*q=r;
}
else
{
t=*q;
while(t->next!=NULL)
{
t=t->next;
}
t->next=r;
}
}
}
void display(struct node **q)
{
struct node *r;
r=*q;
while(r!=NULL)
{
printf("%d->",r->data);
r=r->next;
}
printf("\n");
}
void max(struct node **q)
{
struct node *r;
int max=0;
r=*q;
while((r->next)!=NULL)
{
max=r->data;
r=r->next;
if((r->data)>max)
{
max=r->data;
}
}
printf("The max is %d",max);
printf("\n");
}
int main()
{
struct node *head;
create(&head);
display(&head);
max(&head);
}
If I use the numbers 1, 2, and 3 as input my program currently incorrectly detects 2 as the maximum element. How can I fix it so that it will correctly determine 3 to be the maximum element?
rutuparna#pucsd-rutuparna:~/C/Datastructures assignment$ gcc linkedmax.c
rutuparna#pucsd-rutuparna:~/C/Datastructures assignment$ ./a.out
How many nodes?3
Enter the data 0 3
Enter the data 1 2
Enter the data 2 1
3->2->1->
The max is 2
There are a few issues with the loop
while((r->next)!=NULL) {
max=r->data;
r=r->next;
if((r->data)>max) {
max=r->data;
}
}
The line max=r->data unconditionally updates max. Combining this with the fact that the test (r->next)!=NULL causes your loop to exit one item early, the loop currently always returns the value of the second last item.
The code should be changed to something like
while(r!=NULL) {
if(r->data>max) {
max=r->data;
}
r=r->next;
}
I am getting error while executing this. Compiler does not give any error but when executed it give random output.
What am i doing is taking input from user & storing them to linked list & then implementing insertion sort.
( I am able to call the subroutine insertion sort, so I guess the problem lies within subroutine only)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};
typedef struct node *NODEPTR;
NODEPTR getnode();
NODEPTR insertionSortLinkedList(NODEPTR head);
int main()
{
int i,n,temp;
NODEPTR head,lptr,prevlptr;
printf("No of input integers to be sorted\n");
scanf("%d",&n);
if (n<2){printf("n should be atleast 2 \n");return 0;}
printf("\nType space-separated array of %d integers\n",n);
scanf("%d",&temp);
head=getnode();
head->info=temp;
head->next=NULL;
prevlptr=head;
for (i=0;i<n-1;i++)
{
scanf("%d",&temp);
lptr=getnode();
lptr->info=temp;
prevlptr->next=lptr;
lptr->next=NULL;
prevlptr=lptr;
}
head=insertionSortLinkedList(head);
lptr=head;
while(lptr!=NULL)
{
printf("%d ",lptr->info);
prevlptr=lptr;
lptr=lptr->next;
free(prevlptr);
}
return 0;
}
NODEPTR getnode()
{
NODEPTR p;
p=(struct node*)malloc(sizeof(struct node));
return p;
}
NODEPTR insertionSort(NODEPTR head)
{
NODEPTR listptr,tempptr,prevptr,prevtempptr;
prevptr=head;
listptr=prevptr->next;
while(listptr!=NULL)
{
while (listptr->info < prevptr->info)
{
prevptr->next=listptr->next;
tempptr=head;
prevtempptr=head;
while(tempptr->info <= listptr->info)
{
prevtempptr=tempptr;
tempptr=tempptr->next;
}
if(tempptr->info == prevtempptr->info)
{
listptr->next=head;
head=listptr;
}
else
{e
listptr->next=prevtempptr->next;
prevtempptr->next=listptr;
}
listptr=prevptr->next;
if (listptr==NULL)
break;
}
prevptr=prevptr->next;
if (prevptr=NULL)
break;
listptr=prevptr->next;
}
return(head);
}
What is wrong with my code and how do I fix it?
The 6th line form last
if (prevptr=NULL)
it should be
if (prevptr==NULL)
Single "=" does the assignment instead of comparison.
Here is my program which creates a link list and also reverses it.
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *list=NULL;
struct node *root=NULL;
static int count=0;
struct node *create_node(int);//function to create node
void travel_list(void);
void create_list(int);
void reverse_list(void);
int main()
{
int i, j, choice;
printf("Enter a number this will be root of tree\n");
scanf("%d", &i);
create_list(i);
printf("Enter 1 to enter more numbers \n 0 to quit\n");
scanf("%d", &choice);
while (choice!=0){
printf("Enter a no for link list\n");
scanf("%d",&i);
// printf("going to create list in while\n");
create_list(i);
travel_list();
printf("Enter 1 to enter more numbers \n 0 to quit\n");
scanf("%d", &choice);
}
printf("reversing list\n");
reverse_list();
travel_list();
}
// end of function main
void create_list (int data)
{
struct node *t1,*t2;
//printf("in function create_list\n");
t1=create_node(data);
t2=list;
if( count!=0)
{
while(t2->next!=NULL)
{
t2=t2->next;
}
t2->next=t1;
count++;
}
else
{
root=t1;
list=t1;
count++;
}
}
struct node *create_node(int data)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->next=NULL;
// printf("create node temp->data=%d\n",temp->data);
// printf("the adress of node created %p\n",temp);
return temp;
}
void travel_list(void )
{
struct node *t1;
t1=list;
printf("in travel list\n");
while(t1!=NULL)
{
printf("%d-->",t1->data);
t1=t1->next;
}
printf("\n");
}
void reverse_list(void)
{
struct node *t1,*t2,*t3;
t1=list;
t2=list->next;
t3=list->next->next;
int reverse=0;
if(reverse==0)
{
t1->next=NULL;
t2->next=t1;
t1=t2;
t2=t3;
t3=t3->next;
reverse++;
}
while(t3!=NULL)
{
t2->next=t1;
t1=t2;
t2=t3;
list=t1;
travel_list();
t3=t3->next;
}
t2->next=t1;
list=t2;
}
Above is a fully working code.
I want to know if there can be further enhancement to the above code?
Make your indentation and whitespace usage consistent
Use meaningful identifiers rather than t1, t2 and t3
Make the data member a generic type, for example void * rather than int.
Don't use global variables, pass struct node * pointers to your functions.