Palindrom check of linkedlist - c

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.

Related

How to solve the pointer issue in the double linked list below?

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;
}`

unable to insert in a linked list

i am trying to insert an element in the given linked list but when i want to print it its showing infinite loop.In insert() call i am passing the address of the pointer whereas in display i am only passing the address stored in the pointer. help me plz
#include<stdio.h>
struct node{
int item;
node *ptr;
};
int insert(node **head, int data, int position)
{
int count=1;
node *temp=malloc(sizeof(struct node));
temp->item=data;
temp->ptr=NULL;
node *p,*q;
p=*head;
if(*head==NULL)
{
*head=temp;
}
if(position==1)
{
temp->item=*head;
*head=temp;
}
else{
while(p!=NULL&&count<position)
{
count++;
q=p;
p=p->ptr;
}
q->ptr=temp;
temp->ptr=p;
}
}
void display(node *temp)
{
while(temp!=NULL)
{
printf("the data is %d",temp->item);
temp=temp->ptr;
}
}
void main()
{
node *head=malloc(sizeof(struct node));
insert(&head,29,1);
display(head);
}
Its a C Program please categorize properly.
No use of any C++ modules in your code.
Going with C implementation -
I think the double pointer is not required in a simple insert into a linked list.
You can do is -
void insert(node *head, int data){
node *temp= malloc(sizeof(struct node));
temp->item = data;
temp->ptr= NULL;
if(head==NULL){ //empty list
head=temp;
}
else {
while(head->ptr!=NULL){ //traverse till end
head=head->ptr;
}
head->ptr=temp;
}
}
And from main function call as insert(head,value);

insert in Linked List Turbo C

I am trying to program a text editor in C. I am having trouble with inserting an element in a linked list. The program simply won't insert anything in the middle of the linked list.
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
I used singly linked list.
struct node {
struct node *previous;
int c;
int x;
int y;
struct node *next;
}*head;
this works fine:
void characters(int typed, int xpos, int ypos) //assign values of a node
{
struct node *temp,*var,*temp2;
temp=(struct node *)malloc(sizeof(struct node));
temp->c=typed;
temp->x=xpos;
temp->y=ypos;
if(head==NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp2=head;
while(temp2!=NULL)
{
var=temp2;
temp2=temp2->next;
}
temp2=temp;
var->next=temp2;
temp2->next=NULL;
}
}
this works just fine too.
void printer() //to print everything
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
gotoxy(temp->x,temp->y);
printf("%c",temp->c);
temp=temp->next;
}
}
this works just fine too:
void deletesEnd //delete at the end
{
struct node *temp,*last;
temp=head;
last=temp;
while(temp!=NULL && temp->next!=NULL)
{
last=temp;
temp=temp->next;
}
if(last==temp)
{
free(temp);
head=NULL;
}
else{
free(last->next);
last->next=NULL;
}
}
THIS IS THE PROBLEM:
void checker(int ch, int xpos, int ypos)
{
int flag=0;
struct node *temp,*temp1,*insert_node;
temp=head;
while(temp!=NULL)
{
if(temp->x==xpos && temp->y==ypos)
{
temp1=temp;
temp=insert_node;
insert_node->c=ch;
insert_node->x=xpos;
insert_node->y=ypos;
insert_node->next=temp1;
flag=1;
break;
}
else
temp= temp->next;
}
free(temp);
free(temp1);
if(flag==0)
characters(ch,xpos,ypos);
}
main()
{
int c; //for storing the character
int x,y; //for the position of the character
clrscr();
for(;;)
{
c=getch();
x=wherex();
y=wherey();
if(c==27)
exit(0);
else if(c==0|| c==224)
{
switch(getch())
{
case 72: //for up
gotoxy(x,y-1);
break;
case 80: //for down
gotoxy(x,y+1);
break;
case 75: //for left
gotoxy(x-1,y);
break;
case 77: //for right
gotoxy(x+1,y);
break;
}
}
else if(c==13)
{
printf("\n");
}
else if(c==8) //for backspace
{
deletesEnd();
clrscr();
printer();
}
else //for normal characters
{
checker(c,x,y);
// characters(c,x,y);
printer();
}
}
}
I tried to debug it, it goes inside the loop with the conditional statement of ((temp->x==xpos && temp->y==ypos)) Thus, the program is supposed to insert an element but it doesn't. :(
Maybe you have to malloc a struct node for the element you want to insert first, not just declare a struct node *.
Try to add struct node *insert_node = (struct node *)malloc(struct node) in your checker method.
Try concept of insertion from this one - It can insert a node from front end
Just make a new pointer to node and name it create
struct node *create;
int item;
printf("Enter a number you want to insert\n\t");
scanf("%d",&item);
create = (struct node*)malloc(sizeof(struct node*));
create->info = item;
create->ptr = first;
HEAD = create;

Simpler and faster code for reversing list?

I wrote the code for reversing a doubly linked list containing words in each node, which works perfectly fine.
My teacher says the algorithm is difficult to understand and the code as a whole could be made more efficient(reducing overhead and memory consumption). What changes can i make to the code/the reversing algorithm?
Also is there a way i could input the sentence without having to ask the number of words in advance? Here is the code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct NODE
{
char *item;
struct NODE *next;
struct NODE *prev;
}NODE;
void Insert(char data[],NODE **List)
{
NODE *temp,*last;
last=(*List);
temp=(NODE*)malloc(sizeof(NODE));
temp->item=(char*)malloc(strlen(data));
temp->item=data;
temp->next=NULL;
temp->prev=NULL;
if((*List)->item==NULL)
(*List)=temp;
else
{
while(last->next!=NULL)
last=last->next;
temp->prev=last;
last->next=temp;
last=temp;
}
}
void Reverse(NODE **List)
{
int flag1=0;
NODE *temp,*temp1,*last,*flag;
temp1=(NODE*)malloc(sizeof(NODE));
last=(*List);
while(last->next!=NULL)
last=last->next;
temp=last;
while(temp->prev!=NULL)
{
temp1->item=temp->item;
temp1->next=temp->next;
temp1->prev=temp->prev;
temp->next=temp->prev;
temp->prev=temp1->next;
temp=temp->next;
if(flag1==0)
{
flag1++;
flag=temp;
}
}
temp1->item=temp->item;
temp1->next=temp->next;
temp1->prev=temp->prev;
temp->next=NULL;
temp->prev=temp1->next;
(*List)=flag->prev;
free(temp1);
};
void display(NODE *List)
{
if(List->next==NULL)
{
printf("%s",List->item);
return;
}
NODE *temp;
temp=List;
do
{
printf("%s<-->",temp->item);
temp=temp->next;
}while(temp->next!=NULL);
printf("%s\n",temp->item);
}
int main()
{
int i=0,n;
char s[10][50];
NODE *List;
List=(NODE*)malloc(sizeof(NODE));
List->item=NULL;
List->next=NULL;
List->prev=NULL;
printf("Provide number of words(max 10): ");
scanf("%d",&n);
printf("Enter string of words for the list: ");
while(i<n)
{
scanf("%s",s[i]);
Insert(s[i],&List);
i++;
}
printf("\nOriginal List is: ");
display(List);
Reverse(&List);
printf("\nReversed List is: ");
display(List);
getch();
return 0;
}
Since it's a double-linked list you could just write two traversal functions. One forward and one reverse. Save two anchors for your list in your control structure: one for the first element and one for the last.
You can just swap the next and previous pointers for each node and swap tail and head pointers.
void reverse (struct node *ptr)
{
struct node *tmp, *kid;
if (!ptr) return;
for (kid = ptr->next; kid; kid = kid->prev) {
tmp = kid->prev;
kid->prev = kid->next;
kid->next = tmp;
}
for (kid = ptr->prev; kid; kid = kid->next) {
tmp = kid->prev;
kid->prev = kid->next;
kid->next = tmp;
}
tmp = ptr->prev;
ptr->prev = ptr->next;
ptr->next = tmp;
return;
}
Note: I removed the typedef. I hate typedefs.

What is wrong with my code for singly linked list?

I'm working on a singly linked list in C. This is what I've written so far.
C program
#include<stdio.h>
#include<stdlib.h>
struct Node{
int value;
struct Node *next;
};
struct Node* init()
{
struct Node* head=NULL;
head=malloc(sizeof(struct Node));
head->value=-1;
return head;
}
int length(struct Node* head)
{
struct Node* current=head;
int length=0;
while(current!=NULL)
{
length++;
current=current->next;
}
return length;
}
void print(struct Node* head)
{
int i=0;
int len=length(head);
for(i=0;i<len;i++)
{
printf("%d%d",i,head[i].value);
printf("\n");
}
}
struct Node* insert(int data,struct Node* head)
{
struct Node* current=NULL;
if(length(head) > 0)
{
int val=head->value;
if (val==-1)
{
head->value=data;
head->next=NULL;
}
else
{
current=malloc(sizeof(struct Node));
current->value=data;
current->next=head;
head=current;
}
}
else
{
printf("List is empty");
}
return head;
}
int main()
{
/* printf("Hello"); */
struct Node *head=init();
head=insert(20,head);
head=insert(30,head);
head=insert(40,head);
print(head);
printf("%d",length(head));
return 0;
}
The output values I get are:
Index Value
0 40
1 0
2 0
and for length is 3. I'm not able to grasp what I'm doing wrong here in pointer manipulation.
One obvious problem is not setting next to NULL on init - that would fail when checking length on the empty list
But your real problem is the print function
You can't use:
head[i].value
That notation is only valid for arrays, you need to use next to find each member
The Init function should set Next to NULL
struct Node* init()
{
struct Node* head=NULL;
head=malloc(sizeof(struct Node));
head->value=-1;
head->next=NULL;
return head;
}
otherwise the first call to length return an undefined result ( or GPF ).
Here:
for (i = 0; i < len; i++)
{
printf("%d%d", i, head[i].value);
printf("\n");
}
You need to advance from one node to another with head = head->next in the same manner as you do it in length(). head[i] won't do it.
It's unclear why your init() and insert() are so unnecessarily complicated and I don't even want to try to guess why. I want to suggest a better insert() and no init():
struct Node* insert(int data, struct Node* head)
{
struct Node* current;
current = malloc(sizeof(struct Node));
current->value = data;
current->next = head;
return current;
}
And then you do this:
int main(void)
{
struct Node *head = NULL;
head = insert(20, head);
head = insert(30, head);
head = insert(40, head);
print(head);
printf("%d", length(head));
return 0;
}
The notation head[i].value is only valid for arrays but not for linked lists. Arrays and linked lists are completely different, allocation of memory towards arrays is premeditated where as for linked lists it's dynamic. That is the reason why we use pointers for linked lists.
In init() you didn't assign null which causes the loop to run infinite times when you call length() for first time.
I am posting the modified code of print function:
void print(struct Node* head)
{
int i=0;
int len=0;
struct Node* current=head;
for(i=0;i<len;i++)
{
printf("%d %d",i,current->value);
print("\n");
current=current->next;
}
}

Resources