Creating innovative strategies for optimizing network resources is paramount in response to the growing demand for fast and reliable data transmission. This study delves into a unique method to enhance power allocation and throughput in 5G cellular systems. We aim to conserve resources and ensure top-tier communication through direct terminal connections using the Device-to-Device protocol and a modified Gale-Shapley algorithm. Our approach's robustness is tested in two scenarios: firstly, in standard 5G operations that focus on minimizing energy use while maximizing signal reliability, evaluating parameters like losses, gain, proximity of transmitters to receivers, and capacity using the Gale-Shapley algorithm. Second, we simulate a disaster-induced network disruption in which D2D devices autonomously establish connections without functional base stations. Our findings from detailed MATLAB simulations highlight that D2D communications within the Millimeter Wave frequency band consistently maintain reliable relationships, achieving network capacity rates between 150 and 180 Mbps under regular conditions and 110 to 140 Mbps during disaster scenarios. This underscores our approach's potential to enhance 5G system performance and reliability significantly.INDEX TERMS Device-to-device (D2D), Gale-Shapley Matching Theory, non-orthogonal multiple access (NOMA), power allocation, 5G.