0% found this document useful (0 votes)
71 views8 pages

How Do You Find The Middle of A Linked List? Write A C Program To Return The Middle of A Linked List

This document contains code for finding the middle node of a linked list in C. It defines a struct node with value, next, and prev pointers. It adds several nodes to a list and passes the head to the getTheMiddle function. getTheMiddle uses two pointers, p and q, initialized to the head. It iterates q twice as fast as p to find the middle node, then prints its value.

Uploaded by

smitha_gururaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views8 pages

How Do You Find The Middle of A Linked List? Write A C Program To Return The Middle of A Linked List

This document contains code for finding the middle node of a linked list in C. It defines a struct node with value, next, and prev pointers. It adds several nodes to a list and passes the head to the getTheMiddle function. getTheMiddle uses two pointers, p and q, initialized to the head. It iterates q twice as fast as p to find the middle node, then prints its value.

Uploaded by

smitha_gururaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

How do you find the middle of a linked list?

Write a C program to return the middle of a


linked list
#include<stdio.h>
#include<ctype.h>
typedef struct node
{
int value;
struct node *next;
struct node *prev;
}mynode;

void add_node(struct node **head, int value);


void print_list(char *listName, struct node *head);
void getTheMiddle(mynode *head);

int main()
{
mynode *head;
head = (struct node *)NULL;
add_node(&head, 1);
add_node(&head, 10);
add_node(&head, 5);
add_node(&head, 70);
add_node(&head, 9);
add_node(&head, -99);
add_node(&head, 0);
add_node(&head, 555);

add_node(&head, 55);
print_list("myList", head);
getTheMiddle(head);
getch();
return(0);
}

void getTheMiddle(mynode *head)


{
mynode *p = head;
mynode *q = head;
if(q!=NULL)
{
while((q->next)!=NULL && (q->next->next)!=NULL)
{
p=(p!=(mynode *)NULL?p->next:(mynode *)NULL);
q=(q!=(mynode *)NULL?q->next:(mynode *)NULL);
q=(q!=(mynode *)NULL?q->next:(mynode *)NULL);
}
printf("The middle element is [%d]",p->value);
}
}

void add_node(struct node **head, int value)


{
mynode *temp, *cur;
temp = (mynode *)malloc(sizeof(mynode));

temp->next=NULL;
temp->prev=NULL;
if(*head == NULL)
{
*head=temp;
temp->value=value;
}
else
{
for(cur=*head;cur->next!=NULL;cur=cur->next);
cur->next=temp;
temp->prev=cur;
temp->value=value;
}
}

void print_list(char *listName, struct node *head)

mynode *temp;

printf("\n[%s] -> ", listName);

for(temp=head;temp!=NULL;temp=temp->next)
{

printf("[%d]->",temp->value);

printf("NULL\n");
}

Write a C program to implement a Generic Linked List.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct list
{
void *data;
struct list *next;
}List;

struct check
{
int i;
char c;
double d;
} chk[] = { { 1, 'a', 1.1 },
{ 2, 'b', 2.2 },
{ 3, 'c', 3.3 } };

void insert(List **, void *, unsigned int);


void print(List *, void (*)(void *));
void printstr(void *);

void printint(void *);


void printchar(void *);
void printcomp(void *);
List *list1, *list2, *list3, *list4;
int main(void)
{
char c[] = { 'a', 'b', 'c', 'd' };
int i[] = { 1, 2, 3, 4 };
char *str[] = { "hello1", "hello2", "hello3", "hello4" };
list1 = list2 = list3 = list4 = NULL;
insert(&list1, &c[0], sizeof(char));
insert(&list1, &c[1], sizeof(char));
insert(&list1, &c[2], sizeof(char));
insert(&list1, &c[3], sizeof(char));
insert(&list2, &i[0], sizeof(int));
insert(&list2, &i[1], sizeof(int));
insert(&list2, &i[2], sizeof(int));
insert(&list2, &i[3], sizeof(int));
insert(&list3, str[0], strlen(str[0])+1);
insert(&list3, str[1], strlen(str[0])+1);
insert(&list3, str[2], strlen(str[0])+1);
insert(&list3, str[3], strlen(str[0])+1);
insert(&list4, &chk[0], sizeof chk[0]);
insert(&list4, &chk[1], sizeof chk[1]);
insert(&list4, &chk[2], sizeof chk[2]);
printf("Printing characters:");
print(list1, printchar);

printf(" : done\n\n");
printf("Printing integers:");
print(list2, printint);
printf(" : done\n\n");
printf("Printing strings:");
print(list3, printstr);
printf(" : done\n\n");
printf("Printing composite:");
print(list4, printcomp);
printf(" : done\n");
return 0;

void insert(List **p, void *data, unsigned int n)


{
List *temp;
int i;
/* Error check is ignored */
temp = malloc(sizeof(List));
temp->data = malloc(n);
for (i = 0; i < n; i++)
*(char *)(temp->data + i) = *(char *)(data + i);
temp->next = *p;
*p = temp;
}

void print(List *p, void (*f)(void *))


{
while (p)
{
(*f)(p->data);
p = p->next;
}
}

void printstr(void *str)


{
printf(" \"%s\"", (char *)str);
}

void printint(void *n)


{
printf(" %d", *(int *)n);
}

void printchar(void *c)


{
printf(" %c", *(char *)c);
}

void printcomp(void *comp)


{

struct check temp = *(struct check *)comp;


printf(" '%d:%c:%f", temp.i, temp.c, temp.d);
}

You might also like