Created
October 17, 2021 16:34
-
-
Save suryansh011/906b48fcf44723680624f1c785adb097 to your computer and use it in GitHub Desktop.
Link List Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
struct Node { | |
int data; | |
struct Node *link; | |
}; | |
void printList(struct Node *head) { | |
struct Node *ptr = head; | |
while(ptr != NULL) { | |
printf("%d ", ptr->data); | |
ptr = ptr->link; | |
} | |
printf("\n"); | |
} | |
void add_beg(struct Node **head, int key) { | |
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node)); | |
ptr->data = key; | |
ptr->link = *head; | |
*head = ptr; | |
} | |
void add_end(struct Node **head, int key) { | |
struct Node *ptr; | |
ptr = (struct Node *)malloc(sizeof(struct Node)); | |
ptr->data = key; | |
ptr->link = NULL; | |
if(*head == NULL) | |
*head = ptr; | |
else { | |
struct Node *lastNode = *head; | |
while(lastNode->link != NULL) { | |
lastNode = lastNode->link; | |
} | |
lastNode->link = ptr; | |
} | |
} | |
int getpos(struct Node *head, int key) { | |
int pos = 0; | |
struct Node* ptr; | |
ptr = head; | |
while (ptr != NULL && ptr->data != key) { | |
pos++; | |
ptr = ptr->link; | |
} | |
return (ptr != NULL) ? pos : -1; | |
} | |
void addpos(struct Node *head, int key, int pos) { | |
struct Node *ptr = head; | |
struct Node *ptr2 = malloc(sizeof(struct Node)); | |
ptr2->data = key; | |
ptr2->link = NULL; | |
pos--; | |
while(pos != 1) { | |
ptr = ptr->link; | |
pos--; | |
} | |
ptr2->link = ptr->link; | |
ptr->link = ptr2; | |
} | |
void delpos(struct Node **head, int pos) { | |
struct Node *current = *head; | |
struct Node *previous = *head; | |
if (*head == NULL) | |
return; | |
else if(pos == 1) { | |
*head = current->link; | |
free(current); | |
current = NULL; | |
} | |
else { | |
while(pos != 1) { | |
previous = current; | |
current = current->link; | |
pos--; | |
} | |
previous->link = current->link; | |
free(current); | |
current = NULL; | |
} | |
} | |
struct Node* reverse(struct Node *head) { | |
struct Node *prev = NULL; | |
struct Node *next = NULL; | |
while(head != NULL) { | |
next = head->link; | |
head->link = prev; | |
prev = head; | |
head = next; | |
} | |
head = prev; | |
return head; | |
} | |
int main() { | |
struct Node* head = (struct Node*)malloc(sizeof(struct Node)); | |
head = NULL; | |
int i; | |
int n; | |
scanf("%d", &n); | |
if(n >= 2 && n <= 600) { | |
for(i = 0; i < n; i++) { | |
int l; | |
scanf("%d", &l); | |
if(l == 1) { | |
int x; | |
scanf("%d", &x); | |
add_end(&head, x); | |
printList(head); | |
} | |
else if (l == 2) { | |
int x; | |
scanf("%d", &x); | |
add_beg(&head, x); | |
printList(head); | |
} | |
else if(l == 3) { | |
int x, y; | |
scanf("%d %d", &y, &x); | |
if (getpos(head, y) == -1) | |
add_end(&head, x); | |
else { | |
int t = getpos(head, y); | |
addpos(head, x, t + 2); | |
} | |
printList(head); | |
} | |
else if (l == 4) { | |
int x, y; | |
scanf("%d %d", &y, &x); | |
if (getpos(head, y) == -1) | |
add_beg(&head, x); | |
else { | |
int t = getpos(head, y); | |
if (t + 1 == 1) | |
add_beg(&head, x); | |
else | |
addpos(head, x, t + 1); | |
} | |
printList(head); | |
} | |
else if (l == 5) { | |
int x; | |
scanf("%d", &x); | |
int t = getpos(head, x); | |
delpos(&head, t + 1); | |
printList(head); | |
} | |
else if (l == 6) { | |
int x; | |
scanf("%d", &x); | |
int t = getpos(head, x); | |
if (t == -1) { | |
printf("%d\n", t); | |
} else { | |
printf("%d\n", t+1); | |
} | |
} | |
else if (l == 7) { | |
head = reverse(head); | |
printList(head); | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment