Professional Documents
Culture Documents
Tut 7
Tut 7
Tut 7
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
};
temp->next=NULL;
temp->data=data;
return temp;
if(head==NULL){
return newNode(data);
while(head->next!=NULL){
head=head->next;
head->next=newNode(data);
return temp;
temp->next=head;
return temp;
if(head==NULL){
printf("List is empty.");
return;
printf("%d",head->data);
head=head->next;
while(head!=NULL){
printf("->%d",head->data);
head=head->next;
if(list1==NULL || list2==NULL)
return -1;
while(list1->next!=NULL){
temp=list2;
while(temp->next!=NULL){
if(list1==temp)
{
return list1->data;
temp=temp->next;
list1=list1->next;
int main(){
addNode(common,10);
addNode(common,13);
L1->next=common;
L1=addOnTop(L1,5);
L1=addOnTop(L1,9);
L2->next=common;
L2=addOnTop(L2,8);
9 - 5 - 4_
25 - 10 - 13 - NULL
8 - 2_/
*/
printf("%d",findISn(L1,L2));
return 0;
}
Q2.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
};
temp->next=NULL;
temp->data=data;
return temp;
if(head==NULL){
return newNode(data);
while(head->next!=NULL){
head=head->next;
head->next=newNode(data);
return temp;
}
struct node* swap2Nodes(struct node *head,int x,int y){
if(head==NULL || x==y){
return head;
prevX=currX;
currX=currX->next;
prevY=currY;
currY=currY->next;
if(prevX!=NULL)
prevX->next=currY;
else
head=currY;
if(prevY!=NULL)
prevY->next=currX;
else
head=currX;
currX->next=currY->next;
currY->next=temp;
return head;
if(head==NULL){
printf("List is empty.");
return;
printf("%d",head->data);
head=head->next;
while(head!=NULL){
printf("->%d",head->data);
head=head->next;
int main(){
head.data=5;
head.next=NULL;
addNode(&head,8);
addNode(&head,33);
addNode(&head,9);
addNode(&head,7);
addNode(&head,18);
addNode(&head,25);
dispLL(&head);
swap2Nodes(&head,33,18);
dispLL(&head);
return 0;
Q3.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
};
temp->next=NULL;
temp->data=data;
return temp;
if(head==NULL){
return newNode(data);
while(head->next!=NULL){
head=head->next;
head->next=newNode(data);
return temp;
if(head==NULL || head->next==NULL)
return head;
while(p2!=NULL){
p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3;
return p1;
if(head==NULL){
printf("List is empty.");
return;
printf("%d",head->data);
head=head->next;
while(head!=NULL){
printf("->%d",head->data);
head=head->next;
}
}
int main(){
head.data=5;
head.next=NULL;
addNode(&head,8);
addNode(&head,33);
addNode(&head,9);
addNode(&head,7);
addNode(&head,18);
addNode(&head,25);
printf("Before:");
dispLL(&head);
printf("\nAfter:");
dispLL(temp);
return 0;
Q4.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
};
temp->next=NULL;
temp->data=data;
return temp;
if(head==NULL){
return newNode(data);
while(head->next!=NULL){
head=head->next;
head->next=newNode(data);
return temp;
if (head == NULL)
return NULL;
struct node* temp;
curr = head;
temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
if (curr != head) {
head->next = curr;
return prev;
else {
return head;
if(head==NULL){
printf("List is empty.");
return;
printf("%d",head->data);
head=head->next;
while(head!=NULL){
printf("->%d",head->data);
head=head->next;
int main(){
struct node* p;
head=addNode(head,1);
addNode(head,2);
addNode(head,3);
addNode(head,3);
addNode(head,4);
addNode(head,6);
addNode(head,8);
addNode(head,5);
printf("Before:");
dispLL(head);
head = reverse(head,NULL);
printf("\nAfter:");
dispLL(head);
return 0;
}
Q5.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
};
temp->next=NULL;
temp->data=data;
return temp;
if(head==NULL){
return newNode(data);
while(head->next!=NULL){
head=head->next;
head->next=newNode(data);
return temp;
temp->next=head;
return temp;
if(head==NULL){
printf("List is empty.");
return;
printf("%d",head->data);
head=head->next;
while(head!=NULL){
printf("->%d",head->data);
head=head->next;
int c=0,ctr;
if (head == NULL)
return c;
ctr = 0;
if(temp->data==head->data)
ctr=1;
head=head->next;
if(ctr==0)c++;
return c;
int main(){
addNode(list,12);
addNode(list,8);
addNode(list,78);
addNode(list,5);
addNode(list,45);
addNode(list,8);
addNode(list,15);
addNode(list,18);
addNode(list,20);
addNode(list,2);
addNode(list,19);
addNode(list,9);
addNode(list,8);
addNode(list,25);
addNode(list,17);
dispLL(list);
}
Q6.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
};
temp->next=NULL;
temp->data=data;
return temp;
if(head==NULL){
return newNode(data);
while(head->next!=NULL){
head=head->next;
head->next=newNode(data);
return temp;
temp->next=head;
return temp;
if(head==NULL){
printf("List is empty.");
return;
printf("%d",head->data);
head=head->next;
while(head!=NULL){
printf("->%d",head->data);
head=head->next;
int c=0;
if(head==NULL)return c;
while(head!=NULL){
c++;
head=head->next;
return c;
}
struct node* moveLastM(struct node* head,int m){
if(head==NULL)
return head;
int i,s=countLL(head);
if(s<=m)return head;
temp1=temp1->next;
temp2=temp1->next;
temp1->next=NULL;
temp1=temp2;
while(temp2->next!=NULL)
temp2=temp2->next;
temp2->next=head;
return temp1;
int main(){
addNode(head,23);
addNode(head,14);
addNode(head,67);
addNode(head,34);
int n;
scanf("%d",&n);
printf("Before:");
dispLL(head);
head=moveLastM(head,n);
dispLL(head);
return 0;
Q7.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
};
temp->next=NULL;
temp->data=data;
return temp;
if(head==NULL){
return newNode(data);
while(head->next!=NULL){
head=head->next;
}
head->next=newNode(data);
return temp;
if(head==NULL)
return newNode(data);
temp->next=head;
return temp;
if(head==NULL){
printf("List is empty.");
return;
printf("%d",head->data);
head=head->next;
while(head!=NULL){
printf("->%d",head->data);
head=head->next;
if(head==NULL)return head;
struct node *temp=head;
while(head->next!=NULL)
head=head->next;
if(head->data!=9){
head->data++;
return temp;
head=temp;
int num=0,c;
while(head!=NULL){
temp = head;
head = head->next;
free(temp);
num++;
temp=NULL;
while(num!=0){
c = num % 10;
num /= 10;
temp = addOnTop(temp,c);
return temp;
int main(){
addNode(list,3);
addNode(list,8);
addNode(list,6);
dispLL(list);
list=add1(list);
dispLL(list);
list=add1(list);
dispLL(list);
list=add1(list);
dispLL(list);
list=add1(list);
dispLL(list);
list=add1(list);
dispLL(list);
list=add1(list);
dispLL(list);
list=add1(list);
dispLL(list);
list=add1(list);
return 0;