Functions in C

  • + 0 comments

    Linked list

    include

    using namespace std;

    struct Node { int data; Node* next; };

    Node* head = nullptr;

    void insertEnd(int val) { Node* newNode = new Node{val, nullptr}; if (!head) head = newNode; else { Node* temp = head; while (temp->next) temp = temp->next; temp->next = newNode; } }

    void deleteNode(int val) { if (!head) return; if (head->data == val) { Node* temp = head; head = head->next; delete temp; return; } Node* temp = head; while (temp->next && temp->next->data != val) temp = temp->next; if (temp->next) { Node* del = temp->next; temp->next = del->next; delete del; } }

    void display() { Node* temp = head; while (temp) { cout << temp->data << " -> "; temp = temp->next; } cout << "NULL\n"; }

    int main() { insertEnd(10); insertEnd(20); insertEnd(30); display(); deleteNode(20); display(); } Stack

    include

    using namespace std;

    define MAX 5

    int stack[MAX], top = -1;

    void push(int val) { if (top == MAX - 1) cout << "Stack Overflow\n"; else stack[++top] = val; }

    void pop() { if (top == -1) cout << "Stack Underflow\n"; else cout << "Popped: " << stack[top--] << endl; }

    void display() { if (top == -1) cout << "Stack Empty\n"; else { for (int i = top; i >= 0; i--) cout << stack[i] << " "; cout << endl; } }

    int main() { push(10); push(20); push(30); display(); pop(); display(); } Queue

    include

    using namespace std;

    define MAX 5

    int queueArr[MAX]; int front = -1, rear = -1;

    void enqueue(int val) { if (rear == MAX - 1) cout << "Queue Overflow\n"; else { if (front == -1) front = 0; queueArr[++rear] = val; } }

    void dequeue() { if (front == -1 || front > rear) cout << "Queue Underflow\n"; else cout << "Dequeued: " << queueArr[front++] << endl; }

    void display() { if (front == -1 || front > rear) cout << "Queue Empty\n"; else { for (int i = front; i <= rear; i++) cout << queueArr[i] << " "; cout << endl; } }

    int main() { enqueue(5); enqueue(10); enqueue(15); display(); dequeue(); display(); }

    Binary Tree

    include

    using namespace std;

    struct Node { int data; Node* left; Node* right; Node(int val) : data(val), left(nullptr), right(nullptr) {} };

    void inorder(Node* root) { if (!root) return; inorder(root->left); cout << root->data << " "; inorder(root->right); }

    int main() { Node* root = new Node(10); root->left = new Node(5); root->right = new Node(15); root->left->left = new Node(3); root->left->right = new Node(7);

    cout << "Inorder traversal: ";
    inorder(root);
    

    } Graph (Adjacency List Representation)

    include

    include

    using namespace std;

    class Graph { int V; vector> adj;

    public: Graph(int v) { V = v; adj.resize(V); }

    void addEdge(int u, int v) {
        adj[u].push_back(v);
        adj[v].push_back(u); // Undirected graph
    }
    
    void display() {
        for (int i = 0; i < V; i++) {
            cout << "Node " << i << ": ";
            for (int j : adj[i])
                cout << j << " ";
            cout << endl;
        }
    }
    

    };

    int main() { Graph g(5); g.addEdge(0, 1); g.addEdge(0, 4); g.addEdge(1, 2); g.addEdge(1, 3); g.addEdge(1, 4); g.display(); }