1: #include <stdio.h>
2: #include <stdlib.h>
3: typedef struct my_list
4: {
5: struct my_list *next;
6: char ch;
7: }my_list_t;
8: char list_data1[]={'a','b','c','b','a','\0'};
9: char list_data2[]={'f','a','b','c','a','v','\0'};
10: void adding_node_to_list( my_list_t **list, char data)
11: {
12: my_list_t *node = malloc(sizeof(my_list_t));
13: node->next = NULL;
14: node->ch = data;
15: if( NULL == list)
16: {
17: *list = node;
18: } else {
19: node->next = *list;
20: *list = node;
21: }
22: }
23: void display_list(my_list_t *list)
24: {
25: if( NULL != list)
26: {
27: printf("%c%s",list->ch, list->next==NULL?"\n":"->");
28: display_list(list->next);
29: }
30: return;
31: }
32: int check_for_palindrom(my_list_t *start)
33: {
34: int len = 0, i = 1;
35: my_list_t *temp = start, *cur;
36: while(NULL != temp)
37: {
38: len++;
39: temp = temp->next;
40: }
41: printf("len = %d\n",len);
42: temp = start;
43: cur = temp;
44: while( (len/2) != 0)
45: {
46: while(i != len)
47: {
48: cur = cur->next;
49: i++;
50: }
51: printf("%c === %c\n",temp->ch,cur->ch);
52: if(temp->ch != cur->ch)
53: return 1;
54: temp = temp->next;
55: cur = start;
56: len--;
57: i = 1;
58: }
59: return 0;
60: }
61: main()
62: {
63: int i = 0;
64: my_list_t *list[2] = {NULL};
65: printf("make 1st list\n");
66: for(i=0;i<list_data1[i] != '\0';i++)
67: adding_node_to_list(&list[0],list_data1[i]);
68: for(i=0;i<list_data2[i] != '\0';i++)
69: adding_node_to_list(&list[1],list_data2[i]);
70: printf("Done.\n");
71: for(i = 0 ; i<2 ; i++)
72: {
73: if ( 0 == check_for_palindrom(list[i]))
74: {
75: display_list(list[0]);
76: printf("is palindrome\n");
77: } else {
78: display_list(list[0]);
79: printf("Not a palindrome\n");
80: }
81: }
82: printf("Done.\n");
83: }
RTOS, Linux Kernel internal, OS-Programming C & Data Structures, Debugging, Optimizations, Makefiles and Wireless Technologies (Wi-Fi ,LTE and LTE-Advanced )
Search This Blog
Saturday, October 17, 2015
find out whether the given starting address of a single linked lists data is appended as palindrome or not. Ex: a->b->c->->b->a (Is a palindrome linked lists) f->a->b->c->a->v (Not a palindrome linked lists)
Labels:
Data Structure
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment