Check whether a Singly Linked List is a Palindrome
#include
#include
#include
struct node
{
int num;
struct node *next;
};
int create(struct node **);
int palin_check (struct node *, int);
void release(struct node **);
int main()
{
struct node *p = NULL;
int result, count;
printf("Enter data into the list\n");
count = create(&p);
result = palin_check(p, count);
if (result == 1)
{
printf("The linked list is a palindrome.\n");
}
else
{
printf("The linked list is not a palindrome.\n");
}
release (&p);
return 0;
}
int palin_check (struct node *p, int count)
{
int i = 0, j;
struct node *front, *rear;
while (i != count / 2)
{
front = rear = p;
for (j = 0; j < i; j++)
{
front = front->next;
}
for (j = 0; j < count - (i + 1); j++)
{
rear = rear->next;
}
if (front->num != rear->num)
{
return 0;
}
else
{
i++;
}
}
return 1;
}
int create (struct node **head)
{
int c, ch, count = 0;
struct node *temp;
do
{
printf("Enter number: ");
scanf("%d", &c);
count++;
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = *head;
*head = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
}while (ch != 0);
printf("\n");
return count;
}
void release (struct node **head)
{
struct node *temp = *head;
while ((*head) != NULL)
{
(*head) = (*head)->next;
free(temp);
temp = *head;
}
}
Output
Enter data into the list
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 1
Do you wish to continue [1/0]: 0
The linked list is a palindrome.
Convert a given Singly Linked List to a Circular List
#include
#include
#include
struct node
{
int num;
struct node *next;
};
void create(struct node **);
void tocircular(struct node **);
void release(struct node **);
void display(struct node *);
int main()
{
struct node *p = NULL;
int result, count;
printf("Enter data into the list\n");
create(&p);
tocircular(&p);
printf("Circular list generated\n");
display(p);
release (&p);
return 0;
}
void tocircular(struct node **p)
{
struct node *rear;
rear = *p;
while (rear->next != NULL)
{
rear = rear->next;
}
rear->next = *p;
}
void create(struct node **head)
{
int c, ch;
struct node *temp;
do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = *head;
*head = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
}
while (ch != 0);
printf("\n");
}
void display(struct node *head)
{
struct node *temp = head;
printf("Displaying the list elements\n");
printf("%d\t", temp->num);
temp = temp->next;
while (head != temp)
{
printf("%d\t", temp->num);
temp = temp->next;
}
printf("and back to %d\t%d ..\n", temp->num, temp->next->num);
}
void release(struct node **head)
{
struct node *temp = *head;
temp = temp->next;
(*head)->next = NULL;
(*head) = temp->next;
while ((*head) != NULL)
{
free(temp);
temp = *head;
(*head) = (*head)->next;
}
}
Output
Enter data into the list
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 5
Do you wish to continue [1/0]: 0
Circular list generated
Displaying the list elements
5 4 3 2 1 and back to 5 4 ..
Detect the
Cycle in a Linked List
#include
#include
#include
struct node
{
int num;
struct node *next;
};
void create(struct node **);
void makecycle(struct node **);
void release(struct node **);
int detectcycle(struct node *);
int main()
{
struct node *p = NULL;
int result;
printf("Enter data into the list\n");
create(&p);
makecycle(&p);
printf("Identifying if a cycle exists.\n");
result = detectcycle(p);
if (result)
{
printf("Cycle detected in the linked list.\n");
}
else
{
printf("No cycle detected in the linked list.\n");
}
release (&p);
return 0;
}
void makecycle(struct node **p)
{
struct node *rear, *front;
int n, count = 0, i;
front = rear = *p;
while (rear->next != NULL)
{
rear = rear->next;
count++;
}
if (count)
{
n = rand() % count;
}
else
{
n = 1;
}
for (i = 0; i < n - 1; i++)
{
front = front->next;
}
rear->next = front;
}
int detectcycle(struct node *head)
{
int flag = 1, count = 1, i;
struct node *p, *q;
p = q = head;
q = q->next;
while (1)
{
q = q->next;
if (flag)
{
p = p->next;
}
if (q == p)
{
q = q->next;
while (q != p)
{
count++;
q = q->next;
}
q = p = head;
for (i = 0; i < count; i++)
{
q = q->next;
}
while (p != q)
{
p = p->next;
q = q->next;
}
q->next = NULL;
return 1;
}
else if (q->next == NULL)
{
return 0;
}
flag = !flag;
}
}
void create(struct node **head)
{
int c, ch;
struct node *temp, *rear;
do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
} while (ch != 0);
printf("\n");
}
void release(struct node **head)
{
struct node *temp = *head;
temp = temp->next;
while ((*head) != NULL)
{
free(temp);
temp = *head;
(*head) = (*head)->next;
}
}
Output
Enter data into the list
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 5
Do you wish to continue [1/0]: 1
Enter number: 6
Do you wish to continue [1/0]: 1
Enter number: 7
Do you wish to continue [1/0]: 0
Identifying if a cycle exists.
Cycle detected in the linked list..
No comments:
Post a Comment