manidavid0
BAN USER- 0of 0 votes
AnswersTo Check the given linked list is palindrome or not?
- manidavid0 in India| Report Duplicate | Flag | PURGE
Algorithm
This algo is to find min & max value in the stack using linked list.
the idea is create the linked list by adding new nodes at top(first), after forming the list find the minimum&maximum value in it.
this linked list uses stack procedure to create.
To find the min value,
int find_min(struct node *p)
{
p=top;
int mn=p->data;
while(p!=NULL)
{
if(mn > p->data)
{
mn=p->data;
p=p->link;
}
else
p=p->link;
}
return mn;
}
To find the max value
int find_max(struct node *q)
{
q=top;
int mx=q->data;
while(q!=NULL)
{
if(mx < q->data)
{
mx=q->data;
q=q->link;
}
else
q=q->link;
}
return mx;
}
my poly algo is suffer the test case : 846348
can any one tell me how to the solve this problem in my algo?
my algo is working for perfect values like polyndrome:85458 and non-polyndrome:597643
1.form the linked list,in struct node *p
my structure is,
struct node
{
int data;
struct node *link;
}*p,*m;
1.1 After forming the linked list,
2.copy the list into another struct pointer ,struct node *m, m=p;
3.reverse the linked list,using reverse algo pass the argument as p;
4.reverse algo
reverse()
{
struct node *q,*r,*s;
q=p;
r=NULL;
while(q!=NULL)
{
s=r;
r=q;
q=q->link;
r->link=s;
}
p=r;
}
5.check the linked list m and p to find polyndrome or not
6.polyndrome algo
poly()
{
int flag;
while(m!=NULL)
{
if(m->data==p->data)
{
flag=0;
m=m->link;
p=p->link;
}
else
{
flag=1;
break;
}
}
if(flag==1)
printf("\n\nThe given linked list is not a polyndrome\n\n");
else
printf("\n\nThe given linked list is a polyndrome\n\n");
}
using linear search strategy:
for(i=0;i<n;i++)
if(A[i]!=0)
return i+1;
return 0;
what is the value for high?
its not working for 0000011 this test case
using binary search strategy and with out recursive function:
int low=0,hi=n-1;
while(low<=hi)
{
int mid=(low+hi)/2;
if(A[mid-1]==1)
hi=mid-1;
else if(A[mid]==1)
return mid+1;
else if(A[mid+1]==1)
low=mid+1;
else
low=mid+1;
}
return 0;
can any one tell me the time complixity for this algo
using binary search technique, time complexity is O(log n)
int low=0,hi=n-1;
while(low<=hi)
{
int mid=(low+hi)/2;
if(A[mid-1]==1)
hi=mid-1;
else if(A[mid]==1)
return mid+1;
else if(A[mid+1]==1)
low=mid+1;
else
low=mid+1;
}
return 0;
any change pls reply
No need , because the *p is declared globally,
- manidavid0 March 01, 2012i think the problem is , assigning the p into m,
how to copy the linked list into another...
i thinking ,the normal assigning is not working like m=p;