#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node* HEAD;
void Deleteatn(int c)
{
struct node *store,*store1,*temp=HEAD;
if(c==1)
{
store=temp->next;
store->prev=NULL;
HEAD=store;
free(temp);
}
else
{
for(int i=1;i<c-1;i++)
temp=temp->next;
store=temp->next;
store1=store->next;
temp->next=store->next;
//store1->prev=temp;//DOUBT
free(store);
}
}
void print()
{
struct node *temp=HEAD;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
void Insertatend(int b)
{
struct node *n;
n=(struct node *)malloc(sizeof(struct node));
n->data=b;
n->prev=NULL;
n->next=NULL;
if(HEAD==NULL)
HEAD=n;
else
{
struct node *store,*temp=HEAD;
while(temp!=NULL)
{
store=temp;
temp=temp->next;
}
store->next=n;
n->prev=store;
}
}
int main()
{
int a,b,c;
printf("How many Numbers need to be inserted?\n");
scanf("%d",&a);
for(int i=0;i<a;i++)
{
printf("Enter a number\n");
scanf("%d",&b);
Insertatend(b);
}
printf("The List is\n");
print();
printf("Enter which node need to be deleted?\n");
scanf("%d",&c);
Deleteatn(c);
printf("The List After Deletion is\n");
print();
return 0;
}
Here I have written a program to delete a nth node in a doubly linked list ,I have a doubt that without making a Backward link,how does the output come correctly.So in doubly linked list is it not mandatory to make both forward and backward link?
I have mentioned the code line as doubt,without that code,how is it working???
You can find the issue when you traverse back.
Since you are traversing in forward direction its working fine like a singly linked list.
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;
}`
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;
}
Here is the code i wrote to check if a singly linked list of integers is a palindrome or not.
#include<stdio.h>
#include<stdlib.h>
struct list
{
int data;
struct list *next;
};
struct list *insert(int data,struct list *node)
{
if(node==NULL)
{
node=(struct list *)malloc(sizeof(struct list));
node->data=data;
node->next=NULL;
}
else
{
struct list *newnode=(struct list *)malloc(sizeof(struct list));
newnode->data=data;
newnode->next=node;
node=newnode;
}
return node;
}
int palindrome(struct list *node,int n)
{
int i=0;int j=0;
int arr1[n],arr2[n];
struct list *current;
current=node;
while(current!=NULL)
{
arr1[i]=current->data;
i++;
current=current->next;
}
i=0;j=0;
for(i=n-1;i>=0;i--)
{
arr2[j]=arr1[i];
j++;
}
for(i=0;i<n;i++)
{
if(arr1[i]!=arr2[i])
{
return 0;
}
}
return 1;
}
void main()
{
int n;
scanf("%d",&n);
struct list *node=NULL;
int i=1;int value;
for(i=1;i<=n;i++)
{
scanf("%d",&value);
insert(value,node);
}
int status=palindrome(node,n);
printf("%d",status);
}
But the code returns 0 even in case of valid palindrome inputs like "121" and also in non - palindrome inputs like "154". Please help. Thanks
You need to write
node = insert(value,node);
in main. Otherwise the head node is not changed because the function insert deals with a copy of the node.
codepad linkI'm trying to insert into a linked list using double pointers.but i don't understand where i'm going wrong i followed up other links on stack overflow,i even referred to couple of books so please help me.i kept code for insertion at position 1.In the output the previous insertion is lost.
struct node
{
int data;
node *next;
};
void insert(node **head,int k,int pos)//k refers to the element to be inserted
{
if(pos==1)
{
node *newnode=(node *)malloc(sizeof(node));
newnode->data=k;
newnode->next=*head;
*head=newnode;
}
}
void print(node **head)
{
printf("the elements are.. ");
while(*head!=NULL)
{
printf("%d ",(*head)->data);
(*head)=(*head)->next;
}
printf("\n");
}
int main()
{
insert(&head,5,1);
print(&head);
insert(&head,4,1);
print(&head);
return 0;
}
sorry for poor indentation.i'm beginner please help me.
Your print function is incorrect. You are erasing your head in line (*head)=(*head)->next;. Change function to
void print(node **head)
{
printf("the elements are.. ");
node *temp = *head;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
You will receive following output:
the elements are.. 5
the elements are.. 4 5
Check this out.
struct node //Missed struct's name
{
int data;
node *next;
};
void insert(node **head,int k,int pos)//k refers to the element to be inserted
{
if(pos==1)
{
node *newnode= new node();
newnode->data=k;
newnode->next=*head; //You called head which is not a member of node's struct
*head=newnode;
}
}
int main()
{
node *head=NULL;
insert(&head,5,1);
insert(&head,4,1);
}