Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Node {
- int data;
- struct Node* next;
- };
- struct Node* head;
- void Reverse(struct Node* head) {
- struct Node *current, *prev, *next;
- current = head;
- prev = NULL;
- while(current != NULL) {
- next = current->next;
- current->next = prev;
- current = next;
- prev = current;
- }
- head = prev;
- }
- void Insert(int data, int n) {
- struct Node* temp1 = (struct Node*)malloc(sizeof(struct Node));
- temp1->data = data;
- temp1->next = NULL;
- if(n==1) {
- temp1->next = head;
- head = temp1;
- }
- int i;
- struct Node* temp2 = (struct Node*)malloc(sizeof(struct Node));
- for(i=0;i<n-2;i++) {
- temp2=temp2->next;
- }
- temp1->next = temp2->next;
- temp2->next = temp1;
- }
- void Delete(int n) {
- struct Node* temp1 = head;
- int i;
- for(i=0;i<n-2;i++) {
- temp1=temp1->next;
- }
- struct Node* temp2 = temp1->next;
- temp1->next = temp2->next;
- free(temp2);
- }
- void Print() {
- struct Node* temp = head;
- int i;
- printf("List is: ");
- while(temp->next != NULL) {
- printf(" %d", temp->data);
- temp=temp->next;
- }
- }
- int main() {
- head = NULL;
- Insert(4,1);
- Insert(8,2);
- Insert(6,3);
- Insert(3,4);
- Insert(9,5);
- Insert(23,6);
- Insert(12,2);
- Insert(45,5);
- Print();
- Delete(2);
- Print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement