0% found this document useful (0 votes)
22 views9 pages

2

Uploaded by

Palak Thakur
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)
22 views9 pages

2

Uploaded by

Palak Thakur
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/ 9

//2.

Write a program to find the length of the linked list using recursion

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node*next;

};

int lengthOflinkedlist(struct node*p)

if(p==NULL)

return 0;

else

return 1+lengthOflinkedlist(p->next);

int main()

struct node*p;//a.creating a linked list

struct node*q;

struct node*r;

struct node*s;

struct node*t;

p=(struct node*)malloc(sizeof(struct node));

q=(struct node*)malloc(sizeof(struct node));

r=(struct node*)malloc(sizeof(struct node));

s=(struct node*)malloc(sizeof(struct node));

t=(struct node*)malloc(sizeof(struct node));

p->data=18;

p->next=q;//p->next stores the address of q


q->data=23;

q->next=r;//q->next stores the address of r

r->data=49;

r->next=s;//r->next stores the address of s

s->data=59;

s->next=t;//s->next stores the address of t

t->data=62;

t->next=NULL;

int count =lengthOflinkedlist(p);

printf("Length of linked list = %d",count);

return 0;

Output:

//5.Write a program to delete alternate element of a linked list

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node*next;

};

void display(struct node*p)//i. displaying elemnets of linked list

int i=1;

while(p!=NULL)

{
printf("Entered number = %d\n",p->data);

p=p->next;

struct node*alt_delete(struct node*p)

struct node* curr=p;

struct node *temp=p->next;

while(curr!=NULL&&temp!=NULL)

curr->next=temp->next;

free(temp);

curr=curr->next;

if(curr!=NULL)

temp=curr->next;

return p;

int main()

struct node*p;//a.creating a linked list

struct node*q;

struct node*r;

struct node*s;

struct node*t;

p=(struct node*)malloc(sizeof(struct node));

q=(struct node*)malloc(sizeof(struct node));

r=(struct node*)malloc(sizeof(struct node));

s=(struct node*)malloc(sizeof(struct node));

t=(struct node*)malloc(sizeof(struct node));


p->data=18;

p->next=q;//p->next stores the address of q

q->data=23;

q->next=r;//q->next stores the address of r

r->data=49;

r->next=s;//r->next stores the address of s

s->data=59;

s->next=t;//s->next stores the address of t

t->data=62;

t->next=NULL;

printf("Original linked list\n");

display(p);

printf("linked list after deleting elements\n");

p=alt_delete(p);

display(p);

return 0;

Output:

//6.Write a program to check whether the linked list is a palindrome or not.

#include<stdio.h>

#include<stdlib.h>

struct node

int data;
struct node*next;

};

int checkPlaindrome(struct node*p)

struct node*a=p;

struct node*b=p;

while(b!=NULL&& b->next!=NULL)

a=a->next;

b=b->next->next;

struct node* curr=a;

struct node *prev=NULL;

struct node *tem=NULL;

while(curr!=NULL)

tem=curr->next;

curr->next=prev;

prev=curr;

curr=tem;

b=p;

while(prev!=NULL)

if(b->data!=prev->data)

return 0;

b=b->next;

prev=prev->next;

return 1;
}

int main()

struct node*p;//a.creating a linked list

struct node*q;

struct node*r;

struct node*s;

struct node*t;

p=(struct node*)malloc(sizeof(struct node));

q=(struct node*)malloc(sizeof(struct node));

r=(struct node*)malloc(sizeof(struct node));

s=(struct node*)malloc(sizeof(struct node));

t=(struct node*)malloc(sizeof(struct node));

p->data=18;

p->next=q;//p->next stores the address of q

q->data=23;

q->next=r;//q->next stores the address of r

r->data=49;

r->next=s;//r->next stores the address of s

s->data=23;

s->next=t;//s->next stores the address of t

t->data=18;

t->next=NULL;

if(checkPlaindrome(p))

printf("YES,Linked list is a Palindrome\n");

else

printf("NO,Linked list is not a Palindrome\n");


return 0;

Output:

//7.Write a program to reverse a linked list

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node*next;

};

void display(struct node*p)//i. displaying elemnets of linked list

int i=1;

while(p!=NULL)

printf("Entered number = %d\n",p->data);

p=p->next;

struct node*reverse(struct node*p)

struct node* curr=p;

struct node *prev=NULL;

struct node *tem=NULL;

while(curr!=NULL)

tem=curr->next;

curr->next=prev;

prev=curr;
curr=tem;

p=prev;

return p;

int main()

struct node*p;//a.creating a linked list

struct node*q;

struct node*r;

struct node*s;

struct node*t;

p=(struct node*)malloc(sizeof(struct node));

q=(struct node*)malloc(sizeof(struct node));

r=(struct node*)malloc(sizeof(struct node));

s=(struct node*)malloc(sizeof(struct node));

t=(struct node*)malloc(sizeof(struct node));

p->data=18;

p->next=q;//p->next stores the address of q

q->data=23;

q->next=r;//q->next stores the address of r

r->data=49;

r->next=s;//r->next stores the address of s

s->data=59;

s->next=t;//s->next stores the address of t

t->data=62;

t->next=NULL;

printf("Original linked list\n");

display(p);

printf("reverse linked list\n");

p=reverse(p);
display(p);

return 0;

Output:

You might also like