c to c convertor

Solutions on MaxInterview for c to c convertor by the best coders in the world

showing results for - "c to c convertor"
Daniela
05 Aug 2016
1// C++ program to delete middle
2// of a linked list
3#include <bits/stdc++.h>
4using namespace std;
5
6/* Link list Node */
7struct Node {
8int data;
9struct Node* next;
10};
11// count of nodes
12int countOfNodes(struct Node* head)
13{
14int count = 0;
15while (head != NULL) {
16head = head->next;
17count++;
18}
19return count;
20}
21
22// Deletes middle node and returns
23// head of the modified list
24struct Node* deleteMid(struct Node* head)
25{
26// Base cases
27if (head == NULL)
28return NULL;
29if (head->next == NULL) {
30delete head;
31return NULL;
32}
33struct Node* copyHead = head;
34
35// Find the count of nodes
36int count = countOfNodes(head);
37
38// Find the middle node
39int mid = count / 2;
40
41// Delete the middle node
42while (mid-- > 1) {
43head = head->next;
44}
45
46// Delete the middle node
47head->next = head->next->next;
48
49return copyHead;
50}
51
52// A utility function to print
53// a given linked list
54void printList(struct Node* ptr)
55{
56while (ptr != NULL) {
57cout << ptr->data << "->";
58ptr = ptr->next;
59}
60cout << "NULL\n";
61}
62
63// Utility function to create a new node.
64Node* newNode(int data)
65{
66struct Node* temp = new Node;
67temp->data = data;
68temp->next = NULL;
69return temp;
70}
71
72/* Driver program to test above function*/
73int main()
74{
75/* Start with the empty list */
76struct Node* head = newNode(1);
77head->next = newNode(2);
78head->next->next = newNode(3);
79head->next->next->next = newNode(4);
80
81cout << "Given Linked List\n";
82printList(head);
83
84head = deleteMid(head);
85
86cout << "Linked List after deletion of middle\n";
87printList(head);
88
89return 0;
90}
similar questions
queries leading to this page
c to c convertor