Tut 7

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Q1.

#include<stdio.h>

#include<stdlib.h>

struct node{

int data;

struct node *next;

};

struct node* newNode(int data){

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

temp->next=NULL;

temp->data=data;

return temp;

struct node *addNode(struct node *head, int data){

struct node *temp=head;

if(head==NULL){

return newNode(data);

while(head->next!=NULL){

head=head->next;

head->next=newNode(data);

return temp;

struct node *addOnTop(struct node *head, int data){


struct node *temp=newNode(data);

temp->next=head;

return temp;

void dispLL(struct node *head){

printf("\nElements in Linked List:\n");

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 findISn(struct node *list1,struct node *list2){

// Assuming list of positive nums.

if(list1==NULL || list2==NULL)

return -1;

struct node *temp;

while(list1->next!=NULL){

temp=list2;

while(temp->next!=NULL){

if(list1==temp)
{

return list1->data;

temp=temp->next;

list1=list1->next;

int main(){

struct node *common=newNode(25);

addNode(common,10);

addNode(common,13);

struct node *L1=newNode(4);

L1->next=common;

L1=addOnTop(L1,5);

L1=addOnTop(L1,9);

struct node *L2=newNode(2);

L2->next=common;

L2=addOnTop(L2,8);

/*Currently list looks like:

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;

struct node *next;

};

struct node* newNode(int data){

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

temp->next=NULL;

temp->data=data;

return temp;

struct node* addNode(struct node *head, int data){

struct node *temp=head;

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){

// x and y need to be different or the code won't work as expected.:(

if(head==NULL || x==y){

printf("\nList is empty or x=y.");

return head;

struct node *currX=head,*prevX=NULL,*currY=head,*prevY=NULL;

while(currX->data!=x && currX!=NULL){

prevX=currX;

currX=currX->next;

while(currY->data!=y && currY!=NULL){

prevY=currY;

currY=currY->next;

if(currX==NULL || currY==NULL)return head;

if(prevX!=NULL)

prevX->next=currY;

else

head=currY;

if(prevY!=NULL)

prevY->next=currX;

else

head=currX;

struct node *temp=currX->next;

currX->next=currY->next;
currY->next=temp;

return head;

void dispLL(struct node *head){

printf("\nElements in Linked List:\n");

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 head;

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;

struct node *next;

};

struct node* newNode(int data){

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

temp->next=NULL;

temp->data=data;

return temp;

struct node *addNode(struct node *head, int data){

struct node *temp=head;

if(head==NULL){

return newNode(data);

while(head->next!=NULL){

head=head->next;

head->next=newNode(data);
return temp;

struct node *revLL(struct node *head){

if(head==NULL || head->next==NULL)

return head;

struct node *p1=NULL,*p2=head,*p3=NULL;

while(p2!=NULL){

p3=p2->next;

p2->next=p1;

p1=p2;

p2=p3;

return p1;

void dispLL(struct node *head){

printf("\nElements in Linked List:\n");

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 head;

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);

struct node *temp=revLL(&head);

printf("\nAfter:");

dispLL(temp);

return 0;

Q4.
#include<stdio.h>

#include<stdlib.h>

struct node{
int data;

struct node *next;

};

struct node* newNode(int data){

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

temp->next=NULL;

temp->data=data;

return temp;

struct node *addNode(struct node *head, int data){

struct node *temp=head;

if(head==NULL){

return newNode(data);

while(head->next!=NULL){

head=head->next;

head->next=newNode(data);

return temp;

struct node* reverse(struct node* head, struct node* prev)

if (head == NULL)

return NULL;
struct node* temp;

struct node* curr;

curr = head;

while (curr != NULL && curr->data % 2 == 0) {

temp = curr->next;

curr->next = prev;

prev = curr;

curr = temp;

if (curr != head) {

head->next = curr;

curr = reverse(curr, NULL);

return prev;

else {

head->next = reverse(head->next, head);

return head;

void dispLL(struct node *head){

printf("\nElements in Linked List:\n");

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* head = NULL;

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;

struct node *next;

};

struct node* newNode(int data){

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

temp->next=NULL;

temp->data=data;

return temp;

struct node *addNode(struct node *head, int data){

struct node *temp=head;

if(head==NULL){

return newNode(data);

while(head->next!=NULL){

head=head->next;

head->next=newNode(data);

return temp;

struct node *addOnTop(struct node *head, int data){


struct node *temp=newNode(data);

temp->next=head;

return temp;

void dispLL(struct node *head){

printf("\nElements in Linked List:\n");

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 cUneek(struct node *head){

int c=0,ctr;

if (head == NULL)

return c;

struct node *temp = head;

while (head -> next != NULL){

temp = head -> next;

ctr = 0;

while (temp -> next != NULL){

if(temp->data==head->data)
ctr=1;

temp = temp -> next;

head=head->next;

if(ctr==0)c++;

return c;

int main(){

struct node *list=addNode(NULL,23);

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);

printf("\nNumber of unique elements: %d",cUneek(list));

}
Q6.
#include<stdio.h>

#include<stdlib.h>

struct node{

int data;

struct node *next;

};

struct node* newNode(int data){

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

temp->next=NULL;

temp->data=data;

return temp;

struct node *addNode(struct node *head, int data){

struct node *temp=head;

if(head==NULL){

return newNode(data);

while(head->next!=NULL){

head=head->next;

head->next=newNode(data);

return temp;

struct node *addOnTop(struct node *head, int data){


struct node *temp=newNode(data);

temp->next=head;

return temp;

void dispLL(struct node *head){

printf("\nElements in Linked List:\n");

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 countLL(struct node *head){

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;

struct node *temp1=head,*temp2=NULL;

for(i=1; i<s-m; i++){

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(){

struct node *head=addNode(NULL,9);

addNode(head,23);

addNode(head,14);

addNode(head,67);

addNode(head,34);

int n;

printf("Write n(max.5): ");

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;

struct node *next;

};

struct node* newNode(int data){

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

temp->next=NULL;

temp->data=data;

return temp;

struct node *addNode(struct node *head, int data){

struct node *temp=head;

if(head==NULL){

return newNode(data);

while(head->next!=NULL){

head=head->next;
}

head->next=newNode(data);

return temp;

struct node *addOnTop(struct node *head, int data){

if(head==NULL)

return newNode(data);

struct node *temp=newNode(data);

temp->next=head;

return temp;

void dispLL(struct node *head){

printf("\nElements in Linked List:\n");

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;

struct node *add1(struct node *head){

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){

num = num*10 + head->data;

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(){

struct node *list=addNode(NULL,1);

addNode(list,3);

addNode(list,8);
addNode(list,6);

dispLL(list);

//calling add function (8) times.

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;

You might also like