0% found this document useful (0 votes)
26 views3 pages

19-Linked List.c

Uploaded by

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

19-Linked List.c

Uploaded by

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

// data|next

// 2|200 18|300 24|400 3|500 5|600 7|700 9|800 6|900 12|NULL


// 100 200 300 400 500 600 700 800 900
// 24| 18| 2| 3| 5| 7|
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct SLL {
struct Node* first;
struct Node* last;
};
// int x=5;
struct SLL* createSLL(){
struct SLL* x =(struct SLL*)malloc(sizeof(struct SLL));
x->first = x->last = NULL;
return x;
}
// first->null
// last->null
struct Node* createNode(int ele){
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
newnode->data = ele;
newnode->next = NULL;
return newnode;
}

// 2|200 18|300 24|NULL


// 100 200 300
// first
// last
void insertNode(struct SLL* x,int ele){
if(x->last == NULL){
x->first = createNode(ele);//100
x->last = x->first;//100
}
else {
x->last->next = createNode(ele);//x->last=
x->last = x->last->next;//200
}
}
void printSLL(struct SLL* x){
struct Node* temp = x->first;//100
while(temp!=NULL){//
printf("%d ",temp->data);//2
temp = temp->next;// 300
}

}
void reverseSLL(struct SLL* x,struct Node* start,struct Node* end,struct Node*
mainprev){
struct Node* temp = start;
struct Node* mainNextNode = end!=NULL ? end->next:end;//4
struct Node* prev = mainNextNode;//4
struct Node* nextNode = NULL;
// end->next
// 2|4 18|1 24|2
// 1 2 3
// temp = 24|2(3)
// prev = 2
// 2|4 18|1 24|2
while(temp!=mainNextNode){//1..3
// nextNode = temp->next;// 4
// temp->next = prev;//2
// prev = temp;// 3
// temp = nextNode;// 4
nextNode = temp->next;
temp->next = prev;
prev = temp;//3
temp = nextNode;
}
if(mainprev != NULL){
mainprev->next = prev;
}
else{
x->first = prev;
}
}
int main(){
int n,a;
scanf("%d",&n);
struct SLL* x = createSLL();
for(int i=0;i<n;i++){
scanf("%d",&a);
insertNode(x,a);
}
// printSLL(x);
// printf("\n");
struct Node* temp = x->first;
struct Node* start = x->first;
struct Node* end = NULL;
struct Node* prev = NULL;
struct Node* mainprev = NULL;
int reverse = 1;
// [,24|4,3,5,7,9|8,6|9,12|NULL] == [2|4,3,5,7,9,12,6]
// 3 4 5 6 7 8 9 3 4 5 6 7 8 9
// temp = 9
// start = 8
// end = 9
//mainprev = 0
while (temp!= NULL){//9
prev = temp; // 9
if(temp->data%2 == 0){
end = temp;// 9
reverse = 0;
}
else {
if (reverse == 0){
reverseSLL(x,start,end,mainprev);//
reverse =1;
}
start = temp->next;//8
mainprev = prev;//7
}
temp = temp->next;//100->200->300------->Null
}
if(reverse ==0){
reverseSLL(x,start,end,mainprev);
}
printSLL(x);
}
// {2,18,24,3,5,7,9,6,12}
// {24,18,2,3,5,7,9,12,6}
// {7,4,6,8,2,5,3} == {7,2,8,6,4,5,3}

// 7->2->8->6->4->5->3

// {7,6,4,8,2,5,3}
// {7,6,8,4,2,5,3}
// {7,6,8,2,4,5,3}
// {7,8,6,2,4,5,3}
// {7,8,2,6,4,5,3}
// {7,2,8,6,4,5,3}

You might also like